您的位置:首页 > 编程语言 > Java开发

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.

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
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: