JAX-WS learning 4: deploying to Web containers
Blog type:
- JAX-WS
- Java-related
In the previous introduction, web service is released by calling:
Java code
- Endpoint. publish ()
Method To start a java Embedded web container.
Here we will introduce how to deploy the generated web service to a web container.
Take the example of a calculator as an example. First, we use the wsgen tool to generate all the components required for deployment:
Java code
- Wsgen-wsdl-keep-d ../output-cp. test. CalculatorImpl
When running this command, I got an error message:
Java code
- The @ javax. jws. WebService. name element cannot be used in with @ javax. jws. WebService. endpointInterface element
This means that the name attribute and endpointInterface attribute cannot appear at the same time, so you have to delete the name attribute in CalculatorImpl first and check why this error occurs.
After running the above command line, we will get some new files:
- CalculatorImplService. wsdl
- CalculatorImplService_schema1.xsd
- Add. java
- AddResponse. java
- Multi. java
- MultiResponse. java
Copy them to the project first, where:
- All java files and go to the source code
- Create a new wsdl directory and put the wsdl and xsd files in this directory.
The rest is how to provide a web. xml so that url access can be processed by our web service implementation class.
Download:
Previously, it was implemented based on the built-in jdk api. Below we need to use some lib packages provided by Jax-ws. Therefore, we need to go to the next implementation of Jax-ws. For example, what I currently use 2.2.5 is:
Http://jax-ws.java.net/2.2.5/
Implementation Method:
Sun's Jax-ws implementation provides two classes for configuring web. xml of Web containers to implement class ing from URL to Web Service:
- Listener class: com. sun. xml. ws. transport. http. servlet. WSServletContextListener
- Servlet: com. sun. xml. ws. transport. http. servlet. WSServlet
Here wsservletcontextlistener will go to find another file sun-jaxws.xml in the same level directory as Web. XML, the flow chart of the whole process is about:
The schema of the sun-jaxws.xml file can be found (sun-jaxws.xsd) under the docs directory of the downloaded Jax-ws Ri ).
Here is a direct implementation of calculator web. xml and sun-jaxws.xml:
Sun-jaxws.xml code
- <? Xml version = "1.0" encoding = "UTF-8"?>
- <Endpoints xmlns = "http://java.sun.com/xml/ns/jax-ws/ri/runtime"
- Version = "2.0">
- <Endpoint name = "calculator" implementation = "test. CalculatorImpl"
- Url-pattern = "/calculator"/>
- </Endpoints>
Web. xml Code
- <? Xml version = "1.0" encoding = "UTF-8"?>
- <! DOCTYPE web-app PUBLIC "-// Sun Microsystems,
- Inc. // DTD Web Application 2.3 // EN"
- Http://java.sun.com/j2ee/dtds/web-app_2_3.dtd>
- <Web-app>
- <Listener>
- <Listener-class>
- Com. sun. xml. ws. transport. http. servlet. WSServletContextListener
- </Listener-class>
- </Listener>
- <Servlet>
- <Servlet-name> calculator </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> calculator </servlet-name>
- <Url-pattern>/calculator </url-pattern>
- </Servlet-mapping>
- <Session-config>
- <Session-Time Out> 120 </session-timeout>
- </Session-config>
- </Web-app>
According to the above flowchart, we can clearly understand:
During initialization, wsservletcontextlistener reads the WEB-INF and web. xml under the sun-jaxws.xml directory and binds the Web service implementation class to a URL, Which is indirectly carried out through a wsservlet class. Each time a request is sent, wsservletcontextlistener intercepts the request, finds the implementation of the corresponding web service based on the request URL, and calls other methods.
Dependency package
Obviously, the WSServletContextListener and WSServlet classes are not built by java by default. These classes are in the jar under the lib directory of the downloaded jax-ws RI. Therefore, to successfully run these web services, we also need to add the necessary dependency packages, including:
- Gmbal-api-only.jar
- Ha-api.jar
- Jaxb-impl.jar
- Jaxws-api.jar
- Jaxws-rt.jar
- Management-api.jar
- Policy. jar
- Stax-ex.jar
- Streambuffer. jar
Package
The package structure is as follows:
Reference: Http://www.mkyong.com/webservices/jax-ws/deploy-jax-ws-web-services-on-tomcat/