Spring WS WebServicesTemplate / Jaxb2Marshaller client view raw xml?

Can I view the request and response for the Spring WS client using WebServicesTemplate and Jxb2Marshaller as a marshaling mechanism?

I just want to register xml and not take any action on raw xml.

+3
source share
2 answers

See documentation spring-ws: http://static.springsource.org/spring-ws/sites/2.0/reference/html/common.html#logging

You can log messages through the standard Commons logging interface:

, org.springframework.ws.server.MessageTracing DEBUG TRACE. ; TRACE - . , org.springframework.ws.server.MessageTracing.sent; org.springframework.ws.server.MessageTracing.received .

: org.springframework.ws.client.MessageTracing.sent org.springframework.ws.client.MessageTracing.received.

+4

- ClientInterceptor, , WebServicesTemplate:

package com.wuntee.interceptor;

import java.io.ByteArrayOutputStream;

import org.apache.log4j.Logger;
import org.springframework.ws.client.WebServiceClientException;
import org.springframework.ws.client.support.interceptor.ClientInterceptor;
import org.springframework.ws.context.MessageContext;

public class LoggerInterceptor implements ClientInterceptor {
    private Logger logger = Logger.getLogger(this.getClass().getName());

    public boolean handleFault(MessageContext context) throws WebServiceClientException {
        return false;
    }

    public boolean handleRequest(MessageContext context) throws WebServiceClientException {
        logger.info("handleRequest");
        logRequestResponse(context);        
        return true;
    }

    public boolean handleResponse(MessageContext context) throws WebServiceClientException {
        logger.info("handleResponse");
        logRequestResponse(context);
        return true;
    }

    private void logRequestResponse(MessageContext context){
        try{
            logger.info("Request:");
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            context.getRequest().writeTo(out);
            byte[] charData = out.toByteArray();
            String str = new String(charData, "ISO-8859-1");
            logger.info(str);
        } catch(Exception e){
            logger.error("Could not log request: ", e);
        }

        if(context.hasResponse()){
            try{
                logger.info("Response:");
                ByteArrayOutputStream out = new ByteArrayOutputStream();
                context.getResponse().writeTo(out);
                byte[] charData = out.toByteArray();
                String str = new String(charData, "ISO-8859-1");
                logger.info(str);
            } catch(Exception e){
                logger.error("Could not log response: ", e);
            }
        }
    }

}
+1

Source: https://habr.com/ru/post/1744976/


All Articles