Friday, 12 December 2008

Create your First Web Service Application using JAXWS

NetBeans Environment:
i) Create a new java Web Project name it "HelloService"
ii) create a new java class "Hello.java" and Add "@WebService" annotation at the top of the class
@WebService
public class Hello {
public String sayHello(String name) {
return "Hello " + name ;
}
}

iii) Deploy your application
right click the project and Deploy


That's it.. Your first web service application is ready.

Eclipse Evnironment:

i)Create the Dynamic Web Project "HelloService"
ii) web.xml
------------
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
<listener>
<listener-class>
com.sun.xml.ws.transport.http.servlet.WSServletContextListener</listener-class>
</listener>
<servlet>
<servlet-name>Hello</servlet-name>
<servlet-class>com.sun.xml.ws.transport.http.servlet.WSServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>Hello</servlet-name>
<url-pattern>/Hello</url-pattern>
</servlet-mapping>
</web-app>

iii) sun-jaxws.xml (create under WEB-INF folder )

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

iv) Copy all the jaxws api files into the WEB-INF\lib folder
v) Create the same Hello.java file (copy the code from step ii - webservices - netbeans setup)
vi) Run this application from Eclipise
Now you can view your service from the following url.
http://localhost:8080/HelloService/Hello

No comments:

Post a Comment