2017-04-05 188 views
0

我想記錄我的傳入/傳出消息。目前,我可以記錄整個消息,只需將org.springframework.ws.client.MessageTracing.sent添加到我的log4j2屬性文件中即可。這將記錄如下:Spring SOAP消息參數日誌記錄

[TRACE] TIMESTAMP received - Received response [full message, very long and hard to read] 

我想如下記錄消息:

[TRACE] TIMESTAMP - GetMyLoggingRequest(name: Mike, phone: 5444393) 
[TRACE] TIMESTAMP - GetMyLoggingResponse(surname: Apple, status:"good") 

這只是記錄的請求和響應與參數,它們被髮送/接收的。讀起來更容易!

如何做到這一點?

回答

0

使用攔截器:

@Component 
public class CustomEndpointInterceptor implements EndpointInterceptor { 

    private static final Log LOG = LogFactory.getLog(CustomEndpointInterceptor.class); 

    @Override 
    public boolean handleRequest(MessageContext messageContext, Object endpoint) throws Exception { 
     LOG.info("Endpoint Request Handling"); 
     return true; 
    } 

    @Override 
    public boolean handleResponse(MessageContext messageContext, Object endpoint) throws Exception { 
     LOG.info("Endpoint Response Handling"); 
     return true; 
    } 

    @Override 
    public boolean handleFault(MessageContext messageContext, Object endpoint) throws Exception { 
     LOG.info("Endpoint Exception Handling"); 
     return true; 
    } 

    @Override 
    public void afterCompletion(MessageContext messageContext, Object endpoint, Exception ex) throws Exception { 
     LOG.info("Execute code after completion"); 
    } 
} 

將它添加到你的配置有:

@EnableWs 
@Configuration 
public class SoapServerConfig extends WsConfigurerAdapter { 

    @Override 
    public void addInterceptors(List<EndpointInterceptor> interceptors) { 

     // register global interceptor 
     interceptors.add(new CustomEndpointInterceptor()); 
    } 

    ... 

} 
+1

這是完美的。非常感謝你! – JamesLinux