ITPub博客

首页 > Linux操作系统 > Linux操作系统 > CXF 2.0 webservice 学习笔记系列_1 (转)

CXF 2.0 webservice 学习笔记系列_1 (转)

原创 Linux操作系统 作者:it01 时间:2019-06-17 09:27:06 0 删除 编辑
CXF 2.0 webservice 学习笔记系列_1 (转)

对比了一下CXF2 和 AXIS 2 还是选择在框架中集成CXF2。不过CXF2的文档更新的真是慢啊,试了一下,目前CXF2支持的两种绑定方式jaxb2和aegis,看了半天源码总算调通了。又忍不住批一下他的文档,给的都是半调子的例子,希望开发小组能尽快完善啊!

先放一个HelloWold的aegis的配置:
一、server 端:
1. HelloWorld.java

代码
  1. package demo.spring;
  2. import javax.jws.WebService;
  3. @WebService
  4. public interface HelloWorld {
  5. String sayHi(String text);
  6. }

2.HelloWorldImpl.java

代码
  1. package demo.spring;
  2. import javax.jws.WebService;
  3. @WebService(endpointInterface = "demo.spring.HelloWorld")
  4. public class HelloWorldImpl implements HelloWorld {
  5. public String sayHi(String text) {
  6. return "Hello " + text;
  7. }
  8. }

3.beans.xml

代码
  1. <beans xmlns="http://www.springframework.org/schema/beans"
  2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xmlns:jaxws="http://cxf.apache.org/jaxws"
  4. xsi:schemaLocation="
  5. http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd
  6. http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd">
  7. <import resource="classpath:META-INF/cxf/cxf.xml" />
  8. <import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
  9. <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
  10. <bean id="jaxWsServiceFactoryBean"
  11. class="org.apache.cxf.jaxws.support.JaxWsServiceFactoryBean">
  12. <property name="wrapped" value="true" />
  13. <property name="dataBinding" ref="aegisBean" />
  14. bean>
  15. <bean id="aegisBean"
  16. class="org.apache.cxf.aegis.databinding.AegisDatabinding" />
  17. <jaxws:endpoint id="CollectiveServices"
  18. implementor="demo.spring.HelloWorldImpl" address="/HelloWorld">
  19. <jaxws:serviceFactory>
  20. <ref bean="jaxWsServiceFactoryBean"/>
  21. jaxws:serviceFactory>
  22. jaxws:endpoint>
  23. beans>

4.web.xml

代码
  1. <web-app version="2.4" xmlns="http://java.sun.com/xml/ns/j2ee"
  2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
  4. <context-param>
  5. <param-name>contextConfigLocationparam-name>
  6. <param-value>WEB-INF/beans.xmlparam-value>
  7. context-param>
  8. <listener>
  9. <listener-class>
  10. org.springframework.web.context.ContextLoaderListener
  11. listener-class>
  12. listener>
  13. <servlet>
  14. <servlet-name>CXFServletservlet-name>
  15. <servlet-class>
  16. org.apache.cxf.transport.servlet.CXFServlet
  17. servlet-class>
  18. <load-on-startup>1load-on-startup>
  19. servlet>
  20. <servlet-mapping>
  21. <servlet-name>CXFServletservlet-name>
  22. <url-pattern>/ws/*url-pattern>
  23. servlet-mapping>
  24. web-app>

二、客户端

1.ClientTest.java

代码
  1. package demo.spring.client;
  2. import static org.junit.Assert.assertEquals;
  3. import org.apache.cxf.aegis.databinding.AegisDatabinding;
  4. import org.apache.cxf.frontend.ClientProxyFactoryBean;
  5. import org.junit.AfterClass;
  6. import org.junit.BeforeClass;
  7. import org.junit.Test;
  8. import org.springframework.context.support.ClassPathXmlApplicationContext;
  9. import demo.spring.HelloWorld;
  10. public final class ClientTest {
  11. private static ClassPathXmlApplicationContext context;
  12. @BeforeClass
  13. public static void beforeClass() {
  14. context = new ClassPathXmlApplicationContext(
  15. new String[] { "demo/spring/client/client-beans.xml" });
  16. }
  17. @AfterClass
  18. public static void afterClass() {
  19. context = null;
  20. }
  21. @Test
  22. public void testSayHiWithSpringConfig() throws Exception {
  23. HelloWorld client = (HelloWorld) context.getBean("client");
  24. String response = client.sayHi("Joe");
  25. assertEquals("Hello Joe", response);
  26. }
  27. /**
  28. * 这个官方有例子(http://cwiki.apache.org/CXF20DOC/aegis-databinding.html)
  29. *
  30. * @throws Exception
  31. */
  32. @Test
  33. public void testSayHiByCode() throws Exception {
  34. ClientProxyFactoryBean factory = new ClientProxyFactoryBean();
  35. factory.setServiceClass(HelloWorld.class);
  36. factory.setAddress("http://localhost:8080/testCxf/ws/HelloWorld");
  37. factory.getServiceFactory().setDataBinding(new AegisDatabinding());
  38. HelloWorld client = (HelloWorld) factory.create();
  39. assertEquals("Hello Joe", client.sayHi("Joe"));
  40. }
  41. }

2.client_beans.xml(testSayHiWithSpringConfig()中需要使用的配置文件)

代码
  1. <beans xmlns="http://www.springframework.org/schema/beans"
  2. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xmlns:jaxws="http://cxf.apache.org/jaxws"
  4. xsi:schemaLocation="
  5. http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.0.xsd
  6. http://cxf.apache.org/jaxws http://cxf.apache.org/schema/jaxws.xsd">
  7. <bean id="aegisBean"
  8. class="org.apache.cxf.aegis.databinding.AegisDatabinding" />
  9. <bean id="serviceFactoryBean" class="org.apache.cxf.jaxws.support.JaxWsServiceFactoryBean">
  10. <property name="dataBinding" ref="aegisBean" />
  11. bean>
  12. <bean id="client" class="demo.spring.HelloWorld"
  13. factory-bean="clientFactory" factory-method="create" />
  14. <bean id="clientFactory"
  15. class="org.apache.cxf.frontend.ClientProxyFactoryBean">
  16. <property name="serviceFactory" ref="serviceFactoryBean" />
  17. <property name="serviceClass"
  18. value="demo.spring.HelloWorld" />
  19. <property name="address"
  20. value="http://localhost:8080/testCxf/ws/HelloWorld" />
  21. bean>
  22. beans>

来自 “ ITPUB博客 ” ,链接:http://blog.itpub.net/12467/viewspace-148299/,如需转载,请注明出处,否则将追究法律责任。

请登录后发表评论 登录
全部评论

注册时间:2002-05-25

  • 博文量
    489
  • 访问量
    371354