JAXB unmarshal to object and add raw xml

174 Views Asked by At

In our java/spring project we are using JAXB 2.0 in combination with JAX-WS to recieve some SOAP requests. The goal is to not only unmarshal the payload to java beans, but also save the original xml message on that said java bean. Is there a way to achieve this? Pluspoint: if the solution works together with jaxws-maven-plugin wsimport goal.

1

There are 1 best solutions below

0
Georg Braun On BEST ANSWER

One can make a soap hanlder that extracts the raw payload

public class SoapRawPayloadHandler implements SOAPHandler<SOAPMessageContext> {
    
    @Override
    public boolean handleMessage(SOAPMessageContext smc) {

        ByteArrayOutputStream rawPayloadStream = new ByteArrayOutputStream())
        smc.getMessage().writeTo(rawPayloadStream);
        smc.put(ORIGINAL_MESSAGE, rawPayloadStream.toByteArray());
        smc.setScope(ORIGINAL_MESSAGE, MessageContext.Scope.APPLICATION);
        
        return true;
    }
}

When the Endpoint is created the handler can be passed in the SoapController, and the WebserviceContext must be injected into the SoapController

@Resource private WebServiceContext webServiceContext;

The controller method then has access to the payload.

byte[] payload = (byte[]) webServiceContext.getMessageContext().get(ORIGINAL_MESSAGE);