webservice 教程學習系列(五)——使用JDK開發webservice
1.開發伺服器端程式碼
webservice程式碼
1.建立介面
package ws;
import javax.jws.WebMethod;
import javax.jws.WebService;
/**
* SEI
* @author Administrator
*
*/
@WebService
public interface HelloWS {
/**
* 介面方法
* @param name
* @return
*/
@WebMethod
public String sayHello(String name);
}
2.建立介面實現類
package ws;
import javax.jws.WebService;
/**
* SEI實現類
* @author Administrator
*
*/
@WebService
public class HelloWSImpl implements HelloWS {
@Override
public String sayHello(String name) {
System.out.println("server sayHello"+name);
return "hello "+name;
}
}
3.終端endpoint釋出webservice
package ws; import javax.xml.ws.Endpoint; /** * 釋出webservice * endpoint * @author Administrator * */ public class HelloWSTest { public static void main(String[] args) { String address="http://192.168.43.220:8989/ws_test/zq"; Endpoint.publish(address, new HelloWSImpl()); System.out.println("釋出webservice成功!"); } }
按F11 執行終端,會發現執行成功,並且程序執行成功後未關閉,一直處於啟動狀態,說明色日女啟動之後一直處於啟動狀態,等待被連線。
2.開發客戶端程式碼
獲取webservice server的約束
JDK自帶的轉換工具可以實現:在本地JDK路徑:C:\Program Files\Java\jdk1.8.0_11\bin下有一個wsimport.exe,這個工具可以實現。
新建一個java project 為ws_client。在src處右鍵,在系統瀏覽器中開啟,進入sr路徑,按住shift,滑鼠右鍵,選擇“在此處開啟命令視窗”,進入dos視窗。
輸入命令:wsimport -keep http://192.168.43.220:8989/ws_test/zq?wsdl
即可將伺服器端的約束bean直接匯入進本專案工程的src目錄下。
回到eclipse的工程中,右鍵重新整理,即可看到已經匯入的bean.
其實在瀏覽器端輸入webservice的url也可以比較直觀的看到約束檔案:http://192.168.43.220:8989/ws_test/zq?wsdl
回到eclipse,看一下生成的檔案。找到根檔案。可以根據<service name="HelloWSImplService"> 得知根檔案是HelloWSImplService。ctrl+shift+T 輸入檔名,可以直接開啟給java檔案。
6份檔案原始碼如下所示:
1.HelloWSImpl
package ws;
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.Action;
import javax.xml.ws.RequestWrapper;
import javax.xml.ws.ResponseWrapper;
/**
* This class was generated by the JAX-WS RI.
* JAX-WS RI 2.2.9-b130926.1035
* Generated source version: 2.2
*
*/
@WebService(name = "HelloWSImpl", targetNamespace = "http://ws/")
@XmlSeeAlso({
ObjectFactory.class
})
public interface HelloWSImpl {
/**
*
* @param arg0
* @return
* returns java.lang.String
*/
@WebMethod
@WebResult(targetNamespace = "")
@RequestWrapper(localName = "sayHello", targetNamespace = "http://ws/", className = "ws.SayHello")
@ResponseWrapper(localName = "sayHelloResponse", targetNamespace = "http://ws/", className = "ws.SayHelloResponse")
@Action(input = "http://ws/HelloWSImpl/sayHelloRequest", output = "http://ws/HelloWSImpl/sayHelloResponse")
public String sayHello(
@WebParam(name = "arg0", targetNamespace = "")
String arg0);
}
2.HelloWSImplService
package ws;
import java.net.MalformedURLException;
import java.net.URL;
import javax.xml.namespace.QName;
import javax.xml.ws.Service;
import javax.xml.ws.WebEndpoint;
import javax.xml.ws.WebServiceClient;
import javax.xml.ws.WebServiceException;
import javax.xml.ws.WebServiceFeature;
/**
* This class was generated by the JAX-WS RI.
* JAX-WS RI 2.2.9-b130926.1035
* Generated source version: 2.2
*
*/
@WebServiceClient(name = "HelloWSImplService", targetNamespace = "http://ws/", wsdlLocation = "http://192.168.43.220:8989/ws_test/zq?wsdl")
public class HelloWSImplService
extends Service
{
private final static URL HELLOWSIMPLSERVICE_WSDL_LOCATION;
private final static WebServiceException HELLOWSIMPLSERVICE_EXCEPTION;
private final static QName HELLOWSIMPLSERVICE_QNAME = new QName("http://ws/", "HelloWSImplService");
static {
URL url = null;
WebServiceException e = null;
try {
url = new URL("http://192.168.43.220:8989/ws_test/zq?wsdl");
} catch (MalformedURLException ex) {
e = new WebServiceException(ex);
}
HELLOWSIMPLSERVICE_WSDL_LOCATION = url;
HELLOWSIMPLSERVICE_EXCEPTION = e;
}
public HelloWSImplService() {
super(__getWsdlLocation(), HELLOWSIMPLSERVICE_QNAME);
}
public HelloWSImplService(WebServiceFeature... features) {
super(__getWsdlLocation(), HELLOWSIMPLSERVICE_QNAME, features);
}
public HelloWSImplService(URL wsdlLocation) {
super(wsdlLocation, HELLOWSIMPLSERVICE_QNAME);
}
public HelloWSImplService(URL wsdlLocation, WebServiceFeature... features) {
super(wsdlLocation, HELLOWSIMPLSERVICE_QNAME, features);
}
public HelloWSImplService(URL wsdlLocation, QName serviceName) {
super(wsdlLocation, serviceName);
}
public HelloWSImplService(URL wsdlLocation, QName serviceName, WebServiceFeature... features) {
super(wsdlLocation, serviceName, features);
}
/**
*
* @return
* returns HelloWSImpl
*/
@WebEndpoint(name = "HelloWSImplPort")
public HelloWSImpl getHelloWSImplPort() {
return super.getPort(new QName("http://ws/", "HelloWSImplPort"), HelloWSImpl.class);
}
/**
*
* @param features
* A list of {@link javax.xml.ws.WebServiceFeature} to configure on the proxy. Supported features not in the <code>features</code> parameter will have their default values.
* @return
* returns HelloWSImpl
*/
@WebEndpoint(name = "HelloWSImplPort")
public HelloWSImpl getHelloWSImplPort(WebServiceFeature... features) {
return super.getPort(new QName("http://ws/", "HelloWSImplPort"), HelloWSImpl.class, features);
}
private static URL __getWsdlLocation() {
if (HELLOWSIMPLSERVICE_EXCEPTION!= null) {
throw HELLOWSIMPLSERVICE_EXCEPTION;
}
return HELLOWSIMPLSERVICE_WSDL_LOCATION;
}
}
3.ObjectFactory
package ws;
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 ws 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 _SayHello_QNAME = new QName("http://ws/", "sayHello");
private final static QName _SayHelloResponse_QNAME = new QName("http://ws/", "sayHelloResponse");
/**
* Create a new ObjectFactory that can be used to create new instances of schema derived classes for package: ws
*
*/
public ObjectFactory() {
}
/**
* Create an instance of {@link SayHello }
*
*/
public SayHello createSayHello() {
return new SayHello();
}
/**
* Create an instance of {@link SayHelloResponse }
*
*/
public SayHelloResponse createSayHelloResponse() {
return new SayHelloResponse();
}
/**
* Create an instance of {@link JAXBElement }{@code <}{@link SayHello }{@code >}}
*
*/
@XmlElementDecl(namespace = "http://ws/", name = "sayHello")
public JAXBElement<SayHello> createSayHello(SayHello value) {
return new JAXBElement<SayHello>(_SayHello_QNAME, SayHello.class, null, value);
}
/**
* Create an instance of {@link JAXBElement }{@code <}{@link SayHelloResponse }{@code >}}
*
*/
@XmlElementDecl(namespace = "http://ws/", name = "sayHelloResponse")
public JAXBElement<SayHelloResponse> createSayHelloResponse(SayHelloResponse value) {
return new JAXBElement<SayHelloResponse>(_SayHelloResponse_QNAME, SayHelloResponse.class, null, value);
}
}
4.package-info
@javax.xml.bind.annotation.XmlSchema(namespace = "http://ws/")
package ws;
5.SayHello
package ws;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlType;
/**
* <p>sayHello complex type�� Java �ࡣ
*
* <p>����ģʽƬ��ָ�������ڴ����е�Ԥ�����ݡ�
*
* <pre>
* <complexType name="sayHello">
* <complexContent>
* <restriction base="{http://www.w3.org/2001/XMLSchema}anyType">
* <sequence>
* <element name="arg0" type="{http://www.w3.org/2001/XMLSchema}string" minOccurs="0"/>
* </sequence>
* </restriction>
* </complexContent>
* </complexType>
* </pre>
*
*
*/
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "sayHello", propOrder = {
"arg0"
})
public class SayHello {
protected String arg0;
/**
* ��ȡ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;
}
}
6.SayHelloResponse
package ws;
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>sayHelloResponse complex type�� Java �ࡣ
*
* <p>����ģʽƬ��ָ�������ڴ����е�Ԥ�����ݡ�
*
* <pre>
* <complexType name="sayHelloResponse">
* <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 = "sayHelloResponse", propOrder = {
"_return"
})
public class SayHelloResponse {
@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;
}
}
不知道為啥,最後兩個檔案中文亂碼了。我明明設定的整個workspace的編碼格式都是utf-8,知道為什麼的朋友歡迎在下面留言。
最後,我們就可以寫呼叫程式碼了。
package ws.test;
import ws.HelloWSImpl;
import ws.HelloWSImplService;
public class WSClientTest {
public static void main(String[] args) {
HelloWSImplService helloWSImplService=new HelloWSImplService();
HelloWSImpl helloWSImplPort = helloWSImplService.getHelloWSImplPort();
String response = helloWSImplPort.sayHello("zhangqin");
System.out.println("client接收到:"+response);
}
}
執行結果
到這裡,一個簡單的伺服器端和客戶端的程式碼就寫完了。
補充:剛剛突然知道了原來那個生成的bean必須是中文編碼,使用UTF-8就會亂碼,所以將其屬性手動改成gbk即可。