一、通过jdk实现webservice。
发布webservice。
1、jdk提供了@WebService注释,用来标记webservice类
2、对于webservice发布的方法要求非static或者final的。jdk中提供了Endpoint类用来发布webservice
import javax.jws.WebService; @WebService public interface ServerWebInter { public Integer addInteger(Integer a,Integer b); public String sayHello(String name); }
import javax.jws.WebService; import javax.xml.ws.Endpoint; @WebService public class ServerWeb implements ServerWebInter{ public static void main(String[] args) { //参数一位webservice发布的地址 参数二位webservice的具体实现类 Endpoint.publish("http://localhost:8888/webservice/ServerWeb", new ServerWeb()); System.out.println("game,over!"); } public Integer addInteger(Integer a,Integer b){ System.out.println("您都不要看书了,您都悟透了您都!"); return a+b; } public String sayHello(String name){ System.out.println(name); return name+",hello"; } }执行main方法 打开浏览器输入上面发布的地址: http://localhost:8888/webservice/ServerWeb?wsdl 其中?wsdl是后面加上的。能看到下面所示页面即表示webservice发布成功
<!-- Published by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.2.4-b01. --> <!-- Generated by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.2.4-b01. --> <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://server.webservice.ccsu.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://server.webservice.ccsu.com/" name="ServerWebService"> <types> <xsd:schema> <xsd:import namespace="http://server.webservice.ccsu.com/" schemaLocation="http://localhost:8888/webservice/ServerWeb?xsd=1"/> </xsd:schema> </types> <message name="addInteger"> <part name="parameters" element="tns:addInteger"/> </message> <message name="addIntegerResponse"> <part name="parameters" element="tns:addIntegerResponse"/> </message> <message name="sayHello"> <part name="parameters" element="tns:sayHello"/> </message> <message name="sayHelloResponse"> <part name="parameters" element="tns:sayHelloResponse"/> </message> <portType name="ServerWeb"> <operation name="addInteger"> <input wsam:Action="http://server.webservice.ccsu.com/ServerWeb/addIntegerRequest" message="tns:addInteger"/> <output wsam:Action="http://server.webservice.ccsu.com/ServerWeb/addIntegerResponse" message="tns:addIntegerResponse"/> </operation> <operation name="sayHello"> <input wsam:Action="http://server.webservice.ccsu.com/ServerWeb/sayHelloRequest" message="tns:sayHello"/> <output wsam:Action="http://server.webservice.ccsu.com/ServerWeb/sayHelloResponse" message="tns:sayHelloResponse"/> </operation> </portType> <binding name="ServerWebPortBinding" type="tns:ServerWeb"> <soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"/> <operation name="addInteger"> <soap:operation soapAction=""/> <input> <soap:body use="literal"/> </input> <output> <soap:body use="literal"/> </output> </operation> <operation name="sayHello"> <soap:operation soapAction=""/> <input> <soap:body use="literal"/> </input> <output> <soap:body use="literal"/> </output> </operation> </binding> <service name="ServerWebService"> <port name="ServerWebPort" binding="tns:ServerWebPortBinding"> <soap:address location="http://localhost:8888/webservice/ServerWeb"/> </port> </service> </definitions>二、解析wsdl文件
<xsd:schema> <xsd:import namespace="http://server.webservice.ccsu.com/" schemaLocation="http://localhost:8888/webservice/ServerWeb?xsd=1"/> </xsd:schema>
中schemaLocation的地址在文件中打开<message>中的name对应http://localhost:8888/webservice/ServerWeb?xsd=1页面<xs:complexType >中的name 其子节点表明了方法的参数个数与类型
三、通过jdk自带的工具调用jdk
方法一、通过wsimport导入(jdk提供了wsimport命令用来将wsdl文件方向生成java文件)
打开cmd命令,wsimport -s . 服务的地址如http://localhost:8888/webservice/ServerWeb?wsdl 将在刚才目录下生成相对应的Java文件以及相应的包文件夹
<span style="white-space:pre"> </span>/** * 通过wsimport方法调用webservice * */ public static void wsimportTest(){ //1、wsimport生成的wsdl中的<service name="ServerWebService">这个节点 ServerWebService service = new ServerWebService(); //2、通过wsdl中的<service>节点下的<port name="ServerWebPort" binding="tns:ServerWebPortBinding">得到webservice实现类的代理 ServerWeb web = service.getServerWebPort(); //3、调用具体的方法 Integer count = web.addInteger(35, 23); System.out.println(count); String returnStr = web.sayHello("admin"); System.out.println(returnStr); }方法二、通过ajax+js的方式调用
webservice提供了SOAP(Sample Object Access Protocol)用来传输数据,soap通过HTTP的的方式来传输(其只有POST的方式,因为soap需要有请求体 <env:Body >)起通过XML的方式携带数据。
通过myelcipse得到请求soap以及webservice回传的soap的格式
点击1出Web Services Explorer 2处WSDL Page 点击3处在4处输入服务的地址将会看到ServiceWebPortBinding,点击将会显示具体对应的方法。点击方法在右边的Add 中输入参数点击Go按钮在下一个方框中显示resource 点击resource将会看到SOAP Request Envelope以及SOAP Response Envelope的请求格式:
<script > var xhr = new ActiveXObject("Microsoft.XMLHTTP"); function sendSoap(){ //服务地址 var wsUrl = "http://localhost:8888/webservice/ServerWeb"; //请求提 var soap = "<soapenv:Envelope xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/' xmlns:q0='http://server.webservice.ccsu.com/' xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema instance'>"+ "<soapenv:Body><q0:sayHello><arg0>admin</arg0></q0:sayHello></soapenv:Body></soapenv:Envelope>"; //打开连接 xhr.open("POST",wsUrl,true); //重新设置请求头 xhr.setRequestHeader("Content-Type","text/xml; charset=utf-8"); //设置回调函数 xhr.onreadystatechange = _back; //发送请求 xhr.send(soap); } function _back(){ if(xhr.readyState==4){ if(xhr.status==200){ var ret = xhr.responseXML; alert(ret); alert("webservice 调用成功了"); } } } </script>
/**
* 通过URLConnection调用webservice
* @throws MalformedURLException
*
*/
public static void urlConnectionTest() throws Exception{
String soapStr = helloSoapString("admin");
//服务的地址
URL url = new URL("http://localhost:8888/webservice/ServerWeb");
//打开连接
URLConnection conn = url.openConnection();
conn.setUseCaches(false);
conn.setDoInput(true);
conn.setDoOutput(true);
conn.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
// conn.setRequestProperty("Content-Length", Integer.toString(soapStr.length()));
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(conn.getOutputStream()));
writer.write(soapStr);
writer.flush();
writer.close();
InputStream in = conn.getInputStream();
BufferedReader reader = new BufferedReader(new InputStreamReader(in,"utf-8"));
String str = null;
while((str=reader.readLine())!=null){
System.out.println(str);
}
reader.close();
}
public static String helloSoapString(String name){
String soap = "<soapenv:Envelope xmlns:soapenv='http://schemas.xmlsoap.org/soap/envelope/' xmlns:q0='http://server.webservice.ccsu.com/' "
+ "xmlns:xsd='http://www.w3.org/2001/XMLSchema' xmlns:xsi='http://www.w3.org/2001/XMLSchema instance'>"+
"<soapenv:Body><q0:sayHello><arg0>"+name+"</arg0></q0:sayHello>"+
"</soapenv:Body></soapenv:Envelope>";
return soap;
}
方法四、通过客户端编程的方式调用webservice(首先也要先通过wsimport转换java文件)
/** * 通过客户端的方式调用webservice * @throws Exception */ public static void clientTest() throws Exception{ URL wsurl = new URL("http://localhost:8888/webservice/ServerWeb"); //QName 的第一个参数WSDL文档中targetNamespace对应的值 第二个参数<service>的值 Service service = Service.create(wsurl,new QName("http://server.webservice.ccsu.com/","ServerWebService")); //QName 第一个还是名称空间 第二个是<service>下的port 对应服务的借口 ServerWeb web = service.getPort(new QName("http://server.webservice.ccsu.com/","ServerWebPort"),ServerWeb.class); String ret = web.sayHello("admin"); System.out.println(ret); }