在web项目中发布jaxws

概述


在web项目中发布基于jaxws的webservice。
参考文章说,如果不是servlet3.0及以上,需要配置servlet,请注意。
 
servlet3.0的web.xml的框架:
<?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"
         xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
         xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
         id="WebApp_ID" version="3.0" metadata-complete="false">
    <display-name>Archetype Created Web Application</display-name>
</web-app>
Maven依赖

<!-- JAXWS-RI -->
<dependency>
    <groupId>com.sun.xml.ws</groupId>
    <artifactId>jaxws-rt</artifactId>
    <version>2.2.10</version>
</dependency>
依赖分析结果:
[INFO] - com.sun.xml.ws:jaxws-rt:jar:2.2.10:compile
[INFO]    +- javax.xml.bind:jaxb-api:jar:2.2.12-b140109.1041:compile
[INFO]    +- javax.xml.ws:jaxws-api:jar:2.2.11:compile
[INFO]    +- javax.xml.soap:javax.xml.soap-api:jar:1.3.7:compile
[INFO]    +- javax.annotation:javax.annotation-api:jar:1.2:compile
[INFO]    +- javax.jws:jsr181-api:jar:1.0-MR1:compile
[INFO]    +- com.sun.xml.bind:jaxb-core:jar:2.2.10-b140802.1033:compile
[INFO]    +- com.sun.xml.bind:jaxb-impl:jar:2.2.10-b140802.1033:compile
[INFO]    +- com.sun.xml.ws:policy:jar:2.4:compile
[INFO]    +- org.glassfish.gmbal:gmbal-api-only:jar:3.1.0-b001:compile
[INFO]    |  - org.glassfish.external:management-api:jar:3.0.0-b012:compile
[INFO]    +- org.jvnet.staxex:stax-ex:jar:1.7.7:compile
[INFO]    +- com.sun.xml.stream.buffer:streambuffer:jar:1.5.3:compile
[INFO]    +- org.jvnet.mimepull:mimepull:jar:1.9.4:compile
[INFO]    +- com.sun.xml.fastinfoset:FastInfoset:jar:1.2.13:compile
[INFO]    +- org.glassfish.ha:ha-api:jar:3.1.9:compile
[INFO]    +- com.sun.xml.messaging.saaj:saaj-impl:jar:1.3.25:compile
[INFO]    +- org.codehaus.woodstox:woodstox-core-asl:jar:4.2.0:runtime
[INFO]    +- org.codehaus.woodstox:stax2-api:jar:3.1.1:runtime
[INFO]    - com.sun.org.apache.xml.internal:resolver:jar:20050927:compile
不使用Maven,需要人工导入上述jar包;可以尝试在http://mvnrepository.com/中获取。

服务实现


假设你已经清楚如何使用jaxws实现webservice,这里仅提供简略的说明和代码。
这里写一个简单的服务,返回“hello, jaxws!”字符串。

服务接口

IHelloJAXWSService.java
package cn.ljl.baseframe.test.jaxws;
import javax.jws.WebResult;
import javax.jws.WebService;
@WebService
public interface IHelloJAXWSService {
    @WebResult(name = "helloWorld")
    public String getHelloWord();
}

服务实现类

HelloJAXWSServiceImpl.java
package cn.ljl.baseframe.test.jaxws;
import javax.jws.WebService;
@WebService(endpointInterface = "cn.ljl.baseframe.test.jaxws.IHelloJAXWSService")
public class HelloJAXWSServiceImpl implements IHelloJAXWSService {
    @Override
    public String getHelloWord() {
        String helloWorld = "hello, jaxws!";
        return helloWorld;
    }
}

配置endpoints


在WEB-INF目录下,添加sun-jaxws.xml文件,文件内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<endpoints xmlns="http://java.sun.com/xml/ns/jax-ws/ri/runtime"
    version="2.0">
    <!-- 服务路径http://网站路径/services/hello -->
    <endpoint name="hello"
        implementation="cn.ljl.baseframe.test.jaxws.HelloJAXWSServiceImpl"
        url-pattern="/services/hello" />
</endpoints>
注意:网上多个文章都要求这个文件是UTF-8编码,没有测试是否必须。

测试






原文地址:https://www.cnblogs.com/ywjy/p/5196118.html