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即可。