2015-06-05 40 views
12

我對我的soap服務使用SpringWS並像這樣驗證它;Spring WS:如何獲取並保存XSD驗證錯誤

<sws:interceptors> 
    <bean id="payloadValidatingInterceptor" class="org.springframework.ws.soap.server.endpoint.interceptor.PayloadValidatingInterceptor"> 
     <property name="schema" value="/schemas/my.xsd"/> 
     <property name="validateRequest" value="false"/> 
     <property name="validateResponse" value="true"/> 
    </bean> 

@PayloadRoot(namespace = NAMESPACE, localPart = "ServiceProvider") 
@ResponsePayload 
public ServiceProviderTxn getAccountDetails(@RequestPayload ServiceProviderrequest) 
{ ...} 

這工作得很好,但是當有一個錯誤是它到達終點之前,返回彈簧產生錯誤響應,所以我再也沒有機會對其進行處理。但我希望能夠日誌並將完整的錯誤消息保存到數據庫。我發現的一個方法是在我的另一個問題中做這樣的事情;

Spring WS How to get all error messages when validation fails

但我想這是行不通的。

回答

7

可以延長PayloadValidationInterceptor並重新定義方法

protected boolean handleRequestValidationErrors(MessageContext messageContext, SAXParseException[] errors) 

如果你看一下標準執行(可here),你可以看到它是如何轉儲所有解析錯誤;您還可以轉儲傳入消息,因爲您可以訪問messageContext及其getRequest()方法。你的班級應該像

public class PayloadValidationgInterceptorCustom extends 
PayloadValidatingInterceptor { 

@Override 
protected boolean handleRequestValidationErrors(MessageContext messageContext, SAXParseException[] errors) 
     throws TransformerException { 
    messageContext.getRequest().writeTo(/*place your Outputstream here something like a ByteArrayOutputStream*/); //use this if you want to dump the message 
    for (SAXParseException error : errors) { 
     //dump the each error on the db o collect the stack traces in a single string and dump only one or to the database 
     /*you can use something like this 
     StringWriter sw = new StringWriter(); 
     PrintWriter pw = new PrintWriter(sw); 
     error.printStackTrace(pw); 
     sw.toString(); 
     to get the stack trace 
     */ 

    } 
    return super.handleRequestValidationErrors(messageContext,errors); 

} 
+1

tnx但你能擴展你的答案與代碼並結合我在我的鏈接上描述的答案嗎?它更像是理論而不是工作解決方案 – Spring

+1

@Spring:添加樣本類 – Giovanni

+0

我試過但handleRequestValidationErrors永遠不會調用 – Spring