Spring整合Webservice

首先,CXF和spring整合需要准备如下jar包文件:

image

 

首先在web.xml中添加如下配置:

<!-- 加载Spring容器配置 -->
<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<!-- 设置Spring容器加载配置文件路径 -->
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath*:applicationContext-server.xml</param-value>
</context-param> 
<listener>
    <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
</listener>
<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>/services/*</url-pattern>
</servlet-mapping>

然后在src目录中,新建一个applicationContext-server.xml文件,文件内容如下:

 

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jaxws="http://cxf.apache.org/jaxws"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    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://cxf.apache.org/jaxws 
    http://cxf.apache.org/schemas/jaxws.xsd"

注意上面的带下划线加粗部分,这个很重要的哦!不能写错或是遗漏了。

添加完这个文件后,还需要在这个文件中导入这么几个文件。文件内容如下:

<import resource="classpath:META-INF/cxf/cxf.xml"/>
<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml"/>
<import resource="classpath:META-INF/cxf/cxf-servlet.xml"/>

下面开始写服务器端代码,首先定制服务器端的接口,代码如下:

package test.service; 
import javax.jws.WebParam;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;
import test.entity.User;
import test.entity.Users;
/**
 * <b>function:</b>定制客户端请求WebService所需要的接口
 * @file ComplexUserService.java
 * @package test.service
 * @project CXFWebService
 */
@WebService
@SOAPBinding(style = Style.RPC)
public interface IComplexUserService {
    public User getUserByName(@WebParam(name = "name") String name);
    public void setUser(User user);
}

下面编写WebService的实现类,服务器端实现代码如下:

package test.service;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import javax.jws.WebParam;
import javax.jws.WebService;
import javax.jws.soap.SOAPBinding;
import javax.jws.soap.SOAPBinding.Style;
import test.entity.User;
import test.entity.Users;
/**
 * <b>function:</b> WebService传递复杂对象,如JavaBean、Array、List、Map等
 * @file ComplexUserService.java
 * @version 1.0
 */
@WebService
@SOAPBinding(style = Style.RPC)
@SuppressWarnings("deprecation")
public class ComplexUserService implements IComplexUserService {
    public User getUserByName(@WebParam(name = "name") String name) {
        User user = new User();
        user.setId(new Date().getSeconds());
        user.setName(name);
        user.setAddress("china");
        return user;
    }
    public void setUser(User user) {
        System.out.println("############Server setUser###########");
        System.out.println("setUser:" + user);
    }
}

注意的是和Spring集成,这里一定要完成接口实现,如果没有接口的话会有错误的。

下面要在applicationContext-server.xml文件中添加如下配置:

<bean id="userServiceBean" class="test.service.ComplexUserService"/>
<!-- 注意下面的address,这里的address的名称就是访问的WebService的name -->
<jaxws:server id="userService" serviceClass="test.service.IComplexUserService" address="/Users">
    <jaxws:serviceBean>
        <!-- 要暴露的 bean 的引用 -->
        <ref bean="userServiceBean"/>
    </jaxws:serviceBean>
</jaxws:server>

下面启动tomcat服务器后,在WebBrowser中请求:

http://localhost:8080/CXFWebService/services/Users?wsdl

如果你能看到wsdl的xml文件的内容,就说明你成功了,注意的是上面地址的Users就是上面xml配置中的address的名称,是一一对应的。

下面编写客户端请求的代码,代码如下:

package test.client;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import test.entity.User;
import test.service.IComplexUserService;
/**
 * <b>function:</b>请求Spring整合CXF的WebService客户端
 * @project CXFWebService
 * @version 1.0
 */
public class SpringUsersWsClient {
    public static void main(String[] args) {
        //调用WebService
        JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
        factory.setServiceClass(IComplexUserService.class);
        factory.setAddress("http://localhost:8080/CXFWebService/services/Users");
        IComplexUserService service = (IComplexUserService) factory.create();
        System.out.println("#############Client getUserByName##############");
        User user = service.getUserByName("tets");
        System.out.println(user);   
        user.setAddress("China-Guangzhou");
        service.setUser(user);
    }
}

这个server端是通过Spring整合配置的,下面我们将Client端也通过Spring配置完成整合。

首先增加applicationContext-client.xml配置文件,文件内容如下:

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:jaxws="http://cxf.apache.org/jaxws"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    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://cxf.apache.org/jaxws 
    http://cxf.apache.org/schemas/jaxws.xsd"
    
    <import resource="classpath:META-INF/cxf/cxf.xml"/>
    <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml"/>
    <import resource="classpath:META-INF/cxf/cxf-servlet.xml"/>
    <jaxws:client id="userWsClient" serviceClass="test.service.IComplexUserService" 
        address="http://localhost:8080/CXFWebService/services/Users"/>
</beans>

客户端请求代码如下:

package test.client;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import test.entity.User;
import test.service.IComplexUserService;
/**
 * <b>function:</b>请求Spring整合CXF的WebService客户端
 * @file SpringUsersWsClient.java
 * @project CXFWebService
 * @version 1.0
 */
public class SpringUsersWsClient {
    public static void main(String[] args) {
        ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext-client.xml");
        IComplexUserService service = ctx.getBean("userWsClient", IComplexUserService.class);
        System.out.println("#############Client getUserByName##############");
        User user = service.getUserByName("test");
        System.out.println(user);
        user.setAddress("China-Guangzhou");
        service.setUser(user);
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值