使用Apache CXF创建Web Service

官方主页:http://cxf.apache.org/

下载后内附详细的sample。这里先演示一个最简单的工程。

如下图所示建立工程:

客户端测试代码:TestServiceClient.java

  1. package jp.co.apm.client;
  2. import jp.co.apm.service.TestService;
  3. import org.apache.cxf.frontend.ClientProxyFactoryBean;
  4. public class TestServiceClient {
  5.     public static void main(String[] args) {
  6.         
  7.         ClientProxyFactoryBean factory = new ClientProxyFactoryBean();
  8.         factory.setServiceClass(TestService.class);
  9.         factory.setAddress("http://localhost:8080/APM_CXF/services/test");
  10.         
  11.         TestService service = (TestService) factory.create();
  12.         System.out.println(service.sayHello());
  13.     }
  14. }

TestServiceImpl.java

  1. package jp.co.apm.service.impl;
  2. import jp.co.apm.service.TestService;
  3. public class TestServiceImpl implements TestService {
  4.     public String sayHello() {
  5.         
  6.         return "Hello, Shen Bin";
  7.     }
  8. }

TestService.java

  1. package jp.co.apm.service;
  2. public interface TestService {
  3.     public String sayHello();
  4. }

cxf-servlet.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4.     xmlns:simple="http://cxf.apache.org/simple"
  5.     xmlns:soap="http://cxf.apache.org/bindings/soap"
  6.     xsi:schemaLocation="
  7. http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
  8. http://cxf.apache.org/bindings/soap http://cxf.apache.org/schemas/configuration/soap.xsd
  9. http://cxf.apache.org/simple http://cxf.apache.org/schemas/simple.xsd">
  10.     <simple:server id="testservice" serviceClass="jp.co.apm.service.TestService" address="/test">
  11.         <simple:serviceBean>
  12.             <bean class="jp.co.apm.service.impl.TestServiceImpl" />
  13.         </simple:serviceBean>
  14.     </simple:server>
  15. </beans>

web.xml

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
  3.     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4.     xsi:schemaLocation="http://java.sun.com/xml/ns/javaee 
  5.     http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
  6.     <display-name>APM</display-name>
  7.     <description>APM</description>
  8.     
  9.     <servlet>
  10.         <servlet-name>APM</servlet-name>
  11.         <servlet-class>
  12.             org.apache.cxf.transport.servlet.CXFServlet
  13.         </servlet-class>
  14.         <load-on-startup>2</load-on-startup>
  15.     </servlet>
  16.     
  17.     <servlet-mapping>
  18.         <servlet-name>APM</servlet-name>
  19.         <url-pattern>/services/*</url-pattern>
  20.     </servlet-mapping>
  21.     
  22.     <session-config>
  23.         <session-timeout>60</session-timeout>
  24.     </session-config>
  25.     
  26. </web-app>

启动Tomcat,访问: http://localhost:8080/APM_CXF/services/test?wsdl

运行TestServiceClient.java测试结果。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 3
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值