基于Maven在Spring中集成CXF Web Service框架

一、jar包依赖

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

二、代码编写

1.接口定义:

@WebService(name = "PersonService") 
public interface PersonService {
	@WebMethod 
    Long savePerson(Person person);
}

2.接口实现:

@WebService(endpointInterface="com.ssh.service.PersonService",
			targetNamespace ="http://service.ssh.com")
public class PersonServiceImpl implements PersonService {
。。。
}

3.实体类:

/** 
* Web Service传输User信息的DTO.*  
* 只传输外部接口需要的属性.使用JAXB 2.0的annotation标注JAVA-XML映射,尽量使用默认约定.*  
* @XmlRootElement指定User为XML的根元素。User类的属性默认指定映射为@XmlElement。 
* @XmlElement用来定义XML中的子元素。 
* @XmlType-映射一个类或一个枚举类型成一个XML Schema类型 
*/  
@XmlRootElement  
@XmlType(name = "Person", namespace = "http://person.ssh.com")  
public class Person{  
    private Long id;  
    private String loginName;  
    private String name;  
    private String email;  
    // 相关get set方法

三、配置Springbean及webservice发布

创建cxf-servlet.xml将其放在spring配置文件扫描路径下

<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.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-servlet.xml"/>
    <bean id="myService" class="com.ssh.service.impl.PersonServiceImpl" />
    <jaxws:endpoint id="testService" implementor="#myService" address="/testService"/>
</beans>

说明: 
1. cxf-servlet.xml中import导入的文件不用自己创建,这是在依赖包中的。 

web.xml中

  <!--加载Spring的配置文件到上下文中去-->
        <context-param>
            <param-name>contextConfigLocation</param-name>
            <param-value>
                classpath:applicationContext.xml
             	classpath:cxf-servlet.xml  
            </param-value>
        </context-param>
四、配置webservice拦截
                <servlet>  
			<servlet-name>CXFServlet</servlet-name>       
			<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>  
		</servlet>  
		<servlet-mapping>  
			<servlet-name>CXFServlet</servlet-name>  
			<url-pattern>/cxf/*</url-pattern>  
		</servlet-mapping> 

五、测试服务是否发布成功

部署并启动服务,访问wsdl,本配置访问如下: 
http://localhost:8081/sshTO/cxf/testService?wsdl

我的项目部署在本地,端口8081,应用访问名称为sshTO,cxf拦截配置为/cxf/*,发布相对地址为/testService.


客户端调用方法

/**
 * jdk自动生成文件步骤 1.在cmd中进入客户端src目录下
 * 		 2.输入wsimport -keep http://localhost:8081/sshTO/cxf/testService?wsdl 或者本地wsdl文件绝对路径
 * 				ps:前者会生成.class文件后者不会
 * 
 * @author zibo.liu
 *
 */


public class ClientText {
	
	public static void main(String[] args) {
		//获取PersonService  第一种方法 用于control层会加载不了
		PersonServiceImplService psi = new PersonServiceImplService();
		PersonService psiS = psi.getPersonServiceImplPort();
		//another way 需要导包cxf-rt-frontend-jaxrs(好像这个)
	/*	PersonService PersonService = JAXRSClientFactory  
		            .create("http://localhost:8081/sshTO/cxf/testService",   
		            		PersonService.class); */
		List<Person> list = psiS.findAll();
		for (Person object : list) {
			System.out.println(object.getName());
		}
		//获取PersonService  第二种方法 用于control层   import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
		/*JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();

	       factory.setServiceClass(PersonService.class);

	    factory.setAddress("http://localhost:8081/sshTO/cxf/testService?wsdl");

	    PersonService PersonService = (PersonService) factory.create();*/
	}
}

基于JAX-RS的Restful客户端调用 博文 http://blog.csdn.net/menghuannvxia/article/details/46236513

猜你喜欢

转载自blog.csdn.net/bonie_juzi/article/details/79374594