一、前言
第一次接觸webservice,從採坑到採坑,算是瞭解了一些,明白了一些;生成webservice的方法有很多種(實現方式有CXF、Axis等等),webservice 是一種基於web的應用程序,可以使用多種編程語言實現。JAVA 程序猿當然選擇最快,最有效的方法。
二、使用IDEA創建webservice客戶端
我認爲通過IDE生成是有效的,我的目的只是能夠生成且調用。目的----方法(適配)
1)準備IDEA
首先生成的webservice客戶端java代碼,你可以單獨創建一個項目,或者把他當時是某個項目裏的一個service。(如果單獨創建一個項目,創建一個簡單的javaproject就能夠使用webservice客戶端)。
我是在我的SpringBoot項目中調用webservice(作爲一個service),所以略過創建項目的過程。在項目中可以創建一個新的包來放生成的webservice客戶端java代碼,然後選中這個包,右擊在idea的菜單中的最下面(有點難找,圖片還被水印擋住了,但這些都不重要)有【webservice】點開有【Generate Java code from WSDl】 ,也就是通過wsdl生成java代碼。如下圖所示:
2) 準備WSDL
這個可以通過地址或者本地WSDL文件,這裏可能還有一個坑,就是IDEA的代理設置。點擊進去以後,按下圖操作(字多懶得打),如圖二所示:
說明一下,這些生成方式大同小異,我的目的只是能夠用,我看了通過cxf生成的和通過普片生成的java code 是差不多一樣的,但是使用CSF需要一些依賴,否則IDEA無法生成,先把依賴站上把。
<!--CXF--!>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-bundle</artifactId>
</dependency>
3)設置
第一個 webservice wsdl url ; 是wsdl 文件的位置,可以是本地電腦或者網絡上的。
本地的選擇瀏覽文件,找到就行,網絡的你可以將地址放在瀏覽器中看能不能打開。
第二個: output path : java代碼的輸出路徑
第三個; package prefix : 包路徑前綴
關於第二個和第三個,在一開始選擇的時候選擇好要生成的位置就可以,會自動配置。
第四個就是選擇的生成方式,圖片中的這種挺方便的。
最後一步點擊Ok,等待生成就好了。地址不合法的,按鈕是灰色的,不可點,基於網絡的WSDL 如果無法生成,檢查IDEA中代理設置(setting中搜索proxy)。最後生成的java代碼如圖三所示:
刪除了packageinfo類 \生成的.wsdl文件和。class文件。然後沒個版本可能生成的代碼又差異,但是最重要的就是 圖片中所示的前三個類。
明天再寫怎麼用和設置超時時間吧,好睏。
把生成的代碼粘上,可以導入自己的包內用,但是需要修改地址, 敏感位置用xxx代替了;
package com.springcloud.xxx.webservice;
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.2.4
* 2018-06-06T09:27:30.288+08:00
* Generated source version: 3.2.4
*這個類裏面那些targetNamespace 是根據實際情況修改的,和類的地址需要對應上,需要根據自己的情況修改
*/
@WebService(targetNamespace = "http://server.xxx.com/", name = "CallService")
@XmlSeeAlso({ObjectFactory.class})
public interface CallService {
@WebMethod
@WebResult(targetNamespace = "")
@RequestWrapper(localName = "invoke", targetNamespace = "http://server.xxx.com/", className = "com.springcloud.webservice.Invoke")
@ResponseWrapper(localName = "invokeResponse", targetNamespace = "http://server.xxx.com", className = "com.springcloud.webservice.InvokeResponse")
public String invoke(
@WebParam(name = "arg0", targetNamespace = "")
String arg0,
@WebParam(name = "arg1", targetNamespace = "")
String arg1,
@WebParam(name = "arg2", targetNamespace = "")
String arg2,
@WebParam(name = "arg3", targetNamespace = "")
String arg3,
@WebParam(name = "arg4", targetNamespace = "")
String arg4
);
}
package com.springcloud.slebbatch.webservice;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import javax.xml.ws.WebEndpoint;
import javax.xml.ws.WebServiceClient;
import javax.xml.ws.WebServiceFeature;
import java.net.MalformedURLException;
import java.net.URL;
/**
* This class was generated by Apache CXF 3.2.4
* 2018-06-06T09:27:30.399+08:00
* Generated source version: 3.2.4
* 這個工廠類中的靜態代碼塊可能會有不同,下篇博客會用到。
*/
@WebServiceClient(name = "CallServiceFactory",
wsdlLocation = "http://110.156.13.57:7080/services/CallService?wsdl",
targetNamespace = "http://server.xxx.com/")
public class CallServiceFactory extends Service {
public static URL WSDL_LOCATION;
public final static QName SERVICE = new QName("http://server.xxx.com/", "CallServiceFactory");
public final static QName CallServicePort = new QName("http://server.xxx.com/", "CallServicePort");
public static void setURL(String wsdl) {
URL url = null;
try {
url = new URL(wsdl);
} catch (MalformedURLException e) {
java.util.logging.Logger.getLogger(CallServiceFactory.class.getName())
.log(java.util.logging.Level.INFO,
"Can not initialize the default wsdl from {0}", "http://110.156.13.57:7080/services/CallService?wsdl");
}
WSDL_LOCATION = url;
}
public CallServiceFactory(URL wsdlLocation) {
super(wsdlLocation, SERVICE);
}
public CallServiceFactory(URL wsdlLocation, QName serviceName) {
super(wsdlLocation, serviceName);
}
public CallServiceFactory() {
super(WSDL_LOCATION, SERVICE);
}
public CallServiceFactory(WebServiceFeature ... features) {
super(WSDL_LOCATION, SERVICE, features);
}
public CallServiceFactory(URL wsdlLocation, WebServiceFeature ... features) {
super(wsdlLocation, SERVICE, features);
}
public CallServiceFactory(URL wsdlLocation, QName serviceName, WebServiceFeature ... features) {
super(wsdlLocation, serviceName, features);
}
/**
*
* @return
* returns CallService
*/
@WebEndpoint(name = "CallServicePort")
public CallService getCallServicePort() {
return super.getPort(CallServicePort, CallService.class);
}
/**
*
* @param features
* A list of {@link WebServiceFeature} to configure on the proxy. Supported features not in the <code>features</code> parameter will have their default values.
* @return
* returns CallService
*/
@WebEndpoint(name = "CallServicePort")
public CallService getCallServicePort(WebServiceFeature... features) {
return super.getPort(CallServicePort, CallService.class, features);
}
}
package com.springcloud.webservice;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlType;
/**
* <p>invoke complex type的 Java 類。
*
* <p>以下模式片段指定包含在此類中的預期內容。
*
* <pre>
* <complexType name="invoke">
* <complexContent>
* <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
* <sequence>
* <element name="arg0" type="{http://www.w3.org/2001/XMLSchema}string" minOccurs="0"/>
* <element name="arg1" type="{http://www.w3.org/2001/XMLSchema}string" minOccurs="0"/>
* <element name="arg2" type="{http://www.w3.org/2001/XMLSchema}string" minOccurs="0"/>
* <element name="arg3" type="{http://www.w3.org/2001/XMLSchema}string" minOccurs="0"/>
* <element name="arg4" type="{http://www.w3.org/2001/XMLSchema}string" minOccurs="0"/>
* </sequence>
* </restriction>
* </complexContent>
* </complexType>
* </pre>
*
*
*/
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "invoke", propOrder = {
"arg0",
"arg1",
"arg2",
"arg3",
"arg4"
})
public class Invoke {
protected String arg0;
protected String arg1;
protected String arg2;
protected String arg3;
protected String arg4;
/**
* 獲取arg0屬性的值。
*
* @return
* possible object is
* {@link String }
*
*/
public String getArg0() {
return arg0;
}
/**
* 設置arg0屬性的值。
*
* @param value
* allowed object is
* {@link String }
*
*/
public void setArg0(String value) {
this.arg0 = value;
}
/**
* 獲取arg1屬性的值。
*
* @return
* possible object is
* {@link String }
*
*/
public String getArg1() {
return arg1;
}
/**
* 設置arg1屬性的值。
*
* @param value
* allowed object is
* {@link String }
*
*/
public void setArg1(String value) {
this.arg1 = value;
}
/**
* 獲取arg2屬性的值。
*
* @return
* possible object is
* {@link String }
*
*/
public String getArg2() {
return arg2;
}
/**
* 設置arg2屬性的值。
*
* @param value
* allowed object is
* {@link String }
*
*/
public void setArg2(String value) {
this.arg2 = value;
}
/**
* 獲取arg3屬性的值。
*
* @return
* possible object is
* {@link String }
*
*/
public String getArg3() {
return arg3;
}
/**
* 設置arg3屬性的值。
*
* @param value
* allowed object is
* {@link String }
*
*/
public void setArg3(String value) {
this.arg3 = value;
}
/**
* 獲取arg4屬性的值。
*
* @return
* possible object is
* {@link String }
*
*/
public String getArg4() {
return arg4;
}
/**
* 設置arg4屬性的值。
*
* @param value
* allowed object is
* {@link String }
*
*/
public void setArg4(String value) {
this.arg4 = value;
}
}
package com.springcloud.webservice;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlType;
/**
* <p>invokeResponse complex type的 Java 類。
*
* <p>以下模式片段指定包含在此類中的預期內容。
*
* <pre>
* <complexType name="invokeResponse">
* <complexContent>
* <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
* <sequence>
* <element name="return" type="{http://www.w3.org/2001/XMLSchema}string" minOccurs="0"/>
* </sequence>
* </restriction>
* </complexContent>
* </complexType>
* </pre>
*
*
*/
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "invokeResponse", propOrder = {
"_return"
})
public class InvokeResponse {
@XmlElement(name = "return")
protected String _return;
/**
* 獲取return屬性的值。
*
* @return
* possible object is
* {@link String }
*
*/
public String getReturn() {
return _return;
}
/**
* 設置return屬性的值。
*
* @param value
* allowed object is
* {@link String }
*
*/
public void setReturn(String value) {
this._return = value;
}
}
package com.springcloud.webservice;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.annotation.XmlElementDecl;
import javax.xml.bind.annotation.XmlRegistry;
import javax.xml.namespace.QName;
/**
* This object contains factory methods for each
* Java content interface and Java element interface
* generated in the com.sinosoft.ws.server.manage package.
* <p>An ObjectFactory allows you to programatically
* construct new instances of the Java representation
* for XML content. The Java representation of XML
* content can consist of schema derived interfaces
* and classes representing the binding of schema
* type definitions, element declarations and model
* groups. Factory methods for each of these are
* provided in this class.
*
*/
@XmlRegistry
public class ObjectFactory {
private final static QName _Invoke_QNAME = new QName("http:/.server.xx.com/", "invoke");
private final static QName _InvokeResponse_QNAME = new QName("http://server.xxx.com/", "invokeResponse");
/**
* Create a new ObjectFactory that can be used to create new instances of schema derived classes for package: com.sinosoft.ws.server.manage
*
*/
public ObjectFactory() {
}
/**
* Create an instance of {@link Invoke }
*
*/
public Invoke createInvoke() {
return new Invoke();
}
/**
* Create an instance of {@link InvokeResponse }
*
*/
public InvokeResponse createInvokeResponse() {
return new InvokeResponse();
}
/**
* Create an instance of {@link JAXBElement }{@code <}{@link Invoke }{@code >}}
*
*/
@XmlElementDecl(namespace = "http://server.xxx.com/", name = "invoke")
public JAXBElement<Invoke> createInvoke(Invoke value) {
return new JAXBElement<Invoke>(_Invoke_QNAME, Invoke.class, null, value);
}
/**
* Create an instance of {@link JAXBElement }{@code <}{@link InvokeResponse }{@code >}}
*
*/
@XmlElementDecl(namespace = "http://manage.server.ws.sinosoft.com/", name = "invokeResponse")
public JAXBElement<InvokeResponse> createInvokeResponse(InvokeResponse value) {
return new JAXBElement<InvokeResponse>(_InvokeResponse_QNAME, InvokeResponse.class, null, value);
}
}