基于JAX-WS的Web Service服务端/客户端

来源:互联网 发布:淘宝女包店铺排行榜 编辑:程序博客网 时间:2024/05/20 08:26

JAX-WS简介:

JAX_RPC(Java API for XML-Based RPC)允许Java应用程序可以通过已知的描述信息调用一个基于Java的Web服务,描述信息与Web服务的WSDL描述相一致

JAX-RPC2.0更名为JAX-WS2.0(Java API for XML-Based Web Services)

JAX-WS中,一个远程调用可以转换为一个基于XML的协议,如SOAP。开发者在使用JAX-WS的过程中,不需要编写任何生成、处理SOAP消息的代码,JAX-WS在运行时自动将API的调用转换为相应的SOAP消息

在服务器端,用户只需要通过Java语言定义远程调用所需实现的接口,并提供相应实现,通过调用JAX-WS的服务发布接口即可将其发布为WebService接口

在客户端,用户可以通过JAX-WS的API创建一个代理来实现对于远程服务器端的调用

 

JAX-WS服务端:

JAX-WS服务端采用注释描述WebService,不再依赖WebService描述文件

使用JDK1.6_45(JDK1.5中不包含所需类)

package com.sean.server;import javax.jws.WebMethod;import javax.jws.WebParam;import javax.jws.WebService;@WebServicepublic interface Plus {@WebMethodpublic int plus(@WebParam(name="x") int x,@WebParam(name="y") int y);}
package com.sean.server;import javax.jws.WebMethod;import javax.jws.WebParam;import javax.jws.WebService;@WebServicepublic class PlusImpl implements Plus {@WebMethodpublic int plus(@WebParam(name="x") int x, @WebParam(name="y") int y) {return x + y;}}
package com.sean.server;import javax.xml.ws.Endpoint;public class Server {public static void main(String[] args) {PlusImpl plus = new PlusImpl();String addr = "http://127.0.0.1:8888/Plus";Endpoint.publish(addr, plus);}}

程序启动后,访问http://127.0.0.1:8888/Plus?wsdl即可查看自动生成的WSDL文件

<?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.1.6 in JDK 6. --><!-- Generated by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.1.6 in JDK 6. --><definitions name="PlusImplService" targetNamespace="http://server.sean.com/"xmlns="http://schemas.xmlsoap.org/wsdl/" xmlns:xsd="http://www.w3.org/2001/XMLSchema"xmlns:tns="http://server.sean.com/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"><types><xsd:schema><xsd:import schemaLocation="http://127.0.0.1:8888/Plus?xsd=1"namespace="http://server.sean.com/" /></xsd:schema></types><message name="plus"><part name="parameters" element="tns:plus" /></message><message name="plusResponse"><part name="parameters" element="tns:plusResponse" /></message><portType name="PlusImpl"><operation name="plus"><input message="tns:plus" /><output message="tns:plusResponse" /></operation></portType><binding name="PlusImplPortBinding" type="tns:PlusImpl"><soap:binding style="document"transport="http://schemas.xmlsoap.org/soap/http" /><operation name="plus"><soap:operation soapAction="" /><input><soap:body use="literal" /></input><output><soap:body use="literal" /></output></operation></binding><service name="PlusImplService"><port name="PlusImplPort" binding="tns:PlusImplPortBinding"><soap:address location="http://127.0.0.1:8888/Plus" /></port></service></definitions>

使用 SoapUI5.0.0尝试用上面的WSDL创建WebService服务端报错(org.apache.xmlbeans.XmlException:error:does not close tag.)

使用SoapUI4.5.2则一切正常,只能归咎于不同版本的SoapUI对文件格式校验不同

 

JAX-WS客户端:

package com.sean.client;import java.net.URL;import javax.xml.namespace.QName;import javax.xml.ws.Service;import com.sean.server.Plus;public class Client {public static void main(String[] args) throws Exception {QName serviceName = new QName("http://server.sean.com/", "PlusImplService");QName portName = new QName("http://server.sean.com/", "PlusImplPort");String addr = "http://127.0.0.1:8888/Plus?wsdl";URL url = new URL(addr);Service service = Service.create(url, serviceName);Plus plus = service.getPort(portName,Plus.class);//Plus plus = service.getPort(Plus.class);int result = plus.plus(1, 2);System.out.println("result:" + result);}}

使用Plus plus = service.getPort(Plus.class)方法时,客户端调用时报错:

Exception in thread "main" javax.xml.ws.WebServiceException: Undefined port type: {http://server.sean.com/}Plus

解决方式一:

如客户端示例,将Plus plus = service.getPort(Plus.class)修改为Plus plus = service.getPort(portName,Plus.class)

解决方式二:

修改PlusImpl类的@WebService标记修改为@WebService(endpointInterface="com.sean.server.Plus")

参考:http://stackoverflow.com/questions/13417454/javax-xml-ws-webserviceexception-undefined-port-type-java-struts-soap-wsdl 

 

0 0
原创粉丝点击