下载后内附详细的sample。这里先演示一个最简单的工程。
如下图所示建立工程:
客户端测试代码:TestServiceClient.java
- package jp.co.apm.client;
- import jp.co.apm.service.TestService;
- import org.apache.cxf.frontend.ClientProxyFactoryBean;
- public class TestServiceClient {
- public static void main(String[] args) {
- ClientProxyFactoryBean factory = new ClientProxyFactoryBean();
- factory.setServiceClass(TestService.class);
- factory.setAddress("http://localhost:8080/APM_CXF/services/test");
- TestService service = (TestService) factory.create();
- System.out.println(service.sayHello());
- }
- }
TestServiceImpl.java
- package jp.co.apm.service.impl;
- import jp.co.apm.service.TestService;
- public class TestServiceImpl implements TestService {
- public String sayHello() {
- return "Hello, Shen Bin";
- }
- }
TestService.java
- package jp.co.apm.service;
- public interface TestService {
- public String sayHello();
- }
cxf-servlet.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:simple="http://cxf.apache.org/simple"
- xmlns:soap="http://cxf.apache.org/bindings/soap"
- xsi:schemaLocation="
- http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
- http://cxf.apache.org/bindings/soap http://cxf.apache.org/schemas/configuration/soap.xsd
- http://cxf.apache.org/simple http://cxf.apache.org/schemas/simple.xsd">
- <simple:server id="testservice" serviceClass="jp.co.apm.service.TestService" address="/test">
- <simple:serviceBean>
- <bean class="jp.co.apm.service.impl.TestServiceImpl" />
- </simple:serviceBean>
- </simple:server>
- </beans>
web.xml
- <?xml version="1.0" encoding="UTF-8"?>
- <web-app version="2.5" 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_2_5.xsd">
- <display-name>APM</display-name>
- <description>APM</description>
- <servlet>
- <servlet-name>APM</servlet-name>
- <servlet-class>
- org.apache.cxf.transport.servlet.CXFServlet
- </servlet-class>
- <load-on-startup>2</load-on-startup>
- </servlet>
- <servlet-mapping>
- <servlet-name>APM</servlet-name>
- <url-pattern>/services/*</url-pattern>
- </servlet-mapping>
- <session-config>
- <session-timeout>60</session-timeout>
- </session-config>
- </web-app>
启动Tomcat,访问: http://localhost:8080/APM_CXF/services/test?wsdl
运行TestServiceClient.java测试结果。