CXF+Spring整合发布webservice服务

【服务端】

1:引入cxf相jar包,在pom.xml中加入下面一段(注意第三个依赖再tomcat中部署可以去掉)。

<properties>
  <cxf.version>2.2.3</cxf.version>
</properties>
 
<dependencies>
    <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-transports-http</artifactId>
        <version>${cxf.version}</version>
    </dependency>
        <!-- Jetty is needed if you're are not using the CXFServlet -->
    <dependency>
        <groupId>org.apache.cxf</groupId>
        <artifactId>cxf-rt-transports-http-jetty</artifactId>
        <version>${cxf.version}</version>
    </dependency>
</dependencies>

2:编写类

实体类:

package com.test.model;
 
import java.io.Serializable;
 
public class User implements Serializable {
     
    private String username;
     
    public String getUsername() {
        return username;
    }
    public void setUsername(String username) {
        username = username;
    }
}

WebService接口类:

package com.test.webservice;
 
import java.util.List;
 
import javax.jws.WebParam;
import javax.jws.WebService;
 
import com.test.model.User;
 
@WebService
public interface IUserService {
     
    public String addUser(List<User> users);
 
}

WebService接口实现类:

package com.test.webservice.impl;
 
import java.util.List;
 
import javax.jws.WebService;
 
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.test.model.Order;
import com.test.webservice.IUserService;
 
 
@WebService(endpointInterface="com.test.webservice.IUserService") 
public class UserServiceImpl implements IUserService {
 
    public String addUser(List<User> users) {
        return JSONArray.toJSONString(users,true);
    }
     
}

3:配置applicationContext.xml文件

<?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:jaxws="http://cxf.apache.org/jaxws"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       http://www.springframework.org/schema/beans/spring-beans-2.5.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:endpoint id="userServiceEndPoint" implementor="com.test.webservice.impl.UserServiceImpl" address="/UserService"></jaxws:endpoint>
</beans>

4:配置web.xml文件

<!DOCTYPE web-app PUBLIC
 "-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
 "http://java.sun.com/dtd/web-app_2_3.dtd" >
 
<web-app>
  <display-name>Archetype Created Web Application</display-name>
   
  <context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>classpath:applicationContext.xml</param-value>
  </context-param>
   
  <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  </listener>
   
  <servlet>
    <servlet-name>CXFServlet</servlet-name>
    <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
     <load-on-startup>1</load-on-startup>
  </servlet>
   
  <servlet-mapping>
     <servlet-name>CXFServlet</servlet-name>
     <url-pattern>/services/*</url-pattern>
  </servlet-mapping>
</web-app>
 上面的步骤完成后就可以启动Tomcat进行访问。

wsdl:

http://localhost:8080/项目名/services/接口发布名称?wsdl

【客户端】

1:cmd到其项目的src目录并执行命令就会解析生成相应的调用代码,命令:wsimport -keep http://localhost:8080/项目名/services/接口发布名称?wsdl

2:得到生成的代码后创建客户单项目,导入生成代码。编写调用代码

package com.test.client;
 
import java.util.ArrayList;
import java.util.List;
 
import com.test.webservice.User;
import com.test.webservice.impl.IUserService;
import com.test.webservice.impl.UserServiceImplService;
 
public class Client {
 
    public static void main(String[] args) {
         
         //创建一个用于产生UserServiceImplService实例的工厂,UserServiceImplService类是wsimport工具生成的
        UserServiceImplService factory = new UserServiceImplService();
         
        //通过工厂生成一个IUserService实例,IUserService是wsimport工具生成的
        IUserService service = factory.getUserServiceImplPort();
         
        List<User> users = new ArrayList<User>();
        User user1 = new User();
        user1.setUsername("123");
        users.add(user1);
         
      User user2 = new User();
      user2.setUsername("321");
      users.add(user2);
String resultUser = service.addUser(users); System.out.println(resultUser); } } 运行程序得到结果

©️2020 CSDN 皮肤主题: 编程工作室 设计师:CSDN官方博客 返回首页