欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

spring 整合 cxf

程序员文章站 2022-07-12 18:37:55
...

 1 spring 整合 cxf 服务端

 

   所以jar可以从cxf的lib下面添加所有

  首先web.xml配置文件如下:

 

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
	http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
	<display-name></display-name>

	<!-- 加载Spring容器配置 -->
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
	</listener>

	<!-- 设置Spring容器加载配置文件路径 -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>/WEB-INF/spring/spring-*.xml</param-value>
	</context-param>

    <!-- web service -->
    <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>/ws/*</url-pattern>
    </servlet-mapping>

	<welcome-file-list>
		<welcome-file>index.jsp</welcome-file>
	</welcome-file-list>
</web-app>

 配置spring和cxf

 

 2 创建对应的service类

  先声明接口:

package com.malone.service;

import javax.jws.WebService;


/**
 * @author Claus
 * 本类为web端为android客户端提供的接口
 */
//@WebService(name = "IAndroidService", targetNamespace = "http://publish.smc.webservice.mr.android/")
@WebService
public interface IAndroidService {
	
    /**
     * wanzan 2013-07-15 添加此方法
     * 在自动线巡功能中获取服务器端时间,用来校正客户端时间
     * @param requestJson
     * @return
     */
    public String getServerTime(String requestJson);
}

 

   声明实现类:

package com.malone.serviceImpl;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;

import javax.jws.WebService;

import com.malone.service.IAndroidService;

/**
 * 为android客户端提供的接口服务端。
 * 
 * @author Claus
 */
//@WebService(name = "AndroidService", targetNamespace = "http://publish.smc.webservice.mr.android/", endpointInterface = "com.malone.service.IAndroidService")
@WebService
public class AndroidService implements IAndroidService {

    private static DateFormat dateFormat = new SimpleDateFormat(
            "yyyy-MM-dd HH:mm:ss");

	@Override
	public String getServerTime(String requestJson) {
		return dateFormat.format(new Date());
	}

    
    
    
}

 在spring的配置文件中定义service的bean

<?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"
    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">

	<bean id="androidService" class="com.malone.serviceImpl.AndroidService"/>
	
</beans>

 在配置文件中发布webservice:

<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-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:endpoint 
        id="wsAndroidService1" 
        implementor="#androidService" 
        address="/android">
        <jaxws:inInterceptors>  
            <bean class="com.malone.filter.AndroidServiceLoggingInterceptor"/>  
        </jaxws:inInterceptors> 
    </jaxws:endpoint>
    
</beans>

 把web项目添加到tomcat中运行,访问:http://localhost:8080/应用名/ws/android?wsdl;/ws是在web.xml中配置的,/android是在spring_cxf中配置的

 

2 客户端代码:

  

(1)首先用wsdl命令生成客户端代码:

根据wsdl生成客户端代码的命令

wsdl2java用法:

wsdl2java -p com -d src -all  aa.wsdl
wsdl2java -d src - client http://localhost:9000/helloWorld?wsdl
-p  指定其wsdl的命名空间,也就是要生成代码的包名:
-d  指定要产生代码所在目录
-client 生成客户端测试web service的代码
-server 生成服务器启动web  service的代码
-impl 生成web service的实现代码
-ant  生成build.xml文件
-all 生成所有开始端点代码:types,service proxy,,service interface, server mainline, client mainline, implementation object, and an Ant build.xml file.
(1.1) 根据http://localhost:8080/webserviceDemo/ws/android?wsdl 可以看到wsdl文档,保存成xml格式,我保存到F盘跟目录,在cxf的bin目录下,使用wsdl2java命令生成客户端代码:
wsdl2java -p com.malone.client -d F:workspace\webserviceClient\src -all F:\android.xml
此命令会在路径F:workspace\webserviceClient\src(即webserviceClient项目的源码路径)生成对应的代码
网上很多教程上,客户端测试都是使用服务端的Service类,在此使用(以及下面两中方式)都是使用上面的命令生成的客户端的IAndroidService接口及对应的实现类,生成代码为:
package com.malone.client;

import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
import javax.xml.bind.annotation.XmlSeeAlso;
import javax.xml.ws.RequestWrapper;
import javax.xml.ws.ResponseWrapper;

/**
 * This class was generated by Apache CXF 2.7.11
 * 2014-05-09T14:46:36.919+08:00
 * Generated source version: 2.7.11
 * 
 */
@WebService(targetNamespace = "http://service.malone.com/", name = "IAndroidService")
@XmlSeeAlso({ObjectFactory.class})
public interface IAndroidService {

    @WebResult(name = "return", targetNamespace = "")
    @RequestWrapper(localName = "getServerTime", targetNamespace = "http://service.malone.com/", className = "com.malone.client.GetServerTime")
    @WebMethod
    @ResponseWrapper(localName = "getServerTimeResponse", targetNamespace = "http://service.malone.com/", className = "com.malone.client.GetServerTimeResponse")
    public java.lang.String getServerTime(
        @WebParam(name = "arg0", targetNamespace = "")
        java.lang.String arg0
    );
}
 
/**
 * Please modify this class to meet your needs
 * This class is not complete
 */

package com.malone.client;

import java.util.logging.Logger;
import javax.jws.WebMethod;
import javax.jws.WebParam;
import javax.jws.WebResult;
import javax.jws.WebService;
import javax.xml.bind.annotation.XmlSeeAlso;
import javax.xml.ws.RequestWrapper;
import javax.xml.ws.ResponseWrapper;

/**
 * This class was generated by Apache CXF 2.7.11
 * 2014-05-09T14:46:36.908+08:00
 * Generated source version: 2.7.11
 * 
 */

@javax.jws.WebService(
                      serviceName = "AndroidServiceService",
                      portName = "AndroidServicePort",
                      targetNamespace = "http://serviceImpl.malone.com/",
                      wsdlLocation = "file:/F:/android.xml",
                      endpointInterface = "com.malone.client.IAndroidService")
                      
public class IAndroidServiceImpl implements IAndroidService {

    private static final Logger LOG = Logger.getLogger(IAndroidServiceImpl.class.getName());

    /* (non-Javadoc)
     * @see com.malone.client.IAndroidService#getServerTime(java.lang.String  arg0 )*
     */
    public java.lang.String getServerTime(java.lang.String arg0) { 
        LOG.info("Executing operation getServerTime");
        System.out.println(arg0);
        try {
            java.lang.String _return = "";
            return _return;
        } catch (java.lang.Exception ex) {
            ex.printStackTrace();
            throw new RuntimeException(ex);
        }
    }

}
 
(2)客户端可以使用spring进行整合
   从新开启一个web项目,添加spring整合cxf 客户端的配置:
<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-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="androidService" serviceClass="com.malone.client.IAndroidService" 
        address="http://localhost:8080/webserviceDemo/ws/android"/>
    
</beans>
 在代码中调用方法:
package com.malone.client.test;

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.context.support.FileSystemXmlApplicationContext;

import com.malone.client.IAndroidService;

public class SpringClientTest {

	public static void main(String[] args) {
		ApplicationContext ctx = new FileSystemXmlApplicationContext("WebRoot/WEB-INF/spring/spring-cxf.xml");  
		IAndroidService androidService = (IAndroidService)ctx.getBean("androidService");
		System.out.println(androidService.getServerTime(null));
	}
}
 
(3)使用JaxWsProxyFactoryBean调用
示例代码:
package com.malone.client.test;

import java.text.ParseException;

import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;

import com.malone.client.IAndroidService;

public class SoapClient {
	public static void main(String[] args) throws ParseException {
		JaxWsProxyFactoryBean soapFactoryBean = new JaxWsProxyFactoryBean();

		soapFactoryBean
				.setAddress("http://localhost:8080/webserviceDemo/ws/android");
		soapFactoryBean.setServiceClass(IAndroidService.class);
		Object o = soapFactoryBean.create();
		IAndroidService iAndroidService = (IAndroidService) o;
		System.out.println(iAndroidService.getServerTime(null));
	}
}