本节创建一个Web Services示例:
本文JDK为1.8
(1)服务段代码
在Eclipse中新建一个Java项目,项目名称为webService,最终结构如下图:
创建一个Function类,其代码如下
package com.ws.services;
import javax.jws.WebService;
import javax.xml.ws.Endpoint;
@WebService
public class Function {
// defined the method 'transWords'
public String transWords(String words) {
String res = "";
for (char ch : words.toCharArray()) {
res += ch + "/";
}
return res;
}
public static void main(String[] args) {
// Publish webservice
Endpoint.publish("http://localhost:9990/ws/function", new Function());
System.out.println("Start Publishing... ");
}
}
按照JAVA程序启动之后,控制台打印输出结果:
Start Publishing...
此时,在浏览器上输入:http://localhost:9990/ws/function?wsdl
就可以看到返回内容:
<!--
Published by JAX-WS RI (http://jax-ws.java.net). RI's version is JAX-WS RI 2.2.9-b130926.1035 svn-revision#5f6196f2b90e9460065a4c2f4e30e065b245e51e.
-->
<!--
Generated by JAX-WS RI (http://jax-ws.java.net). RI's version is JAX-WS RI 2.2.9-b130926.1035 svn-revision#5f6196f2b90e9460065a4c2f4e30e065b245e51e.
-->
<definitions xmlns:wsu="http://docs.oasis-open.org/wss/2004/01/oasis-200401-wss-wssecurity-utility-1.0.xsd" xmlns:wsp="http://www.w3.org/ns/ws-policy" xmlns:wsp1_2="http://schemas.xmlsoap.org/ws/2004/09/policy" xmlns:wsam="http://www.w3.org/2007/05/addressing/metadata" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://services.ws.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://services.ws.com/" name="FunctionService">
<types>
<xsd:schema>
<xsd:import namespace="http://services.ws.com/" schemaLocation="http://localhost:9990/ws/function?xsd=1"/>
</xsd:schema>
</types>
<message name="transWords">
<part name="parameters" element="tns:transWords"/>
</message>
<message name="transWordsResponse">
<part name="parameters" element="tns:transWordsResponse"/>
</message>
<portType name="Function">
<operation name="transWords">
<input wsam:Action="http://services.ws.com/Function/transWordsRequest" message="tns:transWords"/>
<output wsam:Action="http://services.ws.com/Function/transWordsResponse" message="tns:transWordsResponse"/>
</operation>
</portType>
<binding name="FunctionPortBinding" type="tns:Function">
<soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/>
<operation name="transWords">
<soap:operation soapAction=""/>
<input>
<soap:body use="literal"/>
</input>
<output>
<soap:body use="literal"/>
</output>
</operation>
</binding>
<service name="FunctionService">
<port name="FunctionPort" binding="tns:FunctionPortBinding">
<soap:address location="http://localhost:9990/ws/function"/>
</port>
</service>
</definitions>
(2) 客户段代码
在Eclipse中新建一个Java项目,项目名为wsClient。其他操作如下,
选择Other,找到Web Service Client,下一步
填入(1)服务器中发步的地址:http://localhost:9990/ws/function?wsdl,Finish
最终的结构如下:
(3)测试用例:
在wsClient下写测试用例,TestFunction类,结构如下
TestFunction类的代码如下:
package com.ws.services;
import java.rmi.RemoteException;
import javax.xml.rpc.ServiceException;
public class TestFunction {
public static void main(String[] args) {
Function fu;
try{
fu=new FunctionServiceLocator().getFunctionPort();
String str=fu.transWords("This is test ws Function.");
System.out.println(str);
}catch (ServiceException e) {
// TODO: handle exception
e.printStackTrace();
}catch (RemoteException e) {
// TODO: handle exception
e.printStackTrace();
}
}
}
执行TestFunction,结果显示如下(图or文字):
执行结果:
六月 27, 2018 10:00:21 下午 org.apache.axis.utils.JavaUtils isAttachmentSupported
警告: Unable to find required classes (javax.activation.DataHandler and javax.mail.internet.MimeMultipart). Attachment support is disabled.
T/h/i/s/ /i/s/ /t/e/s/t/ /w/s/ /F/u/n/c/t/i/o/n/./
感谢参考:https://www.cnblogs.com/AlanLee/archive/2017/06/02/6933073.html