Using JAX-WS to publish WebService in Tomcat

JDKWebservice publishing is already built in, but to publish Web services with Web servers such as Tomcat, you need a third-party Web service framework. Axis2 and CXF are the most popular Webservice frameworks at present, these twoA framework has its own advantages, but they are all heavyweight frameworks. JAX-WS RI is a reference implementation of JAX WebService. Compared to Axis2 and CXF, JAX-WS RI is a lightweight framework. Although it’s a lightweight framework, JAX-WS RIIt also provides the function of publishing Webservice in Web server. Official website address https://jax-ws.java.net/. Next, use JAX-WS RI to publish WebService in Tomcat.

Server-side

1、Create a new Maven Web project, add JAX-WS RI reference to the project, and the pom.xml configuration file is as follows:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <project xmlns="http://maven.apache.org/POM/4.0.0"
 3          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4          xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
 5     <modelVersion>4.0.0</modelVersion>
 6 
 7     <groupId>top.jimc</groupId>
 8     <artifactId>wsi</artifactId>
 9     <version>1.0-SNAPSHOT</version>
10     <packaging>war</packaging>
11 
12     <properties>
13         <junit.version>4.12</junit.version>
14         <jaxws-rt.version>2.2.10</jaxws-rt.version>
15     </properties>
16 
17     <dependencies>
18         <!-- junit -->
19         <dependency>
20             <groupId>junit</groupId>
21             <artifactId>junit</artifactId>
22             <version>${junit.version}</version>
23             <scope>test</scope>
24         </dependency>
25 
26         <!-- JAXWS-RI -->
27         <dependency>
28             <groupId>com.sun.xml.ws</groupId>
29             <artifactId>jaxws-rt</artifactId>
30             <version>${jaxws-rt.version}</version>
31         </dependency>
32     </dependencies>
33 
34     <build>
35         <plugins>
36             <!-- Configure plug-ins that control JDK version-->
37             <plugin>
38                 <groupId>org.apache.maven.plugins</groupId>
39                 <artifactId>maven-compiler-plugin</artifactId>
40                 <version>3.7.0</version>
41                 <configuration>
42                     <source>1.8</source>
43                     <target>1.8</target>
44                     <encoding>utf-8</encoding>
45                 </configuration>
46             </plugin>
47         </plugins>
48     </build>
49 
50 </project>

View Code

2、Create service interface:

 1 package top.jimc.wsi.api;
 2 
 3 import top.jimc.wsi.entity.Person;
 4 
 5 import javax.jws.WebService;
 6 import java.util.Date;
 7 
 8 /**
 9  * WebServiceInterface10  * @author Jimc.
11  * @since 2018/8/31.
12  */
13 @WebService(name = "helloWSoap", targetNamespace = "http://wsi.jimc.top/api/hello")
14 public interface HelloWService {
15 
16     /**
17      * Add two integers.18      *
19      * @param x
20      * @param y
21      * @return Added value22      */
23     Integer add(Integer x, Integer y);
24 
25     /**
26      * Return to the current time27      *
28      * @return
29      */
30     Date now();
31 
32     /**
33      * Getting complex types34      * @param name User name35      * @param age User age36      * @return Return to user class37      */
38     Person getPerson(String name, Integer age);
39 
40 }

View Code

3、Complex type (entity) Person used in services:

 1 package top.jimc.wsi.entity;
 2 
 3 import java.io.Serializable;
 4 
 5 /**
 6  * @author Jimc.
 7  * @since 2018/8/31.
 8  */
 9 public class Person implements Serializable {
10     private static final long serialVersionUID = -7211227224542440039L;
11 
12     private String name;
13     private Integer age;
14 
15     public String getName() {
16         return name;
17     }
18     public void setName(String name) {
19         this.name = name;
20     }
21     public Integer getAge() {
22         return age;
23     }
24     public void setAge(Integer age) {
25         this.age = age;
26     }
27 }

View Code

4、Create a service interface implementation class:

 1 package top.jimc.wsi.api.impl;
 2 
 3 import top.jimc.wsi.api.HelloWService;
 4 import top.jimc.wsi.entity.Person;
 5 
 6 import javax.jws.WebService;
 7 import java.util.Date;
 8 
 9 /**
10  * WebServiceInterface implementation11  * @author Jimc.
12  * @since 2018/8/31.
13  */
14 @WebService(endpointInterface = "top.jimc.wsi.api.HelloWService",
15             portName = "HelloWSoap",
16             serviceName = "HelloWService",
17             targetNamespace = "http://wsi.jimc.top/api/hello")
18 public class HelloWServiceImpl implements HelloWService {
19 
20     @Override
21     public Integer add(Integer x, Integer y) {
22         return x + y;
23     }
24 
25     @Override
26     public Date now() {
27         return new Date();
28     }
29 
30     @Override
31     public Person getPerson(String name, Integer age) {
32         Person person = new Person();
33         person.setName(name);
34         person.setAge(age);
35         return person;
36     }
37 }

View Code

5、Create the WebService configuration file sun-jaxws.xml in WEB-INF, where one WebService corresponds to an Endpoint:

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 name="hello" implementation="top.jimc.wsi.api.impl.HelloWServiceImpl" url-pattern="/api/hello"/>
4 </endpoints>

View Code

6、Add WSServlet to web.xml. If the Web project uses Servlet 3, it does not need the following configuration:

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
 3          xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4          xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
 5          version="3.1">
 6 
 7     <!-- Servlet 3.0Or no configuration is required.-->
 8     <servlet>
 9         <servlet-name>jaxws</servlet-name>
10         <servlet-class>com.sun.xml.ws.transport.http.servlet.WSServlet</servlet-class>
11         <load-on-startup>1</load-on-startup>
12     </servlet>
13     <servlet-mapping>
14         <servlet-name>jaxws</servlet-name>
15         <url-pattern>/api/hello</url-pattern>
16     </servlet-mapping>
17 </web-app>

View Code

7、Start Tomcat to see the effect:

Address bar input: http://localhost:8080/api/hello? WSDL

The above image shows that the interface has been successfully released.

Client

1、wsimport.exeDetailed tools:

 1 Usage: wsimport [options] < WSDL_URI> 2   
 3 Among them, [options] includes: 4   -b <path>                 Specify jaxws/jaxb Binding files or attaching patterns 5                             (Every < path> must have its own.b)  
 6   -B<jaxbOption>            Pass this option to the JAXB mode compiler. 7   -catalog <file>           Specifies directory files for parsing external entity references 8                             Support TR9401, XCatalog and OASIS XML directory format. 9   -d <directory>            Specify where to place the generated output files.10   -encoding <encoding>      Specifies the character encoding used in the source file.11   -extension                Allow suppliers to expand - Not by standard12                             Specified function. Extension may be used.13                             Causes the application to be not portable or14                             Unable to interoperate with other implementations.15   -help                     Show help16   -httpproxy:<host>:<port>  Specify the HTTP proxy server (Port default is 8080)  
17   -keep                     Save generated files18   -p <pkg>                  Specify target package19   -quiet                    Hide wsimport output20   -s <directory>            Specify where to place the generated source file.21   -target <version>         Generate code according to a given JAXWS specification version22                             The default value is 2.2, and the accepted values are 2, 2.1 and 2.2.23                             For example, 2 will be JAXWS 2. Code generation compatible code24   -verbose                  Output information about what operations the compiler is executing.25   -version                  Output version information26   -wsdllocation <location>  @WebServiceClient.wsdlLocation value27   -clientjar <jarfile>      Create the jar file of the generated Artifact and28                             Invoke the WSDL metadata required for Web services.29   -generateJWS              Generate stub JWS implementation file30   -implDestDir <directory>  Specify the location to generate the JWS implementation file.31   -implServiceName <name>   The local part of the service name implemented by the generated JWS.32   -implPortName <name>      The local part of the port name of the generated JWS.33   
34 Extension:35   -XadditionalHeaders              The mapping header is not bound to the request or response message and is not bound to the36                                    Java Method parameters37   -Xauthfile                       A document for transmitting authorization information in the following formats:38                                    http://username:password@example.org/stock?wsdl  
39   -Xdebug                          Output debug information40   -Xno-addressing-databinding      Allows binding of W3C EndpointReferenceType to Java41   
42   -Xnocompile                      Not compiling generated Java files43   -XdisableAuthenticator           Disable from JAX-WS RI The validation procedure is used.44                                    Will ignore -Xauthfile Options (if set)45   -XdisableSSLHostnameVerification Disable SSL host name when extracting WSDL46                                    Verification47   
48 Examples:49   wsimport stock.wsdl -b stock.xml -b stock.xjb  
50   wsimport -d generated http://example.org/stock?wsdl  

View Code

2、The Java class is generated and the generated class is added to the corresponding package of the client:

wsimport -encoding utf-8 -p top.jimc.wst -s E:\Code\Projects\wsp\wst\src\main\java http://localhost:8080/api/hello?wsdl

 3、Call interface

 1 import org.junit.Test;
 2 import top.jimc.wst.HelloWService;
 3 import top.jimc.wst.HelloWSoap;
 4 
 5 /**
 6  * @author Jimc.
 7  * @since 2018/8/31.
 8  */
 9 public class WSTest {
10 
11 
12     /**
13      * call14      */
15     @Test
16     public void helloTest() {
17         HelloWService helloWService = new HelloWService();
18         HelloWSoap hello = helloWService.getHelloWSoap();
19         System.out.println(hello.add(8, 9));
20         System.out.println(hello.now());
21         System.out.println(hello.getPerson("John", 22));
22     }
23 }

View Code

 

Sample source download

Leave a Reply

Your email address will not be published. Required fields are marked *