Friday, January 20, 2012

JAX-WS Deployment

To deploy your WS endpoint you need to package it as a war first then deploy it on your application server.
Note:- You can download the source code for this example from the resources section.
Ok, lets begin
 1) Open Eclipse.
2) Create a new Web project .
3) Create your Web Service interface (Greeting):
01.package juma.mohammad;
02. 
03.import javax.jws.WebMethod;
04.import javax.jws.WebService;
05. 
06.@WebService
07.public interface Greeting {
08.@WebMethod String sayHello(String name);
09.}
 4) Create your Web Service implementation (GreetingImpl):
01.package juma.mohammad;
02. 
03.import javax.jws.WebService;
04. 
05.@WebService(endpointInterface = "juma.mohammad.Greeting")
06.public class GreetingImpl implements Greeting {
07. 
08.@Override
09.public String sayHello(String name) {
10.return "Hello, Welcom to jax-ws " + name;
11.}
12. 
13.}
 5) Now , you need generate Web Services classes, open your command line, and type :
1.cd %project_home%
2.wsgen -s src -d build/classes -cp build/classes juma.mohammad.GreetingImpl
Good , now you have two classe(SayHello.java, SayHelloResponse.java) generated under /greetingWS/src/juma/mohammad/jaxws  .
6) Now we need to write our web.xml and put it under /greetingWS/WebContent/WEB-INF
01.<?xml version="1.0" encoding="UTF-8"?>
02.<web-app
05.xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
07.version="2.4">
08.<listener>
09.<listener-class>
10.com.sun.xml.ws.transport.http.servlet.WSServletContextListener
11.</listener-class>
12.</listener>
13.<servlet>
14.<servlet-name>GreetingWS</servlet-name>
15.<servlet-class>
16.com.sun.xml.ws.transport.http.servlet.WSServlet
17.</servlet-class>
18. 
19.</servlet>
20.<servlet-mapping>
21.<servlet-name>GreetingWS</servlet-name>
22.<url-pattern>/greeting</url-pattern>
23.</servlet-mapping>
24.</web-app>
Note that in this web.xml we  just defined two things : 1) listener-class, 2)servlet !
7) ok,the final step is that you need to add sun-jaxws.xml under /greetingWS/WebContent/WEB-INF  which contains endpoints definition:
1.<?xml version="1.0" encoding="UTF-8"?>
2.<endpoints xmlns="http://java.sun.com/xml/ns/jax-ws/ri/runtime" version="2.0">
3.<endpoint
4.name="GreetingWS"
5.implementation="juma.mohammad.GreetingImpl"
6.url-pattern="/greeting"/>
7.</endpoints>
8) You need to download JAX-WS library and put jars under /greetingWS/WebContent/WEB-INF/lib.
You can get jars from the attached sample :)
9) Great, now you just need to export this project as a war, and drop it under your Tomcat webapps folder .
10) Run Tomcat.
11) Try this url: http://localhost:8080/greetingWS/greeting
Congratulations... web service information page appeared :)

http://java.dzone.com/articles/jax-ws-deployment-five-minute

No comments:

Post a Comment