cxf的许多参数感觉和xfire差不多,因此便不做太多的解释,如果不明白的可以参考之前的xfire搭建来促进理解。
搭建过程如下:
1、使用eclipse创建一个maven项目,maven导包的pom.xml的整体内容如下:
<modelVersion>4.0.0</modelVersion>
<groupId>cxfTest</groupId>
<artifactId>cxfTest</artifactId>
<packaging>war</packaging>
<version>0.0.1-SNAPSHOT</version>
<name>cxfTest Maven Webapp</name>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-core</artifactId>
<version>3.0.4</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.1.6.RELEASE</version>
</dependency>
<dependency>
<groupId>commons-logging</groupId>
<artifactId>commons-logging</artifactId>
<version>1.2</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>3.0.4</version>
</dependency>
</dependencies>
<build>
<finalName>cxfTest</finalName>
</build>
</project>
2、创建webService服务类的接口:
package cxfTest;
import javax.jws.WebService;
@WebService(serviceName = "CxfTestService", targetNamespace = "
http://cxfTest")
public interface CxfTestService {
public String getUserName();
}
3、实现类代码,@Service实际上是spring注解:
package cxfTest.cxfTestImp;
import javax.jws.WebService;
import org.springframework.stereotype.Service;
import cxfTest.CxfTestService;
@Service("CxfTestService")
public class CxfTestServiceImp implements CxfTestService {
@Override
public String getUserName() {
return "name:cxfTest";
}
}
4、cxf配置文件:
<?xml version="1.0" encoding="UTF-8"?>
<context:component-scan base-package="cxfTest"/>
<jaxws:server id="getUserName"
serviceClass="cxfTest.CxfTestService"
address="/CxfTestService">
<jaxws:serviceBean>
<bean
class="cxfTest.cxfTestImp.CxfTestServiceImp"></bean>
</jaxws:serviceBean>
</jaxws:server>
</beans>
这里需要注意的是,一般实际应用时可能会加上下边的代码,这两每当有客户端访问的时候都会把相关的日志打印出来:
<jaxws:inInterceptors>
<bean class="org.apache.cxf.interceptor.LoggingInInterceptor"></bean>
</jaxws:inInterceptors>
<jaxws:outInterceptors>
<bean class="org.apache.cxf.interceptor.LoggingOutInterceptor"></bean>
</jaxws:outInterceptors>
这段代码加在<jaxws:server>和</jaxws:server>中间。
5、web.xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:cxf-context.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>
7、页面正常访问后,我新建一个一个项目,导入了服务端导入的所有的jar包,然后写了个简单的main方法进行测试。需要注意的是,跟xfire一样,这里也需要写一个和服务端一模一样的服务接口(包路径可以不一样),如:
package cxfTest1;
import javax.jws.WebService;
@WebService(serviceName = "CxfTestService", targetNamespace = "
http://cxfTest")
public interface CxfTestService {
public String getUserName();
}
然后对应的模拟客户端调用代码如下:
package cxfTest1;
import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
public class CxfTest {
public static void main(String[] args) {
JaxWsProxyFactoryBean factoryBean = new JaxWsProxyFactoryBean();
// factoryBean.getInInterceptors().add(new LoggingInInterceptor());
// factoryBean.getOutInterceptors().add(new LoggingOutInterceptor());
factoryBean.setServiceClass(CxfTestService.class);
factoryBean
CxfTestService impl = (CxfTestService) factoryBean.create();
System.out.println(impl.getUserName());
}
}
上边被注释的代码也是日志相关的,加不加都不影响主要业务功能,可以自己选择用还是不用。执行main方法后控制台输出如下: