Spring 实现远程访问详解——webservice
2016-04-21 15:12
405 查看
前几章分别介绍了spring rmi,spring httpinvoker,httpclient实现远程访问。本章将通过webserver技术实现spring远程访问。
Spring Web Services 是基于 Spring 框架的 Web 服务框架,主要侧重于基于文档驱动的Web服务,提供 SOAP 服务开发,允许通过多种方式创建 Web 服务。本章利用Apache CXF构建和开发webservice.
2) 服务端创建webservice接口
3) 服务端实现webservice接口
4) 服务端配置暴露webservice接口
5) 客户端创建暴露的webservice接口
6) 客户端调用webservice接口
6) 服务端运行显示
原因是因为客户端的service中的方法没有跟服务端绑定,只需要将客户端的Service接口加上注解@WebService,异常便解决了
代码路径:http://download.csdn.net/detail/a123demi/9495931
Spring Web Services 是基于 Spring 框架的 Web 服务框架,主要侧重于基于文档驱动的Web服务,提供 SOAP 服务开发,允许通过多种方式创建 Web 服务。本章利用Apache CXF构建和开发webservice.
1. webservice远程访问流程
1) pom文件引入cxf和wsdl相关包依赖2) 服务端创建webservice接口
3) 服务端实现webservice接口
4) 服务端配置暴露webservice接口
5) 客户端创建暴露的webservice接口
6) 客户端调用webservice接口
2. Webservice具体实现
1) pom文件引入cxf和wsdl相关包依赖
<cxf.version>3.1.6</cxf.version> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-core</artifactId> <version>${cxf.version}</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-bindings-soap</artifactId> <version>${cxf.version}</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-databinding-jaxb</artifactId> <version>${cxf.version}</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-frontend-simple</artifactId> <version>${cxf.version}</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-transports-http</artifactId> <version>${cxf.version}</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-frontend-jaxws</artifactId> <version>${cxf.version}</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-wsdl</artifactId> <version>${cxf.version}</version> </dependency> <dependency> <groupId>wsdl4j</groupId> <artifactId>wsdl4j</artifactId> <version>1.6.3</version> </dependency>
2) 服务端创建webservice接口
package com.lm.core.service; importjava.util.List; importjavax.jws.WebParam; importjavax.jws.WebService; importcom.lm.core.entity.User; @WebService publicinterface UserWsService { List<User>getUserByAcount(@WebParam(name="name") Stringname,@WebParam(name="password") String password); void insert(User user); }
3) 服务端实现webservice接口
package com.lm.core.service.impl; import java.util.ArrayList; import java.util.List; import javax.jws.WebService; import org.springframework.beans.factory.annotation.Autowired; import com.lm.core.entity.User; import com.lm.core.mapper.UserMapper; import com.lm.core.service.UserWsService; @WebService public class UserWsServiceImpl implements UserWsService { @Autowired private UserMapper userMapper; @Override public List<User> getUserByAcount(String name, String password) { System.err.println("获取用户信息:" + name + password); return new ArrayList<User>(); } @Override public void insert(User user) { System.err.println("开始插入用户信息:" + user.toString()); } }
4) 服务端配置暴露webservice接口
配置头信息:<beans xmlns="http://www.springframework.org/schema/beans" xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"xmlns:tx="http://www.springframework.org/schema/tx" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd"> <bean id="userWsService"class="com.lm.core.service.impl.UserWsServiceImpl"/> <!-- 2:通过jaxws:server方式来配置webservice --> <jaxws:server serviceClass="com.lm.core.service.UserWsService" address="/userWebService" > <jaxws:serviceBean> <ref bean="userWsService" /> </jaxws:serviceBean> </jaxws:server>
5) 服务端web.xml配置
<servlet> <servlet-name>CXFService</servlet-name> <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>CXFService</servlet-name> <url-pattern>/webservice/*</url-pattern> </servlet-mapping>
6) 服务端运行显示
7) 客户端创建暴露的webservice接口
package com.lm.core.service; import java.util.List; import javax.jws.WebParam; import javax.jws.WebService; import com.lm.core.entity.User; @WebService public interface UserWsService { List<User> getUserByAcount(@WebParam(name="name")String name,@WebParam(name="password")String password); void insert(User user); }
8) 客户端配置服务器暴露接口
<import resource="classpath:META-INF/cxf/cxf.xml"/> <!-- <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml"/> <importresource="classpath:META-INF/cxf/cxf-servlet.xml"/> --> <jaxws:client id="userWsService"serviceClass="com.lm.core.service.UserWsService" address="http://127.0.0.1:8080/spring_remote_server/webservice/userWebService"/>
9) 客户端调用webservice接口
@RequestMapping(value = "/wsTest") @ResponseBody public BaseMapVo wsTest(String name, String password) { BaseMapVo vo = new BaseMapVo(); long startDate = Calendar.getInstance().getTimeInMillis(); System.out.println("wsTest客户端开始调用" + startDate); UserWsService ws = (UserWsService) ApplicationContextUtil.getInstance().getBean("userWsService"); ws.getUserByAcount("张三", ":张三的密码"); System.out.println("wsTest客户端调用结束" + (Calendar.getInstance().getTimeInMillis()-startDate)); vo.setRslt("sucess"); return vo; }
3. 注意异常问题
1) 服务端和客户端接口和实现需要加入@WebService注解
否则:Exception in thread "main"javax.xml.ws.WebServiceException: Could not find wsdl:binding operation infofor web method原因是因为客户端的service中的方法没有跟服务端绑定,只需要将客户端的Service接口加上注解@WebService,异常便解决了
2) 暴露接口中参数最好加上@WebParam,实现参数命名
否则会导致参数名称不匹配现象。代码路径:http://download.csdn.net/detail/a123demi/9495931
相关文章推荐
- restTemplete webService in spring
- eclipse新建安卓项目低于4.0,去除v7依赖包的方法
- (java)关于List中对象实现Comparable接口的例子
- java 面向对象及编程理解
- springMVC 枚举传值问题
- [置顶] JAVA串口通讯程序
- 《Spring实战》学习笔记-第六章:web视图解析
- Eclipse下调试安卓程序
- 《Spring实战》学习笔记-第五章:构建Spring web应用
- JAVA数据类型
- ERROR: JAVA_HOME is set to an invalid directory. JAVA_HOME = "D:\tools\jdk\jdk1.8.0_73" Please set t
- java 日期格式化
- [改善Java代码]不要只替换一个类
- java对cookie的操作
- SpringMVC
- java注解(annotation)
- Java的4种代码块总结
- SpringMVC注解@Autowired和@Qualifier 自动注入[根据类型注入] @Autowired 可以对成员变量、方法以及构造函数进行注释, @Qualifier 的
- Java Map和Bean相互转换
- Java Map和Bean相互转换