Spring 5.X+CXF 3.X 開發SOAP Web Service服務端例項
阿新 • • 發佈:2018-11-21
方式1:使用 jaxws:server
步驟1. 定義返回的Model
POJO的定義, 最好是加上預設建構函式。服務端不加不會出錯, 但是在客戶端呼叫的時候類似的Model定義不加預設建構函式會出錯不加會出錯。
/** * @Title: Demo.java * @Package com.osxm.ws.demo.vo * @Description: TODO * @author oscarchen * @date Oct 29, 2018 2:05:44 PM * @version V1.0 */ package com.osxm.ws.demo.vo; /** * @ClassName: Demo * @Description: TODO * @author oscarchen */ public class Demo { private String name; private String description; public Demo() { } public Demo(String name, String description) { this.name = name; this.description = description; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getDescription() { return description; } public void setDescription(String description) { this.description = description; } }
步驟2. 定義服務介面
需要加上@WebService 註解
package com.osxm.ws.demo.service;
import javax.jws.WebService;
/**
* @ClassName: DemoSoapWsService
* @Description: TODO
* @author oscarchen
*/
@WebService
public interface DemoSoapWsService {
public Demo get();
}
步驟3. 實現服務方法
/** * @Title: DemoSoapWsServiceImpl.java * @Package com.osxm.ws.demo.service.impl * @Description: TODO * @author oscarchen * @date Nov 14, 2018 10:36:40 AM * @version V1.0 */ package com.osxm.ws.demo.service.impl; import com.osxm.ws.demo.service.DemoSoapWsService; import com.osxm.ws.demo.vo.Demo; /** * @ClassName: DemoSoapWsServiceImpl * @Description: TODO * @author oscarchen */ public class DemoSoapWsServiceImpl implements DemoSoapWsService { @Override public Demo get() { Demo demo = new Demo("SOAP Ws Demo", "This is Demo for SOAP Webservice"); return demo; } }
步驟4. 在Spring的配置檔案配置服務
<jaxws:server id="demoSoapWsService" serviceClass="com.osxm.ws.demo.service.impl.DemoSoapWsServiceImpl" address="/demosoapws" />
步驟5. 訪問測試地址:
http://localhost:8080/webservice/demosoapws?wsdl
使用SOAPUI 測試方法, 正常返回, 但是會報錯誤:
Nov 14, 2018 1:56:22 PM org.apache.cxf.jaxb.io.DataReaderImpl createUnmarshaller
INFO: PropertyException setting Marshaller properties
javax.xml.bind.PropertyException: name: com.sun.xml.bind.ObjectFactory value: [email protected]
at javax.xml.bind.helpers.AbstractUnmarshallerImpl.setProperty(Unknown Source)
at com.sun.xml.internal.bind.v2.runtime.unmarshaller.UnmarshallerImpl.setProperty(Unknown Source)
at org.apache.cxf.jaxb.io.DataReaderImpl.createUnmarshaller(DataReaderImpl.java:124)
at org.apache.cxf.jaxb.io.DataReaderImpl.read(DataReaderImpl.java:170)
at org.apache.cxf.wsdl.interceptors.DocLiteralInInterceptor.handleMessage(DocLiteralInInterceptor.java:109)
at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:308)
at org.apache.cxf.transport.ChainInitiationObserver.onMessage(ChainInitiationObserver.java:121)
at org.apache.cxf.transport.http.AbstractHTTPDestination.invoke(AbstractHTTPDestination.java:267)
at org.apache.cxf.transport.servlet.ServletController.invokeDestination(ServletController.java:234)
at org.apache.cxf.transport.servlet.ServletController.invoke(ServletController.java:208)
at org.apache.cxf.transport.servlet.ServletController.invoke(ServletController.java:160)
at org.apache.cxf.transport.servlet.CXFNonSpringServlet.invoke(CXFNonSpringServlet.java:216)
at org.apache.cxf.transport.servlet.AbstractHTTPServlet.handleRequest(AbstractHTTPServlet.java:301)
at org.apache.cxf.transport.servlet.AbstractHTTPServlet.doPost(AbstractHTTPServlet.java:220)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:660)
at org.apache.cxf.transport.servlet.AbstractHTTPServlet.service(AbstractHTTPServlet.java:276)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:231)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:193)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:166)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:199)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:494)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:139)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:651)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:343)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:412)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:754)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1385)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Unknown Source)
以上錯誤com.sun.xml.bind.ObjectFactory , 看起來是xml設定屬性時錯誤。
查詢com.sun.xml.bind, 在pom.xml中匯入:
<dependency>
<groupId>com.sun.xml.bind</groupId>
<artifactId>jaxb-impl</artifactId>
<version>2.3.1</version>
</dependency>
匯入之後, 還會出現istack和TxwException包缺少的錯誤
Caused by: java.lang.NoClassDefFoundError: com/sun/xml/txw2/TxwException
at com.sun.xml.bind.v2.schemagen.XmlSchemaGenerator.getNamespace(XmlSchemaGenerator.java:207)
at com.sun.xml.bind.v2.schemagen.XmlSchemaGenerator.add(XmlSchemaGenerator.java:230)
at com.sun.xml.bind.v2.schemagen.XmlSchemaGenerator.<init>(XmlSchemaGenerator.java:195)
at com.sun.xml.bind.v2.runtime.JAXBContextImpl.createSchemaGenerator(JAXBContextImpl.java:858)
at com.sun.xml.bind.v2.runtime.JAXBContextImpl.generateSchema(JAXBContextImpl.java:820)
at org.apache.cxf.common.jaxb.JAXBUtils.generateJaxbSchemas(JAXBUtils.java:824)
at org.apache.cxf.jaxb.JAXBDataBinding.generateJaxbSchemas(JAXBDataBinding.java:473)
匯入以下兩個包:
<dependency>
<groupId>com.sun.istack</groupId>
<artifactId>istack-commons-runtime</artifactId>
<version>3.0.7</version>
</dependency>
<dependency>
<groupId>com.sun.xml.txw2</groupId>
<artifactId>txw2</artifactId>
<version>20110809</version>
</dependency>
再測試, 沒有那個錯誤了。
為什麼呢?為什麼jaxb-impl匯入之前不報類找不到的錯誤, 而是屬性設定錯誤?
原因應該是是使用了舊的版本,
看一下mavan本地庫
的確是有好幾個版本, 但使用mavan的還是Tomcat或者eclipse的庫就還需要驗證了。
另外, 一般狀況Spring配置檔案會匯入cxf 預設的xml,這是cxf預設定義的bean。不使用的話可以不用匯入
方式2:
其他基本相同,只是在配置的時候使用jaxws:endpoint
<jaxws:endpoint id="demoSoapWsEndPoint" implementor="com.osxm.ws.demo.service.impl.DemoSoapWsServiceImpl" address="/demoSoapWsEndPoint" />