Printing XML SOAP Request from Spring Boot client application to the console

Printing XML SOAP Request from Spring Boot client application to the console

by Miljan Puletic -
Number of replies: 0

As title says, I want string representation of request, which I'm initiating towards SOAP Web Service.

How I'm sending the request to the web service:

First, from Swagger I initiate request toward my controller:


@RequestMapping(value = "/helloworld", method = RequestMethod.POST)
@ResponseBody
public HelloWorldResponse helloworldRequest(@RequestParam(value = "request") String request) {

    HelloWorldResponse response = helloWorldClient.getValue(request);

    return response;
}

Then in my Service layer, I have:

@Service
public class HelloWorldClient extends WebServiceGatewaySupport {

    public HelloWorldResponse getValue(String myValue) {

        ObjectFactory objectFactory = new ObjectFactory();
        HelloWorld request = objectFactory.createHelloWorld();
        JAXBElement<String> value = objectFactory.createHelloWorldMyValue(myValue);
        request.setMyValue(value);

        HelloWorldResponse response = (HelloWorldResponse) getWebServiceTemplate()
                .marshalSendAndReceive("https://clienttesthorizon.horizonafs.com/AFSServices/AFSService.svc/basicHttpBinding", 
                        request, new SoapActionCallback("http://tempuri.org/IAFSService/HelloWorld"));

        return response;
    }

}
Accompanying @Configuration class for this service is:

@Configuration
public class HelloWorldConfiguration {

    @Bean
    public Jaxb2Marshaller marshaller() {
        Jaxb2Marshaller marshaller = new Jaxb2Marshaller();
        // this package must match the package in the <generatePackage> specified in
        // pom.xml
        marshaller.setContextPath("com.zesium.arvato");
        return marshaller;
    }

    @Bean
    public HelloWorldClient helloWorldClient(Jaxb2Marshaller marshaller) {
        HelloWorldClient client = new HelloWorldClient();
        client.setDefaultUri(""); //for HelloWorld -> http://tempuri.org/IAFSService/HelloWorld 
        client.setMarshaller(marshaller);
        client.setUnmarshaller(marshaller);
        return client;
    }

}

HelloWorld and HelloWorldResponse are classes generated by Maven JAXB2 plugin. This particular service is working. Basically, I pass string: John, I got a response from that web service: `Hello World John.

My goal is to print out actual string of how request looks like just before it is sent off.

So far I've tried two approaches:

Approach 1

In application.properties I set following lines:

log4j.rootCategory=INFO, stdout
log4j.logger.org.springframework.ws.client.MessageTracing.sent=TRACE
log4j.logger.org.springframework.ws.client.MessageTracing.received=TRACE

Nothing is printed to the console.

Approach 2

In the same package where my controller (from above) is, I have following class:

@Component
public class MyFilter extends OncePerRequestFilter {

    @Override
    protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain)
            throws ServletException, IOException {
        
        ContentCachingRequestWrapper requestWrapper = new ContentCachingRequestWrapper(request);
        ContentCachingResponseWrapper responseWrapper = new ContentCachingResponseWrapper(response);
       
        filterChain.doFilter(requestWrapper, responseWrapper);
        
        byte[] resquestArray = requestWrapper.getContentAsByteArray();
        String resquestStr = new String(resquestArray, requestWrapper.getCharacterEncoding());
        
        System.out.println(resquestStr);
        
        if(resquestStr.isEmpty()) System.out.println("this get printed");
        
    }
}


Average of ratings: -