In this article, we will create a JAX WS Hello World program.
Tools/Technologies Used :
- Eclipse IDE
- Tomcat Server
- Metro jars
Here is an overview of the steps involved :
- Create a Dynamic web project
- Add Web service class
- Define WSServlet and url-mapping in web.xml
- Add sun-jaxws.xml and provide reference to the webservice class
- Add Metro jars
- Deploy and Test
Step 1: (Create Dynamic Web project)
In Eclipse, create a dynamic web project using :
File- > New -> Dynamic Web Project
Name the project as JAXWSHelloWorld.
Step 2: (WebService class)
Create a package com.topjavatutorial.jaxws in the src folder.
Add the below class HelloWorld in it.
package com.topjavatutorial.jaxws; import javax.jws.WebMethod; import javax.jws.WebService; @WebService public class HelloWorld { @WebMethod public String hello(String name){ return "Hello " + name; } }
Step 3: (web.xml changes)
Since we created a dynamic web project, web.xml file should be available under WEB-INF folder.
If you don’t see, you can create it with below steps:
- Right click on the project.
- Select JAVA EE Tools -> Generate Deployment Descriptor Stub
Configure the class WSServletContextListener as listener and WSServlet as the servet class. Also, add url-mapping in web.xml as follows:
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" version="3.0"> <display-name>JAXWSHelloWorld</display-name> <listener> <listener-class> com.sun.xml.ws.transport.http.servlet.WSServletContextListener </listener-class> </listener> <servlet> <servlet-name>jaxws</servlet-name> <servlet-class> com.sun.xml.ws.transport.http.servlet.WSServlet </servlet-class> </servlet> <servlet-mapping> <servlet-name>jaxws</servlet-name> <url-pattern>/*</url-pattern> </servlet-mapping> </web-app>
Step 4 : (sun-jaxws.xml)
Add following sun-jaxws.xml in WEB-INF folder.
<?xml version="1.0" encoding="UTF-8"?> <endpoints xmlns="http://java.sun.com/xml/ns/jax-ws/ri/runtime" version="2.0"> <endpoint name="Hello" implementation="com.topjavatutorial.jaxws.HelloWorld" url-pattern="/*"/> </endpoints>
Step 5 : (Metro jars)
Download Metro jars from the Metro project website metro.java.net.
The classes WSServletContextListener and WSServlet referred in web.xml are present in Metro jar file webservices-rt.jar
Copy the jar files webservices-rt.jar and webservices-api.jar into your project lib folder.
Here is how the project structure looks like at this point :
Step 6 : (Deploy and Test)
Deploy the project on Tomcat server.
Once the webservice is deployed, you can verify the service status using following url in browser:
http://localhost:8080/JAXWSHelloWorld/jaxws
The WSDL can be accessed using :
http://localhost:8080/JAXWSHelloWorld?wsdl
The xml schema associated with this service can be accessed using :
http://localhost:8080/JAXWSHelloWorld?xsd=1
© 2016, https:. All rights reserved. On republishing this post, you must provide link to original post