Package feign.soap

Class SOAPEncoder

  • All Implemented Interfaces:
    feign.codec.Encoder

    public class SOAPEncoder
    extends Object
    implements feign.codec.Encoder
    Encodes requests using SOAPMessage and JAXB for the body part.

    Basic example with Feign.Builder:

    
     public interface MyApi {
    
        @RequestLine("POST /getObject")
        @Headers({
          "SOAPAction: getObject",
          "Content-Type: text/xml"
        })
        MyJaxbObjectResponse getObject(MyJaxbObjectRequest request);
    
     }
    
     ...
    
     JAXBContextFactory jaxbFactory = new JAXBContextFactory.Builder()
         .withMarshallerJAXBEncoding("UTF-8")
         .withMarshallerSchemaLocation("http://apihost http://apihost/schema.xsd")
         .build();
    
     api = Feign.builder()
         .encoder(new SOAPEncoder(jaxbFactory))
         .target(MyApi.class, "http://api");
    
     ...
    
     try {
        api.getObject(new MyJaxbObjectRequest());
     } catch (SOAPFaultException faultException) {
        log.info(faultException.getFault().getFaultString());
     }
     

    The JAXBContextFactory should be reused across requests as it caches the created JAXB contexts.

    • Constructor Detail

      • SOAPEncoder

        public SOAPEncoder​(feign.jaxb.JAXBContextFactory jaxbContextFactory)
    • Method Detail

      • encode

        public void encode​(Object object,
                           Type bodyType,
                           feign.RequestTemplate template)
        Specified by:
        encode in interface feign.codec.Encoder
      • modifySOAPMessage

        protected jakarta.xml.soap.SOAPMessage modifySOAPMessage​(jakarta.xml.soap.SOAPMessage soapMessage)
                                                          throws jakarta.xml.soap.SOAPException
        Override this in order to modify the SOAP message object before it's finally encoded.
        This might be useful to add SOAP Headers, which are not supported by this SOAPEncoder directly.
        This is an example of how to add a security header: protected SOAPMessage modifySOAPMessage(SOAPMessage soapMessage) throws SOAPException { SOAPFactory soapFactory = SOAPFactory.newInstance(); String uri = "http://schemas.xmlsoap.org/ws/2002/12/secext"; String prefix = "wss"; SOAPElement security = soapFactory.createElement("Security", prefix, uri); SOAPElement usernameToken = soapFactory.createElement("UsernameToken", prefix, uri); usernameToken.addChildElement("Username", prefix, uri).setValue("test"); usernameToken.addChildElement("Password", prefix, uri).setValue("test"); security.addChildElement(usernameToken); soapMessage.getSOAPHeader().addChildElement(security); return soapMessage; }
        Throws:
        jakarta.xml.soap.SOAPException