The Service
- Create a dynamic web project in Eclipse
- Create the Web Service End point
import javax.jws.WebMethod;
import javax.jws.WebService;
@WebService
public class JaxWsService {
@WebMethod
public String sayHello(String message) {
System.out.println("sayHello:" + message);
return "You said '" + message + "'";
}
}JaxWsService.java - @WebService annotation is used to denote a Web Service End point.
- Alternatively, you can have a Service End Point Interface (SEI). An SEI declares methods that can be invoked by clients.
- Generate artifacts required for deploying, using wsgen: The wsgen.bat file is located in the GLASSFISH_HOME/bin directory. The following command can be used to create the artifacts
C:\workspaces\WebServices\JaxWsTest\src>wsgen -classpath ../build/classes/ -wsdl -s . ws.JaxWsService
You can see that I ran wsgen from my src directory (the project was created in eclipse). - Export the WAR file and deploy on Glassfish. To verify, go to the glassfish administrative console -> Web Services, you should see a Web service installed by the name JaxWsService. You might get an exception as shown below
Error loading deployment descriptors for module [WebServicesEAR] -- (class: com/sun/xml/ws/modeler/RuntimeModeler, method: processRpcMethod signature: (Lcom/sun/xml/ws/model/JavaMethod;Ljava/lang/String;Ljavax/jws/WebMethod;Ljava/lang/String;Ljava/lang/reflect/Method;Ljavax/jws/WebService;)V) Incompatible argument to funct
This is due to JAXWS/JWSDP binaries in the classpath. Remove any WS jar files (appserv-ws.jar, webservices-tools.jar etc.) from your class path.
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" version="2.5" 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"> <display-name>JaxWsTest</display-name>
<welcome-file-list>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
For this example I used a web application as a client. A servlet that invokes the actual Web Service client, and the Web Service client class which uses invokes the web method.
- Create a dynamic web application in Eclipse.
- Generate the required artifacts using wsimport command. The command I used is shown below.
C:\workspaces\WebServices\JaxWsClientWeb\src>wsimport -s . http://localhost:8080/JaxWsTest/JaxWsServiceService?wsdl
- Write the Servlet: The servlet uses the @WebServiceRef annotation, so that the server can inject the Web service reference into the servlet. The following is the code for the servlet.
import java.io.IOException;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.ws.WebServiceRef;
import ws.JaxWsServiceService;
import client.JaxWsClient;
public class JaxWsClientServlet extends javax.servlet.http.HttpServlet implements javax.servlet.Servlet {
@WebServiceRef(wsdlLocation = "http://localhost:8080/JaxWsTest/JaxWsServiceService?wsdl")
JaxWsServiceService service;
public JaxWsClientServlet() {
super();
}
protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
JaxWsClient client = new JaxWsClient(service);
response.getWriter().println(client.callService());
response.getWriter().close();
}
}JaxWsClientServlet.java - Write the client class: The client class is a simple Java class, which uses the Service references generated by the wsimport command to invoke the Web Service. Following is the code for the client class.
import ws.JaxWsService;
import ws.JaxWsServiceService;
public class JaxWsClient {
JaxWsServiceService service;
public JaxWsClient(JaxWsServiceService service) {
this.service = service;
}
public String callService() {
JaxWsService port = service.getJaxWsServicePort();
String result = port.sayHello("Hello");
return result;
}
}JaxWsClient - The Web Deployment Descriptor
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://java.sun.com/xml/ns/javaee" version="2.5" 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">
<display-name>JaxWsTest</display-name>
<display-name>JaxWsClientWeb</display-name>
<servlet>
<description></description>
<display-name>clientServlet</display-name>
<servlet-name>clientServlet</servlet-name>
<servlet-class>servlets.JaxWsClientServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>clientServlet</servlet-name>
<url-pattern>/clientServlet</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>web.xml - Export WAR file and deploy. To test go to
http://localhost:8080/JaxWsClientWeb/clientServlet
No comments:
Post a Comment