lunedì 26 dicembre 2011

Create a Web Service with JAX-WS, Eclipse, Maven2 and deploy on Tomcat or Jetty

We create a web service in these steps:

1 - create Web service endpoint interface
2 - create Web service endpoint that implements interface
3 - publish Web Service with Endpoint publisher
4 - create Client for Web Service generated with wsimport command
5 - create Web Project with Maven2
6 - generate web service classes with wsgen command
7 - configure web service on web.xml and sun-jaxws.xml files
8 - create war file with Maven2
9 - deploy on jetty server (with Maven2) and also on Tomcat

For this example I have downloaded:
- Eclipse Indigo: http://www.eclipse.org/downloads/
- Tomcat7: http://tomcat.apache.org/download-70.cgi
- jdk7: http://www.oracle.com/technetwork/java/javase/downloads/index.html
- Maven 3.0.3: http://maven.apache.org/download.html

1 - create Web service endpoint interface:

open eclipse --> create new Java Project named JAX-WS and the endpoint web service interface below:


package com.myapp;


import javax.jws.WebService;
import javax.jws.WebMethod;


@WebService
public interface MyService 
{
@WebMethod
String getMessage(String name);
}


2 - Now we create the endpoint implementation of the interface:


package com.myapp;


import javax.jws.WebService;


@WebService(endpointInterface = "com.myapp.MyService")
public class MyServiceImpl implements MyService
{
@Override
public String getMessage(String yourName)
{
return "Hello " + yourName + " this is your first web service in JAX-WS";
}
}



3 - Now we publish the web service using the static method of the Endpoint class, creating and running the Main class below:

package com.publisher;

import javax.xml.ws.Endpoint;

import com.myapp.MyServiceImpl;

public class PublisherMain 
{

public static void main(String[] args)
{
Endpoint.publish("http://localhost:8080/WS/MyService", new MyServiceImpl());
}
}


After running the PublisherMain class we can put the address http://localhost:8080/WS/MyService?wsdl on address bar of the browser and see the wsdl file generated.

4 - Create the client:

Create a new Java Project named JAX-WS Client, open prompt and go into src folder in JAX-WS Client project, then put this command:

wsimport –s . http://localhost:8080/WS/MyService?wsdl


In this way you have generated all client classes. At the end you can create the java Main class to call  the service below:


package com.client;


import com.myapp.MyService;
import com.myapp.MyServiceImplService;


public class MyServiceClient {


public static void main(String[] args)
{
MyServiceImplService myService = new MyServiceImplService();
MyService service = myService.getMyServiceImplPort();
System.out.println(service.getMessage("Christian"));
}
}

Running the client class above we can see the message printed on console.

5 - We create Web project using Maven. After installing maven we set environment variabile M2_HOME on installation folder of Maven and link path variable on bin folder of %M2_HOME% folder.

After step above we can create project folder, for example MyWebProject, and in the prompt we can go in this folder and put this command:

mvn archetype:create -DgroupId=com.myapp -DartifactId=MyWebProject -DarchetypeArtifactId=maven-archetype-webapp.





If you want build project in the command line go to MyWebProject folder and type this command:

mvn clean package 

This will generate a folder named target  under MyWebProject folder and inside it the file MyWebProject.war.


But let's insert our service into the web project MyWebProject:

go into MyWebProject and create under src/main the folders /java/com/myapp. In this folder put MyService.java and MyServiceImpl.java implemented above.

Now open file pom.xml under MyWebProject folder and into the tag <build> insert the following two plugins:

- plugin to compile project
- plugin to build project on server jetty

  <build>
    <finalName>MyProject-webapp</finalName>
         <plugins>
           <plugin>
              <groupId>org.apache.maven.plugins</groupId>
              <artifactId>maven-compiler-plugin</artifactId>
              <configuration>
                   <source>1.5</source>
                   <target>1.5</target>
              </configuration>
           </plugin>
             <plugin>
                  <groupId>org.mortbay.jetty</groupId>
                  <artifactId>maven-jetty-plugin</artifactId>
             </plugin>
         </plugins>
</build>


Now you can build the project going under MyWebProject folder through  the command line and typing this command: mvn install 

With the command above appear target folder under MyWebProject folder and under target folder ther is MyWebProject.war and also the file .classes under folder target/classes/com/myapp.

Now you have to generate web service classes with this command under MyWebProject folder:

wsgen -s src -d target/classes -cp build/classes com.myapp.MyServiceImpl

the command above generates com.myapp.jaxws package under MyWebProject/src folder and the classes GetMessage.java and GetMessageResponse.java.

Open web.xml and configure Web Service as Listner and servlet:

 <?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
version="2.4">
<listener>
<listener-class>com.sun.xml.ws.transport.http.servlet.WSServletContextListener</listener-class>
</listener>
<servlet>
<servlet-name>MyServiceWS</servlet-name>
<servlet-class>com.sun.xml.ws.transport.http.servlet.WSServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>MyServiceWS</servlet-name>
<url-pattern>/myservice</url-pattern>
</servlet-mapping>
</web-app>

Then create under webapp/WEB-INF the file sun-jaxws like this:


<?xml version="1.0" encoding="UTF-8"?>
<endpoints xmlns="http://java.sun.com/xml/ns/jax-ws/ri/runtime" version="2.0">
<endpoint name="MyServiceWS"
 implementation="com.myapp.MyServiceImpl"
 url-pattern="/myservice"/>
</endpoints>

At the end you have to put into webapp/WEB-INF/lib all libreries needed for JAX-WS that are all these:

activation.jar
FastInfoset.jar
http.jar
jaxb-api.jar
jaxb-impl.jar
jaxb-rpc.jar
jaxws-api.jar
jaxws-rt.jar
jaxws-tools.jar
jsr173_api.jar
jsr181-api.jar
jsr250-api.jar
resolver.jar
saaj-api.jar
saaj-impl.jar
sjsxp.jar
stax-ex.jar
streambuffer.jar

At this point you can build application with "mvn install" command (another way to build project without plugin is with command "mvn clean package" in the root folder of MyWebProject) and then you can deploy the project on server Jetty with this command: mvn jetty:run


then you can open browser and put this address in the address bar:

http://localhost:8080/MyWebProject/myservice?wsdl

and view the wsdl.

At the end close server jetty with ctrl+C on prompt and let's deploy on tomcat: in the command line reach the bin folder under installation folder of tomcat. For Example: cd apache-tomcat-7.0.23 --> cd bin. Then type the command startup to start Tomcat. Now you can open browser and type this address: http://localhost:8080/manager. At this point tomcat manager will ask you login and password to access and they are configureable on file conf/tomcat-users.xml and you have to configure somthing like this:


<tomcat-users>  
  <role rolename="manager-gui"/>
  <user username="admin" password="admin" roles="manager-gui"/>
</tomcat-users>



After this is done you can access to Tomcat manager with login admin and password admin.

Under the list of application you can see that there is the possibility to choose the war file to deploy. We choose MyWebProject.war and deploy it. Now type the address

http://localhost:8080/MyWebProject/myservice?wsdl

on the address bar of the browser and you will see the wsdl.