cxf-spring-pratice-service

来源:互联网 发布:sql 自增id 重置 编辑:程序博客网 时间:2024/06/07 02:45

其实吧,这篇博客基本上就是照搬了官方的How-to.

2017/11/15 时隔六个月, 重新下组织格式和内容.

诸如webservice,WSDL的定义及作用,还有意义啥的咱就不再废话了,直接上干货。

1. 引入cxf相关的依赖

1.1 使用maven
<dependency>    <groupId>org.apache.cxf</groupId>    <artifactId>cxf-rt-frontend-jaxws</artifactId>    <version>3.1.1</version></dependency><dependency>    <groupId>org.apache.cxf</groupId>    <artifactId>cxf-rt-transports-http</artifactId>    <version>3.1.1</version></dependency>

1.2 直接引入相关jar

真心不建议采用这种方式!

asm-5.0.4.jarcxf-core-3.1.1.jarcxf-rt-bindings-soap-3.1.1.jarcxf-rt-bindings-xml-3.1.1.jarcxf-rt-databinding-jaxb-3.1.1.jarcxf-rt-frontend-jaxws-3.1.1.jarcxf-rt-frontend-simple-3.1.1.jarcxf-rt-transports-http-3.1.1.jarcxf-rt-ws-addr-3.1.1.jarcxf-rt-ws-policy-3.1.1.jarcxf-rt-wsdl-3.1.1.jarjaxb-core-2.2.11.jarjaxb-impl-2.2.11.jarneethi-3.0.3.jarstax2-api-3.1.4.jarwoodstox-core-asl-4.4.1.jarwsdl4j-1.6.3.jarxml-resolver-1.2.jarxmlschema-core-2.2.1.jar

2. 创建相关类

2.1 IHelloWorld接口

@WebServiceinterface IHelloWorld {    String sayHello(@WebParam(name = "username") String username);    Map<String, Object> getObj(@WebParam(name = "username")String username, @WebParam(name = "sexy")String sexy);}   

2.2 HelloWorldImpl实现类

public class HelloWorldImpl implements IHelloWorld {      @Override      public String sayHello(String username) {          return "Hello " + username;      }      @Override    public Map<String, Object> getObj(String username, String sexy) {        Map<String, Object> customer = new HashMap<String, Object>();        customer.put("NAME", username);        customer.put("SEXY", sexy);        customer.put("BIRTHDAY", Calendar.getInstance().getTime());        return customer;    }}

3. Spring配置

spring-cxf-service.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"    xmlns:http-conf="http://cxf.apache.org/transports/http/configuration"    xsi:schemaLocation="http://www.springframework.org/schema/beans           http://www.springframework.org/schema/beans/spring-beans.xsd             http://cxf.apache.org/jaxws           http://cxf.apache.org/schemas/jaxws.xsd          http://cxf.apache.org/transports/http/configuration              http://cxf.apache.org/schemas/configuration/http-conf.xsd">    <import resource="classpath:META-INF/cxf/cxf.xml" />    <import resource="classpath:META-INF/cxf/cxf-servlet.xml" />    <jaxws:endpoint id="helloService"        implementor="com.kq.webservice.impl.HelloWorldImpl"        address="/helloService" /></beans>

4. web.xml

<!-- 载入上面配置的spring-cxf-service.xml文件 --><context-param>    <param-name>contextConfigLocation</param-name>    <param-value>        classpath:config/spring.xml;        <!-- cxf配置,必须在这里;这里涉及到的是SpringMVC中child,parent container的加载问题 -->        classpath:config/spring-cxf-service.xml;      </param-value></context-param><!-- 配置cxf相关的servlet --><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>/webservice/*</url-pattern></servlet-mapping>

5. 访问

  1. 获取该路径所有的服务列表

    // 格式http://host:port/context_path/services// samplehttp://localhost:9090/SpringMVCDemo/webservice
  2. 读取wsdl文件内容

    // 格式http://host:port/context_path/services/serviceName?wsdl// samplehttp://localhost:9090/SpringMVCDemo/webservice/helloService?wsdl

6. 测试

6.1 单独的测试项目

  1. 引入相同的cxf相关依赖.
  2. 唯一需要注意的是webservice接口名彼此必须一模一样.

6.2 基于SoapUI进行接口测试

  1. 提取出指定的wsdl文件,按照上面的方式提取出指定的wsdl内容,复制到本地的文件中.
  2. 使用提取到的wsdl文件创建soapUI工程.

6.3 编写测试类

此种方式比较方便,无须配置比较灵活,大部分情况下都会采用这种方式调用服务程序。

  1. 返回简单的string

    private static void test_returnSimpleString(){    JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();    factory.setServiceClass(IHelloWorld.class);    factory.setAddress("http://localhost:8080/JYXT/webservice/helloService");    IHelloWorld client = (IHelloWorld) factory.create();    String response = client.sayHello("LQ");    System.out.println("Response:" + response);     }
  2. 返回复杂对象

    private static void test_returnCustomObj(){    JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();    factory.setServiceClass(IHelloWorld.class);    factory.setAddress("http://localhost:8080/JYXT/webservice/helloService");    IHelloWorld client = (IHelloWorld) factory.create();    Map<String, Object> obj = client.getObj("LQ","1");    System.out.println(obj);    }   
  3. 返回JSON

    未完成

7. 参考链接

  1. http://cxf.apache.org/docs/writing-a-service-with-spring.html - 官方文档

  2. http://blog.csdn.net/blueheart20/article/details/42971713

    1. 有几处问题.基本是因为cxf的版本问题导致的。
    2. 关于webservice相关的理论可以在此文章中看看。
0 0
原创粉丝点击