提到cxf ,毫不避免的要說到webservice
什麼是webservice,我就不多說了。
建立一個maven 項目 通過pom引用cxf的jar
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>3.0.4</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>3.0.4</version>
</dependency>
我用的版本是3.0.4
引入springmvc 的jar
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>4.0.7.RELEASE</version>
</dependency>
建立一個包com.andy.cxf
建立一個interface ITestService 如下
package com.text.service;
import javax.jws.WebService;
@WebService
public interface ITestService {
public String test();
}
建立實作類
包名com.text.service.impl
類名TestServiceImpl
内容:
package com.text.service.impl;
import javax.jws.WebService;
import com.text.service.ITestService;
@WebService
public class TestServiceImpl implements ITestService{
public String test(){
return "success";
}
}
在classpath下面建立一個beanReServer.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-3.0.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd">
<jaxws:endpoint id="TestFacade" implementor="com.text.service.impl.TestServiceImpl" address="/TestFacade" />
</beans>
在web.xml裡面加入
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:beanReServer.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>/*</url-pattern>
</servlet-mapping>
至此,成功搭建完畢,啟動項目
http://localhost:8080/test-cxf
成功通路
由于cxf版本的版本差異,可能需要引入一些 xml如下
<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"/>
我用的3.0.4版本不需要引入
源碼我已經上次,位址:http://download.csdn.net/detail/q343814703/9154103