CXF 开发SOAP WebService服务(Spring+多种形式)

使用CXF框架来开发SOAP WebService服务

服务端(web项目):

1.使用maven构建项目所需jar

  <dependencies>
    	<dependency>
	    <groupId>junit</groupId>
	    <artifactId>junit</artifactId>
	    <version>4.10</version>
            <scope>test</scope>
	</dependency>
 	<dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-webmvc</artifactId>
            <version>4.1.3.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context</artifactId>
            <version>4.1.3.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId>
            <version>4.1.3.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId>
            <version>4.1.3.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-jdbc</artifactId>
            <version>4.1.3.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId>
            <version>4.1.3.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-tx</artifactId>
            <version>4.1.3.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId>
            <version>4.1.3.RELEASE</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-orm</artifactId>
            <version>4.1.3.RELEASE</version>
        </dependency>

        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-core</artifactId>
            <version>4.3.11.Final</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-entitymanager</artifactId>
            <version>4.3.11.Final</version>
        </dependency>
        <dependency>
            <groupId>org.hibernate</groupId>
            <artifactId>hibernate-ehcache</artifactId>
            <version>4.3.11.Final</version>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <version>5.1.35</version>
        </dependency>
        <dependency>
            <groupId>commons-dbcp</groupId>
            <artifactId>commons-dbcp</artifactId>
            <version>1.2.2</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>

   
        <!-- jackson -->
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
            <version>2.5.0</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-core</artifactId>
            <version>2.5.0</version>
        </dependency>
        <dependency>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-annotations</artifactId>
            <version>2.5.0</version>
        </dependency>
        <!-- https://mvnrepository.com/artifact/org.springframework.data/spring-data-jpa -->
        <dependency>
            <groupId>org.springframework.data</groupId>
            <artifactId>spring-data-jpa</artifactId>
            <version>1.7.1.RELEASE</version>
        </dependency>
        
        <dependency>
		    <groupId>org.slf4j</groupId>
		    <artifactId>slf4j-api</artifactId>
		    <version>1.7.13</version>
		</dependency>
		<dependency>
		    <groupId>org.slf4j</groupId>
		    <artifactId>slf4j-simple</artifactId>
		    <version>1.7.13</version>
		    <scope>test</scope>
		</dependency>
		<!-- CXF -->
		<dependency>
			<groupId>org.apache.cxf</groupId>
			<artifactId>cxf</artifactId>
			<version>2.7.7</version>
			<type>pom</type>
		</dependency>
		<dependency>
			<groupId>org.apache.cxf</groupId>
			<artifactId>cxf-rt-frontend-jaxws</artifactId>
			<version>2.7.7</version>
		</dependency>
	
		<dependency>
			<groupId>org.apache.cxf</groupId>
			<artifactId>cxf-rt-transports-http</artifactId>
			<version>2.7.7</version>
		</dependency>
  </dependencies>

2.spring的配置主要包括 导入CXF的配置文件和发布WS服务的配置  其他配置省略,注意开启注解扫描(以下xx表示你的公司)

      <!-- 导入CXF的核心配置 -->  
    <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" />  
     
    <!-- 发布ws服务的配置 -->
      <jaxws:endpoint id="userService" implementor="com.xx.UserServiceImpl" address="/userService" />
3.web.xml

        <!-- Spring -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath:application.xml</param-value>
	</context-param>
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>

	<!-- CXF -->
	<servlet>
        <servlet-name>cxf</servlet-name>
        <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
	</servlet>
	<servlet-mapping>
	    <servlet-name>cxf</servlet-name>
	    <url-pattern>/*</url-pattern>
	</servlet-mapping>

4.功能代码(entity层省略)

dao层:

public interface IUserDao extends JpaRepository<User, Integer>
	,JpaSpecificationExecutor<User>{

}

service层:

@Service
@WebService
public interface IUserService {
	@WebMethod
	public String sayHi(String name);
	@WebMethod
	public User queryById(int uno);
	@WebMethod
	public List<User> queryAll();
	@WebMethod
	public void delUser(User user);
}
@Service
@Transactional
@WebService(endpointInterface="com.xx.IUserService"
,serviceName="userService",targetNamespace="http://xx.com/")
public class UserServiceImpl implements IUserService {
	@Autowired
	private IUserDao userDao;
	@Override
	public String sayHi(String name) {
		// TODO Auto-generated method stub
		return "你好~"+name;
	}
	@Override
	public User queryById(int uno) {
		// TODO Auto-generated method stub
		return userDao.findOne(uno);
	}
	public List<User> queryAll() {
	// TODO Auto-generated method stub
		return userDao.findAll();
		}
	@Override
	public void delUser(User user) {
		// TODO Auto-generated method stub
		userDao.delete(user);
	}
}

5.启动Tomcat测试,地址栏输入http://localhost:8080/soapServer/,出现如下图所示,说明发布WS服务暴露成功.


===================================================================================================================================

客户端,采用两种方式实现,一种是依赖JaxWsDynamicClientFactory实现,一种是与Spring结合jaxws:client的方式实现.

pom.xml:

     <dependencies>
       <!-- Junit -->
       <dependency>
          <groupId>junit</groupId>
          <artifactId>junit</artifactId>
          <version>4.10</version>
          <scope>test</scope>
       </dependency>
       <!-- CXF -->
       <dependency>
          <groupId>org.apache.cxf</groupId>
	  <artifactId>cxf</artifactId>
	  <version>2.7.7</version>
	  <type>pom</type>
       </dependency>
       <dependency>
	  <groupId>org.apache.cxf</groupId>
	  <artifactId>cxf-rt-frontend-jaxws</artifactId>
	  <version>2.7.7</version>
	  </dependency>
	<dependency>
	  <groupId>org.apache.cxf</groupId>
	  <artifactId>cxf-rt-transports-http</artifactId>
	  <version>2.7.7</version>
	</dependency>
	<!-- https://mvnrepository.com/artifact/commons-lang/commons-lang -->
	<dependency>
	    <groupId>commons-lang</groupId>
	    <artifactId>commons-lang</artifactId>
	    <version>2.6</version>
	</dependency>
	<!-- JSONArray,JSONObject -->
	<!-- https://mvnrepository.com/artifact/commons-beanutils/commons-beanutils -->
	<dependency>
	    <groupId>commons-beanutils</groupId>
	    <artifactId>commons-beanutils</artifactId>
	    <version>1.8.3</version>
	</dependency>
	<!-- https://mvnrepository.com/artifact/commons-collections/commons-collections -->
	<dependency>
	    <groupId>commons-collections</groupId>
	    <artifactId>commons-collections</artifactId>
	    <version>3.2.1</version>
	</dependency>
	<!-- https://mvnrepository.com/artifact/commons-logging/commons-logging -->
	<dependency>
	    <groupId>commons-logging</groupId>
	    <artifactId>commons-logging</artifactId>
	    <version>1.1.1</version>
	</dependency>
	<!-- https://mvnrepository.com/artifact/net.sf.ezmorph/ezmorph -->
	<dependency>
	    <groupId>net.sf.ezmorph</groupId>
	    <artifactId>ezmorph</artifactId>
	    <version>1.0.6</version>
	</dependency>
	<!-- https://mvnrepository.com/artifact/net.sf.json-lib/json-lib -->
	<dependency>
	    <groupId>net.sf.json-lib</groupId>
	    <artifactId>json-lib</artifactId>
	    <version>2.2.2</version>
	    <classifier>jdk15</classifier>
	</dependency>
	<!-- Spring -->
	<dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
        <version>4.1.3.RELEASE</version>
   	</dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>4.1.3.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-beans</artifactId>
        <version>4.1.3.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>4.1.3.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-jdbc</artifactId>
        <version>4.1.3.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-aop</artifactId>
        <version>4.1.3.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-tx</artifactId>
        <version>4.1.3.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>4.1.3.RELEASE</version>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-orm</artifactId>
        <version>4.1.3.RELEASE</version>
    </dependency>
  </dependencies>
(一)依赖JaxWsDynamicClientFactory实现(复杂的数据类型暂时没写):

public class soapclientTest {
	private static final String url = "http://localhost:8080/soapServer/userService?wsdl";
	//cxf客户端
	Client client = null;
	@Before
	public void before(){
		JaxWsDynamicClientFactory factory = JaxWsDynamicClientFactory.newInstance();//动态代理
		client = factory.createClient(url);
	}
	//传入String,返回String
	@Test
	public void sayHi(){
		try {
			Object[] result = client.invoke("sayHi","呢喃北上");
			System.out.println(result[0]);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	//传入int,返回object
	@Test
	public void queryById(){
		try {
			Object[] result = client.invoke("queryById",1);
			Method uno = result[0].getClass().getMethod("getUno");  
	        Object objuno = uno.invoke(result[0]);  
	        System.out.println("user--uno:" + objuno);
	        Method uname = result[0].getClass().getMethod("getUname");  
	        Object objuname = uname.invoke(result[0]);  
	        System.out.println("user--uname:" + objuname);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	//返回list
	@Test
	public void queryAll(){
		try {
			Object[] result = client.invoke("queryAll");
			JSONArray jsonArray = JSONArray.fromObject(result[0]);
			for (Object object : jsonArray) {
				System.out.println(object.toString());
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	//传入object
	@Test
	public void delOne() throws Exception{
		System.out.println("start");
		Object user = Thread.currentThread().getContextClassLoader()
				.loadClass("com.xx.User").newInstance();//动态加载
		Method m0 = user.getClass().getMethod("setUno", Integer.class);
		Method m1 = user.getClass().getMethod("setUname", String.class);
		m0.invoke(user, 1);
		m1.invoke(user, "mokey");
		client.invoke("delUser", user);
	    System.out.println("end");
	}
}
(二)与Spring结合jaxws:client的方式实现

application.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:context="http://www.springframework.org/schema/context"
	xmlns:tx="http://www.springframework.org/schema/tx" xmlns:jpa="http://www.springframework.org/schema/data/jpa"
	xmlns:jaxws="http://cxf.apache.org/jaxws"
	xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
    http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
    http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-4.0.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd
    http://cxf.apache.org/jaxws 
	http://cxf.apache.org/schemas/jaxws.xsd"
     >
    <!-- 使用 jaxws:client -->
    <jaxws:client id="userService"
                  serviceClass="com.xx.IUserService"
                  address="http://localhost:8080/soapServer/userService"/>
</beans>

测试类:
public class springsoapClient {

	/**
	 * 基于Spring的客户端
	 */
	public static void main(String[] args) {
	     // TODO Auto-generated method stub
	     ApplicationContext context = new ClassPathXmlApplicationContext("application.xml");
             IUserService userService = context.getBean("userService", IUserService.class);
	     String result = userService.sayHi("呢喃北上");
	     System.out.println(result);
	}

}

项目源码:http://download.csdn.net/download/qq_33764491/10175218

2017.12.27日更新.







评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值