用JAX-WS在Tomcat中发布WebService

JDK中已经内置了Webservice发布,不过要用Tomcat等Web服务器发布WebService,还需要用第三方Webservice框架。Axis2和CXF是目前最流行的Webservice框架,这两个框架各有优点,不过都属于重量级框架。 JAX-WS RI是JAX WebService参考实现。相对于Axis2和CXF,JAX-WS RI是一个轻量级的框架。虽然是个轻量级框架,JAX-WS RI也提供了在Web服务器中发布Webservice的功能。官网地址https://jax-ws.java.net/。下面用JAX-WS RI在Tomcat中发布WebService。

服务端

1、新建一个Maven Web项目,在项目中添加JAX-WS RI引用,pom.xml配置文件如下:

 <?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion> <groupId>top.jimc</groupId>
<artifactId>wsi</artifactId>
<version>1.0-SNAPSHOT</version>
<packaging>war</packaging> <properties>
<junit.version>4.12</junit.version>
<jaxws-rt.version>2.2.10</jaxws-rt.version>
</properties> <dependencies>
<!-- junit -->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>${junit.version}</version>
<scope>test</scope>
</dependency> <!-- JAXWS-RI -->
<dependency>
<groupId>com.sun.xml.ws</groupId>
<artifactId>jaxws-rt</artifactId>
<version>${jaxws-rt.version}</version>
</dependency>
</dependencies> <build>
<plugins>
<!-- 配置控制jdk版本的插件 -->
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.7.0</version>
<configuration>
<source>1.8</source>
<target>1.8</target>
<encoding>utf-8</encoding>
</configuration>
</plugin>
</plugins>
</build> </project>

2、创建服务接口:

 package top.jimc.wsi.api;

 import top.jimc.wsi.entity.Person;

 import javax.jws.WebService;
import java.util.Date; /**
* WebService接口
* @author Jimc.
* @since 2018/8/31.
*/
@WebService(name = "helloWSoap", targetNamespace = "http://wsi.jimc.top/api/hello")
public interface HelloWService { /**
* 两个整数相加
*
* @param x
* @param y
* @return 相加后的值
*/
Integer add(Integer x, Integer y); /**
* 返回当前时间
*
* @return
*/
Date now(); /**
* 获取复杂类型
* @param name 用户姓名
* @param age 用户年龄
* @return 返回用户类
*/
Person getPerson(String name, Integer age); }

3、服务中用到的复杂类型(实体)Person:

 package top.jimc.wsi.entity;

 import java.io.Serializable;

 /**
* @author Jimc.
* @since 2018/8/31.
*/
public class Person implements Serializable {
private static final long serialVersionUID = -7211227224542440039L; private String name;
private Integer age; public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
}

4、创建服务接口实现类:

 package top.jimc.wsi.api.impl;

 import top.jimc.wsi.api.HelloWService;
import top.jimc.wsi.entity.Person; import javax.jws.WebService;
import java.util.Date; /**
* WebService接口实现
* @author Jimc.
* @since 2018/8/31.
*/
@WebService(endpointInterface = "top.jimc.wsi.api.HelloWService",
portName = "HelloWSoap",
serviceName = "HelloWService",
targetNamespace = "http://wsi.jimc.top/api/hello")
public class HelloWServiceImpl implements HelloWService { @Override
public Integer add(Integer x, Integer y) {
return x + y;
} @Override
public Date now() {
return new Date();
} @Override
public Person getPerson(String name, Integer age) {
Person person = new Person();
person.setName(name);
person.setAge(age);
return person;
}
}

5、在WEB-INF中创建WebService配置文件sun-jaxws.xml,配置文件中一个WebService对应一个Endpoint:

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

6、在web.xml中添加WSServlet,如果Web项目使用Servlet 3.0则不需要以下配置:

 <?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
version="3.1"> <!-- Servlet 3.0或者以上不需要配置 -->
<servlet>
<servlet-name>jaxws</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>jaxws</servlet-name>
<url-pattern>/api/hello</url-pattern>
</servlet-mapping>
</web-app>

7、启动tomcat,查看效果:

地址栏输入:http://localhost:8080/api/hello?wsdl

用JAX-WS在Tomcat中发布WebService

出现上面的画面,说明接口已成功发布。

8、其他webservice服务发布的方式:

除了上面的服务发布方式以外,最常用的发布方式可以通过下面的代码来发布

/**
* 手动发布服务
*/
public static void main(String[] args) {
/*
* 参数1:服务的发布地址
* 参数2:服务的实现者
*/
Endpoint.publish("http://127.0.0.1:8080/api/hello", new HelloWServiceImpl()); }

客户端

1、wsimport.exe工具详解:

 用法: wsimport [options] <WSDL_URI>  

 其中 [options] 包括:
-b <path> 指定 jaxws/jaxb 绑定文件或附加模式
(每个 <path> 都必须具有自己的 -b)
-B<jaxbOption> 将此选项传递给 JAXB 模式编译器
-catalog <file> 指定用于解析外部实体引用的目录文件
支持 TR9401, XCatalog 和 OASIS XML 目录格式。
-d <directory> 指定放置生成的输出文件的位置
-encoding <encoding> 指定源文件所使用的字符编码
-extension 允许供应商扩展 - 不按规范
指定功能。使用扩展可能会
导致应用程序不可移植或
无法与其他实现进行互操作
-help 显示帮助
-httpproxy:<host>:<port> 指定 HTTP 代理服务器 (端口默认为 8080)
-keep 保留生成的文件
-p <pkg> 指定目标程序包
-quiet 隐藏 wsimport 输出
-s <directory> 指定放置生成的源文件的位置
-target <version> 按给定的 JAXWS 规范版本生成代码
默认为 2.2, 接受的值为 2.0, 2.1 和 2.2
例如, 2.0 将为 JAXWS 2.0 规范生成兼容的代码
-verbose 有关编译器在执行什么操作的输出消息
-version 输出版本信息
-wsdllocation <location> @WebServiceClient.wsdlLocation 值
-clientjar <jarfile> 创建生成的 Artifact 的 jar 文件以及
调用 Web 服务所需的 WSDL 元数据。
-generateJWS 生成存根 JWS 实现文件
-implDestDir <directory> 指定生成 JWS 实现文件的位置
-implServiceName <name> 生成的 JWS 实现的服务名的本地部分
-implPortName <name> 生成的 JWS 实现的端口名的本地部分 扩展:
-XadditionalHeaders 映射标头不绑定到请求或响应消息不绑定到
Java 方法参数
-Xauthfile 用于传送以下格式的授权信息的文件:
http://username:password@example.org/stock?wsdl
-Xdebug 输出调试信息
-Xno-addressing-databinding 允许 W3C EndpointReferenceType 到 Java 的绑定 -Xnocompile 不编译生成的 Java 文件
-XdisableAuthenticator 禁用由 JAX-WS RI 使用的验证程序,
将忽略 -Xauthfile 选项 (如果设置)
-XdisableSSLHostnameVerification 在提取 wsdl 时禁用 SSL 主机名
验证 示例:
wsimport stock.wsdl -b stock.xml -b stock.xjb
wsimport -d generated http://example.org/stock?wsdl

2、生成java类,并把生成的类添加到客户端相应的package下:

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

3、调用接口

 import org.junit.Test;
import top.jimc.wst.HelloWService;
import top.jimc.wst.HelloWSoap; /**
* @author Jimc.
* @since 2018/8/31.
*/
public class WSTest { /**
* 调用
*/
@Test
public void helloTest() {
HelloWService helloWService = new HelloWService();
HelloWSoap hello = helloWService.getHelloWSoap();
System.out.println(hello.add(8, 9));
System.out.println(hello.now());
System.out.println(hello.getPerson("John", 22));
}
}

示例源码下载

上一篇:用cocos2d-html5做的消除类游戏《英雄爱消除》(2)——Block设计实现


下一篇:spring+jax 出现java.io.Serializable is an interface, and JAXB can't handle interfaces