Apache CXF: A SOAP 1.2 message is not valid when sent to a SOAP 1.1 only endpoint

18,177

Solution 1

You can use JaxWsClientFactoryBean to create client. It will have option to setBinding to Soap 1.2.

JaxWsClientFactoryBean factory = new JaxWsClientFactoryBean();
factory.setServiceClass(MyServiceInterface.class);
factory.setAddress("myEndpoint");
List<Interceptor<? extends Message>> interceptors = new ArrayList<Interceptor<? extends Message>>();
interceptors.add(new HeaderOutInterceptor());
factory.setOutInterceptors(interceptors);
factory.setBindingId("http://www.w3.org/2003/05/soap/bindings/HTTP/");//soap 1.2
MyServiceInterface service = (MyServiceInterface) factory.create();

Solution 2

geg already said it, I just want to make it more visible:

import javax.xml.ws.soap.SOAPBinding;
...
factory.setBindingId(SOAPBinding.SOAP12HTTP_BINDING);
Share:
18,177
Aurasphere
Author by

Aurasphere

Crafting high quality bugs and deploying them in production environments only on Friday's evening since 2013.

Updated on June 22, 2022

Comments

  • Aurasphere
    Aurasphere almost 2 years

    first of all, I know that there are already some questions on SO about this topic but none of them solved my problem (or I am too stupid to understand them, that's a possibility as well).

    So, I have a WSDL. From the WSDL I've generated a Java client using Eclipse CXF plugin. Now I'm doing this:

    JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
    factory.setServiceClass(MyServiceInterface.class);
    factory.setAddress("myEndpoint");
    List<Interceptor<? extends Message>> interceptors = new ArrayList<Interceptor<? extends Message>>();
    interceptors.add(new HeaderOutInterceptor());
    factory.setOutInterceptors(interceptors);
    
    MyServiceInterface service = (MyServiceInterface) factory.create();
    

    The interceptor only adds an header to the requests I'm sending through the client:

    message.put(Message.CONTENT_TYPE, "application/soap+xml");
    

    I'm adding this manually since by default the content type is text/xml and I get a 415 error.

    The problem is that with this configuration I get this exception:

    org.apache.cxf.binding.soap.SoapFault: A SOAP 1.2 message is not valid when sent to a SOAP 1.1 only endpoint.
    at org.apache.cxf.binding.soap.interceptor.ReadHeadersInterceptor.handleMessage(ReadHeadersInterceptor.java:178)
    at org.apache.cxf.binding.soap.interceptor.ReadHeadersInterceptor.handleMessage(ReadHeadersInterceptor.java:69)
    at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:308)
    

    I've tried adding this annotation to the generated client interface:

    @BindingType(javax.xml.ws.soap.SOAPBinding.SOAP12HTTP_BINDING)
    

    But nothing changed. Can anybody help me?

    EDIT

    I've added a cxf.xml file under the classpath. This is the content:

    <?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: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://cxf.apache.org/bindings/soap 
           http://cxf.apache.org/schemas/configuration/soap.xsd
           http://cxf.apache.org/jaxws 
           http://cxf.apache.org/schemas/jaxws.xsd">
    
    <jaxws:endpoint serviceName="ClabService" endpointName="ClabServicePort">
        <jaxws:binding>
            <soap:soapBinding version="1.2" mtomEnabled="true" />
        </jaxws:binding>
    </jaxws:endpoint>
    
    </beans>
    

    However, now I get this exception:

    Exception in thread "main" java.lang.RuntimeException: org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'org.apache.cxf.jaxws.EndpointImpl---51955260': Invocation of init method failed; nested exception is javax.xml.ws.WebServiceException: org.apache.cxf.service.factory.ServiceConstructionException: serviceClass must be set to a valid service interface or class
    

    I've tried to add this during the factory configuration:

    factory.setServiceClass(MyServiceInterface_Service.class);
    

    but nothing changed.

    • Vijendra Kumar Kulhade
      Vijendra Kumar Kulhade over 7 years
      Need more clarification on this. What is the end point you are trying to hit?
    • Aurasphere
      Aurasphere over 7 years
      The endpoint works fine, if I use SOAP UI I get a correct response back. I think the problem is that CXF is generating a SOAP 1.1 client meanwhile the server response is 1.2.
    • Vijendra Kumar Kulhade
      Vijendra Kumar Kulhade over 7 years
      if you are getting namespace as SOAP_NAMESPACE = "http://www.w3.org/2003/05/soap-envelope". You are on soap 1.2. you can check that once? Exception says otherwise.
    • Aurasphere
      Aurasphere over 7 years
      I'm not sure how to check that on CXF but using SOAP UI I get that namespace back.
    • Vijendra Kumar Kulhade
      Vijendra Kumar Kulhade over 7 years
      @BindingType(javax.xml.ws.soap.SOAPBinding.SOAP12HTTP_BINDIN‌​G) doesn't work for me when I tried putting this on my client. I changed the version from xml like <jaxws:binding> <soap:soapBinding version="1.2" mtomEnabled="true" /> </jaxws:binding>. you can try changing the version through xml instead of annotation. I fee the problem is you are not not able to set cxf soap version to 1.2.
    • Aurasphere
      Aurasphere over 7 years
      Ok, but I don't have an xml configuration since I'm not using Spring. Can I do that anyway? If so, how? I've only found spring tutorials. Thank you!
    • Vijendra Kumar Kulhade
      Vijendra Kumar Kulhade over 7 years
      Follow this post to change soap version stackoverflow.com/questions/4248098/…
    • Aurasphere
      Aurasphere over 7 years
      I've already tried what that question says (the annotation comes from there) without success. I've updated the question with my most recent tries. Thank you.
  • Robert Strauch
    Robert Strauch over 7 years
    Adding this line worked for me: factory.setBindingId("http://www.w3.org/2003/05/soap/binding‌​s/HTTP/");
  • geg
    geg over 7 years
    Available bindings can be found in the javax.xml.ws.soap.SOAPBinding interface
  • MindGame
    MindGame almost 6 years
    Do you provide a complete example of this please? I'm trying to change the SOAP port but with no luck. stackoverflow.com/questions/50141966/…
  • aliopi
    aliopi almost 6 years
    Use the code from the accepted answer above (stackoverflow.com/a/39405537/332788) and then add the line factory.setBindingId(SOAPBinding.SOAP12HTTP_BINDING); before MyServiceInterface service = (MyServiceInterface) factory.create();
  • aliopi
    aliopi almost 6 years
    Wait, I looked again, he already has that line, only he didn't used the constant factory.setBindingId("http://www.w3.org/2003/05/soap/binding‌​s/HTTP/");//soap 1.2