附wsdl2java用法:java
wsdl2java -p com -d D:\\src -all xx.wsdlweb
-p 指定其wsdl的命名空間,也就是要生成代碼的包名:apache
-d 指定要產生代碼所在目錄服務器
-client 生成客戶端測試web service的代碼app
-server 生成服務器啓動web service的代碼框架
-impl 生成web service的實現代碼frontend
-ant 生成build.xml文件測試
-all 生成全部開始端點代碼:types,service proxy,,service interface, server mainline, client mainline, implementation object, and an Ant build.xml file.ui
-------------------------------------------------------------------------------------------------------------this
package com.haid.webservice.client.bpm; import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement; import javax.xml.bind.annotation.XmlType; @XmlAccessorType(XmlAccessType.FIELD) @XmlType(name = "", propOrder = { "workerCode", "workerName" }) @XmlRootElement(name = "GetOTUser") public class GetOTUser { @XmlElement(name = "WorkerCode") protected String workerCode; @XmlElement(name = "WorkerName") protected String workerName; public String getWorkerCode() { return workerCode; } public void setWorkerCode(String value) { this.workerCode = value; } public String getWorkerName() { return workerName; } public void setWorkerName(String value) { this.workerName = value; } }
package com.haid.webservice.client.bpm; import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement; import javax.xml.bind.annotation.XmlType; @XmlAccessorType(XmlAccessType.FIELD) @XmlType(name = "", propOrder = { "getOTUserResult" }) @XmlRootElement(name = "GetOTUserResponse") public class GetOTUserResponse { @XmlElement(name = "GetOTUserResult") protected String getOTUserResult; public String getGetOTUserResult() { return getOTUserResult; } public void setGetOTUserResult(String value) { this.getOTUserResult = value; } }
package com.haid.webservice.client.bpm; import javax.xml.bind.JAXBElement; import javax.xml.bind.annotation.XmlElementDecl; import javax.xml.bind.annotation.XmlRegistry; import javax.xml.namespace.QName; @XmlRegistry public class ObjectFactory { private final static QName _String_QNAME = new QName("http://tempuri.org/", "string"); private final static QName _Boolean_QNAME = new QName("http://tempuri.org/", "boolean"); private final static QName _ArrayOfStaffInfo_QNAME = new QName("http://tempuri.org/", "ArrayOfStaffInfo"); classes for package: com.haid.webservice.client.bpm public ObjectFactory() { } public GetOTUser createGetOTUser() { return new GetOTUser(); } public GetOTUserResponse createGetOTUserResponse() { return new GetOTUserResponse(); } @XmlElementDecl(namespace = "http://tempuri.org/", name = "string") public JAXBElement<String> createString(String value) { return new JAXBElement<String>(_String_QNAME, String.class, null, value); } @XmlElementDecl(namespace = "http://tempuri.org/", name = "boolean") public JAXBElement<Boolean> createBoolean(Boolean value) { return new JAXBElement<Boolean>(_Boolean_QNAME, Boolean.class, null, value); } }
這個是用於封裝xml的報文請求消息。改動這裏,會改變xml的結構。下面將會測試具體的例子。
package com.haid.webservice.client.bpm; import javax.jws.WebMethod; import javax.jws.WebParam; import javax.jws.WebResult; import javax.jws.WebService; import javax.xml.bind.annotation.XmlSeeAlso; import javax.xml.ws.RequestWrapper; import javax.xml.ws.ResponseWrapper; /** * This class was generated by Apache CXF 3.0.15 * 2017-10-10T10:59:08.882+08:00 * Generated source version: 3.0.15 * */ @WebService(targetNamespace = "http://tempuri.org/", name = "EffortServiceSoap") @XmlSeeAlso({ObjectFactory.class}) public interface EffortServiceSoap { @WebResult(name = "GetOTUserResult", targetNamespace = "http://tempuri.org/") @RequestWrapper(localName = "GetOTUser", targetNamespace = "http://tempuri.org/", className = "com.haid.webservice.client.bpm.GetOTUser") @WebMethod(operationName = "GetOTUser", action = "http://tempuri.org/GetOTUser") className = "com.haid.webservice.client.bpm.GetOTUserResponse") @ResponseWrapper(localName = "GetOTUserResponse", targetNamespace = "http://tempuri.org/", className = "com.haid.webservice.client.bpm.GetOTUserResponse") public java.lang.String getOTUser( @WebParam(name = "WorkerCode"/*, targetNamespace = "http://tempuri.org/"*/) java.lang.String workerCode, @WebParam(name = "WorkerName"/*, targetNamespace = "http://tempuri.org/"*/) java.lang.String workerName ); }
這個是測試執行的main方法:
package com.haid.webservice.client.bpm; /** * Please modify this class to meet your needs * This class is not complete */ import java.io.File; import java.net.MalformedURLException; import java.net.URL; import javax.xml.namespace.QName; import javax.jws.WebMethod; import javax.jws.WebParam; import javax.jws.WebResult; import javax.jws.WebService; import javax.xml.bind.annotation.XmlSeeAlso; import javax.xml.ws.RequestWrapper; import javax.xml.ws.ResponseWrapper; /** * This class was generated by Apache CXF 3.0.15 * 2017-10-10T10:59:08.824+08:00 * Generated source version: 3.0.15 * */ public final class EffortServiceSoap_EffortServiceSoap_Client { private static final QName SERVICE_NAME = new QName("http://tempuri.org/", "EffortService"); private EffortServiceSoap_EffortServiceSoap_Client() { } public static void main(String args[]) throws java.lang.Exception { URL wsdlURL = EffortService.WSDL_LOCATION; if (args.length > 0 && args[0] != null && !"".equals(args[0])) { File wsdlFile = new File(args[0]); try { if (wsdlFile.exists()) { wsdlURL = wsdlFile.toURI().toURL(); } else { wsdlURL = new URL(args[0]); } } catch (MalformedURLException e) { e.printStackTrace(); } } EffortService ss = new EffortService(wsdlURL, SERVICE_NAME); EffortServiceSoap port = ss.getEffortServiceSoap(); { System.out.println("Invoking getOTUser..."); java.lang.String _getOTUser_workerCode = ""; java.lang.String _getOTUser_workerName = "吳"; java.lang.String _getOTUser__return = port.getOTUser(_getOTUser_workerCode, _getOTUser_workerName); System.out.println("getOTUser.result=" + _getOTUser__return); } System.exit(0); } }
運行main方法,結構是直接報錯了,報錯信息以下:
Caused by: javax.xml.bind.UnmarshalException
- with linked exception:
[com.sun.istack.SAXParseException2; lineNumber: 1; columnNumber: 266; 意外的元素 (uri:"http://tempuri.org/", local:"GetOTUserResult")。所需元素爲<{}GetOTUserResult>]
at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.handleStreamException(UnmarshallerImpl.java:483)
at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:417)
at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal(UnmarshallerImpl.java:394)
at org.apache.cxf.jaxb.JAXBEncoderDecoder.doUnmarshal(JAXBEncoderDecoder.java:843)
at org.apache.cxf.jaxb.JAXBEncoderDecoder.access$100(JAXBEncoderDecoder.java:102)
at org.apache.cxf.jaxb.JAXBEncoderDecoder$2.run(JAXBEncoderDecoder.java:871)
at java.security.AccessController.doPrivileged(Native Method)
at org.apache.cxf.jaxb.JAXBEncoderDecoder.unmarshall(JAXBEncoderDecoder.java:869)
at org.apache.cxf.jaxb.JAXBEncoderDecoder.unmarshall(JAXBEncoderDecoder.java:703)
at org.apache.cxf.jaxb.io.DataReaderImpl.read(DataReaderImpl.java:160)
at org.apache.cxf.wsdl.interceptors.DocLiteralInInterceptor.handleMessage(DocLiteralInInterceptor.java:108)
at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:307)
at org.apache.cxf.endpoint.ClientImpl.onMessage(ClientImpl.java:798)
at org.apache.cxf.transport.http.HTTPConduit$WrappedOutputStream.handleResponseInternal(HTTPConduit.java:1638)
at org.apache.cxf.transport.http.HTTPConduit$WrappedOutputStream.handleResponse(HTTPConduit.java:1527)
at org.apache.cxf.transport.http.HTTPConduit$WrappedOutputStream.close(HTTPConduit.java:1330)
at org.apache.cxf.transport.AbstractConduit.close(AbstractConduit.java:56)
at org.apache.cxf.transport.http.HTTPConduit.close(HTTPConduit.java:638)
at org.apache.cxf.interceptor.MessageSenderInterceptor$MessageSenderEndingInterceptor.handleMessage(MessageSenderInterceptor.java:62)
at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:307)
at org.apache.cxf.endpoint.ClientImpl.doInvoke(ClientImpl.java:514)
at org.apache.cxf.endpoint.ClientImpl.invoke(ClientImpl.java:423)
at org.apache.cxf.endpoint.ClientImpl.invoke(ClientImpl.java:326)
at org.apache.cxf.endpoint.ClientImpl.invoke(ClientImpl.java:279)
at org.apache.cxf.frontend.ClientProxy.invokeSync(ClientProxy.java:96)
at org.apache.cxf.jaxws.JaxWsClientProxy.invoke(JaxWsClientProxy.java:138)
... 2 more
Caused by: com.sun.istack.SAXParseException2; lineNumber: 1; columnNumber: 266; 意外的元素 (uri:"http://tempuri.org/", local:"GetOTUserResult")。所需元素爲<{}GetOTUserResult>
at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext.handleEvent(UnmarshallingContext.java:693)
at com.sun.xml.bind.v2.runtime.unmarshaller.Loader.reportError(Loader.java:262)
at com.sun.xml.bind.v2.runtime.unmarshaller.Loader.reportError(Loader.java:257)
at com.sun.xml.bind.v2.runtime.unmarshaller.Loader.reportUnexpectedChildElement(Loader.java:124)
at com.sun.xml.bind.v2.runtime.unmarshaller.Loader.childElement(Loader.java:105)
at com.sun.xml.bind.v2.runtime.unmarshaller.StructureLoader.childElement(StructureLoader.java:262)
at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext._startElement(UnmarshallingContext.java:525)
at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallingContext.startElement(UnmarshallingContext.java:507)
at com.sun.xml.bind.v2.runtime.unmarshaller.StAXStreamConnector.handleStartElement(StAXStreamConnector.java:246)
at com.sun.xml.bind.v2.runtime.unmarshaller.StAXStreamConnector.bridge(StAXStreamConnector.java:180)
at com.sun.xml.bind.v2.runtime.unmarshaller.UnmarshallerImpl.unmarshal0(UnmarshallerImpl.java:415)
... 26 more
Caused by: javax.xml.bind.UnmarshalException: 意外的元素 (uri:"http://tempuri.org/", local:"GetOTUserResult")。所需元素爲<{}GetOTUserResult>
... 37 more
其實,我也不知道是啥緣由致使的,我就是在不停的嘗試修改EffortServiceSoap 類的配置信息。可以正確獲取服務器數據的配置是以下樣子:
package com.haid.webservice.client.bpm; import javax.jws.WebMethod; import javax.jws.WebParam; import javax.jws.WebResult; import javax.jws.WebService; import javax.xml.bind.annotation.XmlSeeAlso; import javax.xml.ws.RequestWrapper; import javax.xml.ws.ResponseWrapper; /** * This class was generated by Apache CXF 3.0.15 * 2017-10-10T10:59:08.882+08:00 * Generated source version: 3.0.15 * */ @WebService(targetNamespace = "http://tempuri.org/", name = "EffortServiceSoap") @XmlSeeAlso({ObjectFactory.class}) public interface EffortServiceSoap { @WebResult(name = "GetOTUserResult", targetNamespace = "http://tempuri.org/") @WebMethod(operationName = "GetOTUser", action = "http://tempuri.org/GetOTUser") className = "com.haid.webservice.client.bpm.GetOTUserResponse") public java.lang.String getOTUser( @WebParam(name = "WorkerCode", targetNamespace = "http://tempuri.org/") java.lang.String workerCode, @WebParam(name = "WorkerName", targetNamespace = "http://tempuri.org/") java.lang.String workerName ); }
報文信息以下:
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> <soap:Body> <ns2:GetOTUser xmlns:ns2="http://tempuri.org/"> <ns2:WorkerCode></ns2:WorkerCode> <ns2:WorkerName>吳</ns2:WorkerName> </ns2:GetOTUser> </soap:Body> </soap:Envelope>
若是我改爲這樣子:
package com.haid.webservice.client.bpm; import javax.jws.WebMethod; import javax.jws.WebParam; import javax.jws.WebResult; import javax.jws.WebService; import javax.xml.bind.annotation.XmlSeeAlso; import javax.xml.ws.RequestWrapper; import javax.xml.ws.ResponseWrapper; /** * This class was generated by Apache CXF 3.0.15 * 2017-10-10T10:59:08.882+08:00 * Generated source version: 3.0.15 * */ @WebService(targetNamespace = "http://tempuri.org/", name = "EffortServiceSoap") @XmlSeeAlso({ObjectFactory.class}) public interface EffortServiceSoap { @WebResult(name = "GetOTUserResult", targetNamespace = "http://tempuri.org/") @RequestWrapper(localName = "GetOTUser", targetNamespace = "http://tempuri.org/", className = "com.haid.webservice.client.bpm.GetOTUser") @WebMethod(operationName = "GetOTUser", action = "http://tempuri.org/GetOTUser") className = "com.haid.webservice.client.bpm.GetOTUserResponse") public java.lang.String getOTUser( @WebParam(name = "WorkerCode"/*, targetNamespace = "http://tempuri.org/"*/) java.lang.String workerCode, @WebParam(name = "WorkerName"/*, targetNamespace = "http://tempuri.org/"*/) java.lang.String workerName ); }
服務器那邊是解析不到不到參數的。傳輸的請求報文信息以下:
<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> <soap:Body> <ns2:GetOTUser xmlns:ns2="http://tempuri.org/"> <WorkerCode></WorkerCode> <WorkerName>'吳'</WorkerName> </ns2:GetOTUser> </soap:Body> </soap:Envelope>