CXF使用参考指南

xiaoxiao2021-02-28  52

文章背景:由于项目要与其他单位对接,所以原技术方案采用的是spring-ws发布服务,本地测试无问题,但是与其他单位联调接口的时候,发现接口通信正常,但是传参无法获取,直白点就是快递员拿着包装好的快递找到了你家,但是快递路上丢了;分析原因可能是由于spring-ws传参的时候会把所有参数封装成一个request类,但是别的单位传参的时候又没有直接set进这个类的变量,所以接收参数为空,所以更改了技术方案,由spring-ws改为cxf,以下是使用的经验文档,供各位参考。如果有大牛能解决上述的spring-ws的问题也可以底部留言,谢谢。

1.关于cxf

1.1关于框架:

 

Apache CXF 是一个开源的 Services 框架,CXF 帮助您利用 Frontend 编程 API 来构建和开发 Services ,像 JAX-WS 。这些 Services 可以支持多种协议,比如:SOAPXML/HTTPRESTful HTTP 或者 CORBA ,并且可以在多种传输协议上运行,比如:HTTPJMS 或者 JBICXF 大大简化了 Services 的创建,同时它继承了 XFire 传统,一样可以天然地和 Spring 进行无缝集成。

1.2 CXF 框架支撑环境:

CXF 框架是一种基于 Servlet 技术的 SOA 应用开发框架,要正常运行基于 CXF 应用框架开发的企业应用,除了 CXF 框架本身之外,还需要 JDK 和 Servlet 容器的支持。

1.3:功能特性:

支持 Web Services 标准:CXF 支持多种 Web Services 标准,包含 SOAPBasic ProfileWS-AddressingWS-PolicyWS-ReliableMessaging  WS-SecurityFrontendsCXF 支持多种“Frontend”编程模型,CXF 实现了JAX-WS API (遵循 JAX-WS 2.0 TCK 版本),它也包含一个“simple frontend”允许客户端和 EndPoint 的创建,而不需要 Annotation 注解。CXF 既支持 WSDL优先开发,也支持从 Java 的代码优先开发模式。容易使用: CXF 设计得更加直观与容易使用。有大量简单的 API 用来快速地构建代码优先的 Services,各种 Maven 的插件也使集成更加容易,支持 JAX-WS API ,支持 Spring 2.0 更加简化的 XML 配置方式,等等。支持二进制和遗留协议:CXF 的设计是一种可插拨的架构,既可以支持 XML ,也可以支持非 XML 的类型绑定,比如:JSON 和 CORBA

 

发布接口实例配置参考

2.1 配置pom

文本框中为CXFjar,同时还要配置springjar,cxf版本尽量选择3以上,测试spring4以上版本容易和cxf3以下的版本不兼容.

备注:这里特别需要说明的是,cxf 3.0以后的版本只能在jdk1.7上使用,如果在1.6使用的话,会直接报错的。

 

 

<cxf.version>3.1.6</cxf.version>

 

<!-- CXF Dependencies --><dependency>    <groupId>org.apache.cxf</groupId>    <artifactId>cxf-rt-frontend-jaxws</artifactId>    <version>${cxf.version}</version></dependency><dependency>    <groupId>org.apache.cxf</groupId>    <artifactId>cxf-rt-transports-http</artifactId>    <version>${cxf.version}</version></dependency><!-- Jetty is needed if you're are not using the CXFServlet --><dependency>    <groupId>org.apache.cxf</groupId>    <artifactId>cxf-rt-transports-http-jetty</artifactId>    <version>${cxf.version}</version></dependency><!-- End of CXF Dependencies -->

 

2.2 配置web.xml

<!-- 用来声明一个servlet,CXFServlet是 --><servlet>    <servlet-name>cxf</servlet-name>    <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>    <load-on-startup>2</load-on-startup></servlet><servlet-mapping>    <servlet-name>cxf</servlet-name>    <url-pattern>/webService/*</url-pattern</servlet-mapping>

 

2.3 配置spring.xml

以为cxf要依赖spring的容器,所以cxf的配置可以直接在spring.xml中配置,但是为了代码的可读性,建议新建一个cxf.xml的配置文件,springimport.

<import resource="classpath:cxf.xml"/>

Cxf.xml: 版本3以下的还需要import一个cxf中的soap配置文件

<beans xmlns="http://www.springframework.org/schema/beans"       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"       xmlns:jaxws="http://cxf.apache.org/jaxws"       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">    <!-- Import Apache CXF Bean Definition -->    <import resource="classpath:META-INF/cxf/cxf.xml"/>    <import resource="classpath:META-INF/cxf/cxf-servlet.xml"/>

 

<!--接口发布-->

<!--先注入实现类的bean--><bean id="demoImpl" class="com.anyuan.server.impl.HelloImpl"></bean><jaxws:endpoint id="demoService" implementor="#demoImpl"                address="/demoService"/>

<!--publishedEndpointUrl可以强制修改wsdl的路径,如果未加此参数,默认地址就是 http://域名.service.demoService-->

 

2.4 编写接口java代码

 

import javax.jws.WebParam;import javax.jws.WebResult;import javax.jws.WebService;@WebService(name = "IHello",targetNamespace = "http://ehs.demo.ay.com")public interface IHello {    public @WebResult(name = "result") String sayHello(@WebParam(name = "name") String name);}

 

2.5 编写接口实现类

import javax.jws.WebService;@WebService(targetNamespace = "http://ehs.demo.ay.com",endpointInterface = "com.anyuan.server.IHello")public class HelloImpl implements IHello {    public String sayHello(String name) {        return "hello  with  "+ name;    }}

<!--@WebService中的endpointInterface 不可少,映射的是接口-->

 

2.6 包下增加一个package-info.java,用于配置elementFormDefault和attributeFormDefault

@javax.xml.bind.annotation.XmlSchema(namespace = "http://ehs.portal.ay.com",    elementFormDefault = javax.xml.bind.annotation.XmlNsForm.QUALIFIED, attributeFormDefault = XmlNsForm.UNQUALIFIED)package com.ay.custom.demo.soap;import javax.xml.bind.annotation.XmlNsForm;

 

2.7 启动tomcat

访问路径为http://localhost:8080/webService/demoService?wsdl

Wsdl如下:

 

This XML file does not appear to have any style information associated with it. The document tree is shown below.

<wsdl:definitions xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:tns="http://ehs.demo.ay.com" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:ns1="http://schemas.xmlsoap.org/soap/http" name="HelloImplService"targetNamespace="http://ehs.demo.ay.com">

<wsdl:types>

<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:tns="http://ehs.demo.ay.com" elementFormDefault="qualified" targetNamespace="http://ehs.demo.ay.com" version="1.0">

<xs:element name="sayHello" type="tns:sayHello"/>

<xs:element name="sayHelloResponse" type="tns:sayHelloResponse"/>

<xs:complexType name="sayHello">

<xs:sequence>

<xs:element minOccurs="0" name="name" type="xs:string"/>

</xs:sequence>

</xs:complexType>

<xs:complexType name="sayHelloResponse">

<xs:sequence>

<xs:element minOccurs="0" name="result" type="xs:string"/>

</xs:sequence>

</xs:complexType>

</xs:schema>

</wsdl:types>

<wsdl:message name="sayHelloResponse">

<wsdl:part element="tns:sayHelloResponse" name="parameters"></wsdl:part>

</wsdl:message>

<wsdl:message name="sayHello">

<wsdl:part element="tns:sayHello" name="parameters"></wsdl:part>

</wsdl:message>

<wsdl:portType name="IHello">

<wsdl:operation name="sayHello">

<wsdl:input message="tns:sayHello" name="sayHello"></wsdl:input>

<wsdl:output message="tns:sayHelloResponse" name="sayHelloResponse"></wsdl:output>

</wsdl:operation>

</wsdl:portType>

<wsdl:binding name="HelloImplServiceSoapBinding" type="tns:IHello">

<soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>

<wsdl:operation name="sayHello">

<soap:operation soapAction="" style="document"/>

<wsdl:input name="sayHello">

<soap:body use="literal"/>

</wsdl:input>

<wsdl:output name="sayHelloResponse">

<soap:body use="literal"/>

</wsdl:output>

</wsdl:operation>

</wsdl:binding>

<wsdl:service name="HelloImplService">

<wsdl:port binding="tns:HelloImplServiceSoapBinding" name="HelloImplPort">

<soap:address location="http://localhost:8080/webService/demoService"/>

</wsdl:port>

</wsdl:service>

</wsdl:definitions>

 

 

3 client端实例

3.1 方法一

public static void main(String[] args) {    JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();    String wsUrl = "http://localhost:8080/webService/demoService?wsdl";// webservice的wsdl地址    Client client = dcf.createClient(wsUrl);    String method = "sayHello";// webservice的方法名    Object[] result = null;    try {        result = client.invoke(method, "td");// 调用webservice        System.out.println(result[0]);    } catch (Exception e) {        e.printStackTrace();    }}

 

3.2 方法二

注:以下方法都需要使用cxf工具生成客户端java代码

下载网站:http://cxf.apache.org/download.html

 

下载后解压,设置环境变量

# set cxf environment

export CXF_HOME=/opt/soft/apache-cxf-3.1.15

export PATH=$CXF_HOME/bin:$PATH

export CLASSPATH=.:$JAVA_HOME/lib:$JRE_HOME/lib:$CXF_HOME/lib

出现如下信息表示配置成功

 

在命令中输入:

wsdl2java http://localhost:8080/webService/demoService?wsdl

 

// client第二种方法public static void main(String[] args) {    HelloImplService factory=new HelloImplService();    IHello iHello=factory.getHelloImplPort();    String result = iHello.sayHello("td");    System.out.println(result);}

 

3.3 方法三

// client第三种方法public static void main(String[] args) {    JaxWsProxyFactoryBean factoryBean = new JaxWsProxyFactoryBean();    factoryBean.getInInterceptors().add(new LoggingInInterceptor());    factoryBean.getOutInterceptors().add(new LoggingOutInterceptor());    factoryBean.setServiceClass(IHello.class);    factoryBean.setAddress("http://localhost:8080/webService/demoService?wsdl");    IHello impl = (IHello) factoryBean.create();    String result = impl.sayHello("td");    System.out.println(result);}

 

3.4 方法四

// client第四种方法

增加cxf-client.xml的配置文件

<?xml version="1.0" encoding="UTF-8"?><beans xmlns="http://www.springframework.org/schema/beans"       xmlns:jaxws="http://cxf.apache.org/jaxws"       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"       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">    <jaxws:client id="client"                  serviceClass="com.ay.demo.ehs.IHello"                  address="http://localhost:8080/webService/demoService">    </jaxws:client></beans>

 在spring.xml中注入

 

public static void main(String[] args) {    ApplicationContext applicationContext=new ClassPathXmlApplicationContext("spring.xml");    IHello iHello= (IHello) applicationContext.getBean("client");    String result = iHello.sayHello("td");    System.out.println(result);}

官方网站

官方网站:http://cxf.apache.org/

转载请注明原文地址: https://www.6miu.com/read-2622290.html

最新回复(0)