概述
在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編碼,沒有測試是否必須。
測試
