轻量级webservice解决方案Jax-ws(集成Spring3)
发布时间:2020-12-16 23:39:12 所属栏目:安全 来源:网络整理
导读:背景: ? ? ? ? 公司一直以来都使用xfire作为webservice通讯组件,它的功能性、稳定性都非常不错,而且通过myeclipse的一些功能生成client也是分分钟的事情,使得开发webservice变得非常便利,这也是我们选择它的原因吧。不过它是一个比较重量级的组件,一旦
背景:? ? ? ? 公司一直以来都使用xfire作为webservice通讯组件,它的功能性、稳定性都非常不错,而且通过myeclipse的一些功能生成client也是分分钟的事情,使得开发webservice变得非常便利,这也是我们选择它的原因吧。不过它是一个比较重量级的组件,一旦使用xfire服务端就需要引用非常多的jar包,客户端同样也需要对xfire的jar包进行引用(客户端可能也可以不需要引用xfire的jar,但是目前公司的实现是这样没有深入研究)。本人一直偏爱轻量级的组件,所以一直在找一个轻量级的webservice组件。目前我知道的webservice组件有 Axis 、CXF 、jax-ws等,其中CXF基本上是目前最为主流的组件,Axis已经有些过时了(没有贬低意思),jax-ws则是sun(oracle)公司自己的实现,也是其中最轻的,完全符合我的胃口 哈哈,那么我就为大家介绍一下如何实现吧。 实现目标:
spring jax-ws - 服务端教程:
<!-- jax-ws --> <dependency> <groupId>org.jvnet.jax-ws-commons.spring</groupId> <artifactId>jaxws-spring</artifactId> <version>1.8</version> <!-- 排除其对spring2.0的依赖 --> <exclusions> <exclusion> <groupId>org.springframework</groupId> artifactId>spring</artifactId> </exclusion> </exclusions> </dependency> <!-- jax-ws :解决 noclass XMLStreamReaderToContentHandler问题(添加jaxws-rt也可以解决noclass问题,但会出现xsd文件异常) --> <dependency> <span style="white-space:pre"> </span><groupId>com.sun.istack</groupId> <span style="white-space:pre"> </span><artifactId>istack-commons-runtime</artifactId> <span style="white-space:pre"> </span><version>2.2.1</version> </dependency>
package com.metecyu.yusr.ws; import javax.jws.WebMethod; import javax.jws.WebParam; import javax.jws.WebService; import javax.jws.soap.SOAPBinding; import com.metecyu.yusr.ws.model.WsUser; import com.metecyu.yusr.ws.model.WsUserList; @WebService(name = "YusrWsServiceItf",targetNamespace = "http://ws.yusr.metecyu.com/") @SOAPBinding(style = SOAPBinding.Style.RPC) public interface YusrWsServiceItf { <span style="white-space:pre"> </span> ? ? /** ? ? ?* 获取用户对象 ? ? ?* @param userid ? ? ?* @return ? ? ?* ? ? returns testjws.client.WsUser ? ? ?*/ ? ? @WebMethod ? ? public WsUser getUser( ? ? ? ? @WebParam(name = "userid",partName = "userid") ? ? ? ? String userid); ? ? /** ? ? ?* 获取部门用户列表 ? ? ?* @param deptid ? ? ?* @return ? ? ?* ? ? returns testjws.client.WsUserList ? ? ?*/ ? ? @WebMethod ? ? public WsUserList getDeptUserList( ? ? ? ? @WebParam(name = "deptid",partName = "deptid") ? ? ? ? String deptid); }
package com.metecyu.yusr.ws; import java.util.ArrayList; import java.util.List; import javax.annotation.Resource; import javax.jws.WebService; import org.springframework.stereotype.Component; import com.metecyu.yusr.bmodel.UserD; import com.metecyu.yusr.model.User; import com.metecyu.yusr.service.UserService; import com.metecyu.yusr.ws.model.WsUser; import com.metecyu.yusr.ws.model.WsUserList; @WebService(endpointInterface="com.metecyu.yusr.ws.YusrWsSeviceItf",serviceName="yusrWsService",portName="yusrWsServicePort",targetNamespace="http://ws.yusr.metecyu.com") //该对象交由spring管理,studentWsService即为该实现类在bean容器中的name @Component("yusrWsSeviceImpl") public class YusrWsSeviceImpl implements YusrWsSeviceItf{ @Resource UserService userService; @Override public WsUser getUser(String id) { User user =this.userService.findById(id); WsUser ws = this.userService.turnToWsUser(user); return ws; } @Override public WsUserList getDeptUserList(String deptid) { List<UserD> userList = userService.findDeptUser(deptid); ArrayList<WsUser> outList = new ArrayList(); /**/ for(UserD user :userList){ WsUser ws = this.userService.turnToWsUser(user); outList.add(ws); } WsUserList usrList= new WsUserList(); usrList.userList = outList; return usrList; } }
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:context="http://www.springframework.org/schema/context" xmlns:ws="http://jax-ws.dev.java.net/spring/core" xmlns:wss="http://jax-ws.dev.java.net/spring/servlet" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd http://jax-ws.dev.java.net/spring/core http://jax-ws.java.net/spring/core.xsd http://jax-ws.dev.java.net/spring/servlet http://jax-ws.java.net/spring/servlet.xsd "> <context:annotation-config/> <context:component-scan base-package="com.metecyu.yusr"/> <!-- 这个错误是因为xsd的地址访问不到,不影响 --> <wss:binding url="/jaxws-spring"> <wss:service> <!-- bean的值需要加前缀 "#",studentWsService是实现类在bean容器中的名称 --> <ws:service bean="#yusrWsServiceImpl"> </ws:service> </wss:service> </wss:binding> </beans>
<servlet> <servlet-name>springWsServlet</servlet-name> <servlet-class>com.sun.xml.ws.transport.http.servlet.WSSpringServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>springWsServlet</servlet-name> <url-pattern>/jaxws-spring</url-pattern> </servlet-mapping>
<!-- Published by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.1.3-b02-. --> <!-- Generated by JAX-WS RI at http://jax-ws.dev.java.net. RI's version is JAX-WS RI 2.1.3-b02-. --> <definitions xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:tns="http://ws.yusr.metecyu.com" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="http://ws.yusr.metecyu.com" name="yusrWsSevice"> <import namespace="http://ws.yusr.metecyu.com/" location="http://localhost:8080/yusr/jaxws-spring?wsdl=1"/> <binding xmlns:ns1="http://ws.yusr.metecyu.com/" name="yusrWsSevicePortBinding" type="ns1:YusrWsServiceItf"> <soap:binding transport="http://schemas.xmlsoap.org/soap/http" style="rpc"/> <operation name="getUser"> <soap:operation soapAction=""/> <input> <soap:body use="literal" namespace="http://ws.yusr.metecyu.com/"/> </input> <output> <soap:body use="literal" namespace="http://ws.yusr.metecyu.com/"/> </output> </operation> <operation name="getDeptUserList"> <soap:operation soapAction=""/> <input> <soap:body use="literal" namespace="http://ws.yusr.metecyu.com/"/> </input> <output> <soap:body use="literal" namespace="http://ws.yusr.metecyu.com/"/> </output> </operation> </binding> <service name="yusrWsSevice"> <port name="yusrWsSevicePort" binding="tns:yusrWsSevicePortBinding"> <soap:address location="http://localhost:8080/yusr/jaxws-spring"/> </port> </service> </definitions> spring jax-ws - 客户端实现:
2.调用客户端
package testjws.client.main; import testjws.client.WsUser; import testjws.client.WsUserList; import testjws.client.YusrWsService; import testjws.client.YusrWsServiceItf; public class Main { public static void main(String[] args) { YusrWsServiceItf service = new YusrWsService().getYusrWsServicePort(); long st = System.currentTimeMillis(); // 自定义单个对象定义 WsUser user = service.getUser("yh1"); System.out.println("用户姓名:"+user.getUsername()); long end = System.currentTimeMillis(); System.out.println("用时:"+(end -st)); // 列表对象 WsUserList wsUserList = service.getDeptUserList("dept1"); System.out.println("n部门用户数量:"+ wsUserList.getUserList().size()); for(WsUser usr : wsUserList.getUserList()){ System.out.println(usr.getUsername()); } } } 参考资料:1?JAX与spring的无缝集成(一) ? http://schy-hqh.iteye.com/blog/1923934
2 添加 jaxws-spring、istack-commons-runtime 以后后台一共会添加的jar。jar相对xfire是不是稍多了,不过这只是服务器端才需要的jar包。使用jax-ws的优势在于客户端调用时则不需要任何jar包,只需要一个jdk就好了(jdk-1.6以上),这才是jax-ws的优势。
(编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |