Tutorial: How to create Axis2 web service with IntelliJ IDEA? Part 1: Service side

There is Axis2 plugin for IntelliJ IDEA, but it is not compatible with latest versions. So I lost some time trying to use this plugin. In this post we will not use plugin. Fortunately there is good tool called ant, which will generate some files for us.

What we are going to do? We will not run web service directly from IntelliJ IDEA. Instead of this we need to download Tomcat, install Axis2, install web service manually.

Step 1. Configure Tomcat and install Axis2
Tomcat version 8 is available here. Download and unzip.
Axis2. Download last WAR distibution. WAR file is packaged app which can be very easily installed in Tomcat.

Run Tomcat by executing bin/startup.bat (for Windows) or bin/startup.sh (for Linux/Mac). By default Tomcat will start on port 8080. Type “http://localhost:8080” and if everything is OK Tomcat main page will be opened.

1

We need access to Apps management page. To do it edit conf/tomcat-users.xml. Add “<user username=”admin” password=”admin” roles=”manager-gui”/>” between <tomcat-users> and </tomcat-users> tags.

1-2

In section “WAR file to deploy” deploy axis2.war file.

1-3

Then axis2 will appear in the list of applications.

1-4

Tomcat and Axis2 are ready, let’s create web service.

Step 2. Create project
Create project (“File”->”New”->”Project…”). We will not use any libraries and frameworks proposed by IntelliJ IDEA.

2

So structure of our project will be very simple.

2-2

Also it is needed to import Axis2 library. We need to download axis2 Binary Distribution from this page. The reason is why we cannot use library from WAR file is that WAR does not contain all libraries. Go to “File” -> “Project Structure…”->tab “Libraries”. Press green plus button and select lib directory from unzipped folder.

Step 3. Write code

In src folder create models directory. In directory create Worker class, which will be used to defined parameters of Worker instance. Write code:

public class Worker {
 public String getName() {
  return name;
 }
 public void setName(String name) {
  this.name = name;
 }
 public String getSurname() {
  return surname;
 }
 public void setSurname(String surname) {
  this.surname = surname;
 }
 public String getPosition() {
  return position;
 }
 public void setPosition(String position) {
  this.position = position;
 }
 public int getSalary() {
  return salary;
 }
 public void setSalary(int salary) {
  this.salary = salary;
 }
 private String name;
 private String surname;
 private String position;
 private int salary;
}

Little hint. You can type only write
 private String name;
 private String surname;
 private String position;
 private int salary;

then press “Alt”+”Insert”->”Getter and Setter”->Select all fields by “Shift”->”OK”.

Create “CustomService” class in src folder. Define needed functions. I wrote:

public Worker getWorkerWithChangedSalary(Worker worker){
 worker.setSalary(worker.getSalary()*2);
 return worker;
}

Step 4. Create configuration files

Create directory “resources” in root of project, then create “META-INF” inside of “resouces”. Put file services.xml in “META-INF” with the content below:
<service name="CustomService" scope="application" targetNamespace="http://doszhan.com/">
 <description>
  My Custom Service
 </description>
 <messageReceivers>
  <messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-only" class="org.apache.axis2.rpc.receivers.RPCInOnlyMessageReceiver" />
  <messageReceiver mep="http://www.w3.org/2004/08/wsdl/in-out" class="org.apache.axis2.rpc.receivers.RPCMessageReceiver" />
 </messageReceivers>
 <schema schemaNamespace="http://doszhan.com/xsd"/>
 <parameter name="ServiceClass">CustomService</parameter>
</service>

Create file build.xml:
<project name="ForAnt" basedir="." default="generate.service">
 <property environment="env"/>
 <property name="build.dir" value="build"/>
 <path id="axis2.classpath">
  <fileset dir="PATH_TO_AXIS2_LIB">
   <include name="*.jar"/>
  </fileset>
 </path>
 <target name="compile.service">
  <mkdir dir="${build.dir}"/>
  <mkdir dir="${build.dir}/classes"/>
  <mkdir dir="${build.dir}/resources"/>
   <javac debug="on"
    fork="true"
    destdir="${build.dir}/classes"
    srcdir="${basedir}/src"
    classpathref="axis2.classpath">
  </javac>
 </target>
 <target name="generate.wsdl" depends="compile.service">
  <taskdef name="java2wsdl"
   classname="org.apache.ws.java2wsdl.Java2WSDLTask"
   classpathref="axis2.classpath"/>
  <java2wsdl className="CustomService"
   outputLocation="${build.dir}"
   targetNamespace="http://doszhan.com/"
   schemaTargetNamespace="http://doszhan.com/xsd">
   <classpath>
    <pathelement path="${axis2.classpath}"/>
    <pathelement location="${build.dir}/classes"/>
   </classpath>
  </java2wsdl>
 </target>
 <target name="generate.service" depends="compile.service">
  <copy toDir="${build.dir}/classes" failonerror="false">
   <fileset dir="${basedir}/resources">
    <include name="**/*.xml"/>
   </fileset>
  </copy>
  <jar destfile="${build.dir}/CustomService.aar">
   <fileset excludes="**/Test.class" dir="${build.dir}/classes"/>
  </jar>
 </target>
 <target name="clean">
  <delete dir="${build.dir}"/>
 </target>
</project>

build.xml is for ant. How you see, in build.xml defined operations compile.service, generate.wsdl and generate.service. Pay attention to PATH_TO_AXIS2_LIB. Here you need to define axis2 library folder.

In the end of this step project directory will look like this:
4

Step 5. Build
To build the project we need to use Ant plugin for IntelliJ IDEA. Click on “Ant Build” on the right side of window. If there is no such button you can use hotkey “Ctrl”+”Shift”+”A” and write “Ant Build”.
Press green plus icon and select build.xml:

5

Double click on generate.service and our service file generated in folder build. Copy CustomService.aar file into TOMCATE_HOMEwebappsaxis2WEB-INFservices.

Then type http://localhost:8080/axis2/services/listServices in internet browser. If you see page below, you have successfully build and install service

5-1

If you have found a spelling error, please, notify us by selecting that text and pressing Ctrl+Enter.

Profile photo of Doszhan Kalibek

Doszhan Kalibek