使用 CXF 生成 SOAP 1.2 端点的 Java Spring Boot

Posted

技术标签:

【中文标题】使用 CXF 生成 SOAP 1.2 端点的 Java Spring Boot【英文标题】:Java Spring Boot with CXF generating SOAP 1.2 endpoint 【发布时间】:2018-06-13 19:34:48 【问题描述】:

几天来,我一直在尝试将 spring boot 与 Apache CXF 一起使用来生成 SOAP 1.2 端点,但是即使 WSDL 不使用 SOAP 1.1 命名空间,spring 也会继续生成 SOAP 1.1 和 SOAP 1.2 端点在同一个位置!

我的 wsdl 定义只有一个 SOAP 1.2 的端点

<wsdl:service name="MyService"> 
  <wsdl:port name="IMyServicePort" binding="tns:IMyServiceBinding"> 
    <soap12:address location="http://localhost:8080/MyService/services/IMyServicePort"/> 
  </wsdl:port> 
</wsdl:service> 

Web 服务 bean 文件包含以下内容;

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:util="http://www.springframework.org/schema/util" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:jaxws="http://cxf.apache.org/jaxws" 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://www.springframework.org/schema/util
   http://www.springframework.org/schema/util/spring-util-2.0.xsd
   http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd
   http://cxf.apache.org/bindings/soap http://cxf.apache.org/schemas/configuration/soap.xsd">

<!-- Import the necessary CXF configuration files -->
<import resource="classpath:META-INF/cxf/cxf.xml" />
<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />

<bean id="messageFactory" class="org.springframework.ws.soap.saaj.SaajSoapMessageFactory">
    <property name="soapVersion">
        <util:constant static-field="org.springframework.ws.soap.SoapVersion.SOAP_12" />
    </property>
    <property name="messageFactory">
        <null />
    </property>
</bean>


<jaxws:endpoint id="service" implementor="#ImyServiceImpl"
    address="/myService/v1" wsdlLocation="wsdl/MyService.wsdl">
    <jaxws:binding>
        <soap:soapBinding mtomEnabled="true" version="1.2"/>
    </jaxws:binding>
    <jaxws:properties>
        <entry key="schema-validation-enabled" value="true" />
        <entry key="jaxb-validation-event-handler">
            <bean
                class="myservice.OutSoapFaultInterceptor"></bean>
        </entry>
    </jaxws:properties>
</jaxws:endpoint>
</beans>

但是,当我浏览到 wsdl 时,我看到了 SOAP 1.1 和 SOAP 1.2 端点

<wsdl:service name="MyService"> 
  <wsdl:port binding="tns:IMyServiceBinding" name="IMyServicePort"> 
    <soap12:address location="http://localhost:8080/services/services/myservice/v1"/> 
  </wsdl:port> 
</wsdl:service> 
<wsdl:service name="IMyServiceService"> 
  <wsdl:port binding="tns:IMyServiceSoapBinding" name="IMyServicePort"> 
    <soap:address location="http://localhost:8080/services/services/myservice/v1"/> 
  </wsdl:port> 
</wsdl:service> 

令人讨厌的是,两者都定义为相同的端点位置,因此我无法访问 SOAP 1.2 端点,所有请求都被拒绝,并显示“发送到仅 SOAP 1.1 的端点时,SOAP 1.2 消息无效”。

我可以通过在 Java 中定义端点来解决这个问题(尽管我不知道如何在 Java 代码中复制 jaxb-validation-event-handler!),但我宁愿使用 XML 配置。

是否有人建议只生成一个 SOAP 1.2 端点,或者知道如何分离端点位置以便我可以将请求发送到 SOAP 1.2 端点?

【问题讨论】:

【参考方案1】:

好吧,我不知道为什么 WSDL 会生成 SOAP 1.1 接口...但是,为了解决我的问题,我刚刚从 jaxws:endpoint 定义中删除了 wsdlLocation,所以;

<jaxws:endpoint id="service" implementor="#ImyServiceImpl"
    address="/myService/v1" wsdlLocation="wsdl/MyService.wsdl">

成为;

<jaxws:endpoint id="service" implementor="#ImyServiceImpl"
    address="/myService/v1">

如果我浏览到 /myService/v1,现在那里只有一个 SOAP 1.2 定义。

【讨论】:

以上是关于使用 CXF 生成 SOAP 1.2 端点的 Java Spring Boot的主要内容,如果未能解决你的问题,请参考以下文章

关于webservice(CXF)的一些理解

java使用CXF做的webservice服务端,怎么支持soap1.2。

使用amq处理cxf请求

CXF方式搭建本地webservice服务和soap方式调用踩坑

apache CXF wsdl2java工具的使用

CXF:通过现有的 Servlet 传输提供解耦端点