JAX-WS服务端及客户端
Posted 掉尾瓶
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了JAX-WS服务端及客户端相关的知识,希望对你有一定的参考价值。
一、概述
Java API for XML Web Services (JAX-WS)是Java程序设计语言一个用来创建Web服务的API。
在服务器端,用户只需要通过Java语言定义远程调用所需要实现的接口SEI(service endpoint interface),并提供相关的实现,通过调用JAX-WS的服务发布接口就可以将其发布为WebService接口。
在客户端,用户可以通过JAX-WS的API创建一个代理(用本地对象来替代远程的服务)来实现对于远程服务器端的调用。
二、使用jdk的JAX-WS发布服务
1.写服务端的接口
1 package com.webservice.jaxws; 2 3 public interface Hello { 4 public String sayHello(String name); 5 }
2.写服务端的实现(使用注解@WebService)这个必须在实现类写,不然报错(class com.webservice.jaxws.HelloImpl has neither @WebService nor @WebServiceProvider annotation)
1 package com.webservice.jaxws; 2 3 import javax.jws.WebService; 4 @WebService 5 public class HelloImpl implements Hello{ 6 7 @Override 8 public String sayHello(String name) { 9 return "hi, "+name; 10 } 11 12 }
3.使用jdk中Endpoint发布服务
package com.webservice.jaxws; import javax.xml.ws.Endpoint; public class HelloServerPub { public static void main(String[] args) { Hello hello = new HelloImpl(); Endpoint.publish("http://localhost:8080/hello", hello); System.out.println("发布成功!"); } }
4.在浏览器地址栏输入服务发布的地址查看wsdl及schema文件
1 <?xml version="1.0" encoding="UTF-8"?><!-- 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://jaxws.webservice.com/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://jaxws.webservice.com/" name="HelloImplService"> 2 <types> 3 <xsd:schema> 4 <xsd:import namespace="http://jaxws.webservice.com/" schemaLocation="http://localhost:8080/hello?xsd=1"></xsd:import> 5 </xsd:schema> 6 </types> 7 <message name="sayHello"> 8 <part name="parameters" element="tns:sayHello"></part> 9 </message> 10 <message name="sayHelloResponse"> 11 <part name="parameters" element="tns:sayHelloResponse"></part> 12 </message> 13 <portType name="HelloImpl"> 14 <operation name="sayHello"> 15 <input wsam:Action="http://jaxws.webservice.com/HelloImpl/sayHelloRequest" message="tns:sayHello"></input> 16 <output wsam:Action="http://jaxws.webservice.com/HelloImpl/sayHelloResponse" message="tns:sayHelloResponse"></output> 17 </operation> 18 </portType> 19 <binding name="HelloImplPortBinding" type="tns:HelloImpl"> 20 <soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="document"></soap:binding> 21 <operation name="sayHello"> 22 <soap:operation soapAction=""></soap:operation> 23 <input> 24 <soap:body use="literal"></soap:body> 25 </input> 26 <output> 27 <soap:body use="literal"></soap:body> 28 </output> 29 </operation> 30 </binding> 31 <service name="HelloImplService"> 32 <port name="HelloImplPort" binding="tns:HelloImplPortBinding"> 33 <soap:address location="http://localhost:8080/hello"></soap:address> 34 </port> 35 </service> 36 </definitions>
1 <?xml version="1.0" encoding="UTF-8"?><!-- Published by JAX-WS RI at http://jax-ws.dev.java.net. RI‘s version is JAX-WS RI 2.2.4-b01. --><xs:schema xmlns:tns="http://jaxws.webservice.com/" xmlns:xs="http://www.w3.org/2001/XMLSchema" version="1.0" targetNamespace="http://jaxws.webservice.com/"> 2 3 <xs:element name="sayHello" type="tns:sayHello"></xs:element> 4 5 <xs:element name="sayHelloResponse" type="tns:sayHelloResponse"></xs:element> 6 7 <xs:complexType name="sayHello"> 8 <xs:sequence> 9 <xs:element name="arg0" type="xs:string" minOccurs="0"></xs:element> 10 </xs:sequence> 11 </xs:complexType> 12 13 <xs:complexType name="sayHelloResponse"> 14 <xs:sequence> 15 <xs:element name="return" type="xs:string" minOccurs="0"></xs:element> 16 </xs:sequence> 17 </xs:complexType> 18 </xs:schema>
三、客户端
1.根据wsdl生成客户端(打开命令行窗口,切换到src目录,执行"wsimport -keep http://localhost:8080/hello?wsdl"生成客户端代码,如下图所示:)
2、 借助生成的代码编写调用WebService对外提供的方法
wsimport工具帮我们生成了好几个java类,但我们只需要关心HelloImplService类和HelloImpl接口的使用即可
1 package com.webservice; 2 3 import com.webservice.jaxws.HelloImpl; 4 import com.webservice.jaxws.HelloImplService; 5 6 public class ClientTest { 7 public static void main(String[] args) { 8 //创建一个用于产生HelloImpl实例的工厂,HelloImplService类是wsimport工具生成的 9 HelloImplService service = new HelloImplService(); 10 //通过工厂生成HelloImpl一个实例 11 HelloImpl hello = service.getHelloImplPort(); 12 //调用HelloImpl接口的方法 13 String value = hello.sayHello("Tom"); 14 System.out.println(value); 15 } 16 }
打印的结果:hi, Tom
以上是关于JAX-WS服务端及客户端的主要内容,如果未能解决你的问题,请参考以下文章
jq ajax传递json对象到服务端及contentType的用法