2015-06-05 13 views
12

Tôi sử dụng SpringWS cho dịch vụ xà phòng của mình và xác nhận nó như thế này;Spring WS: Cách nhận và lưu các lỗi xác thực 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) 
{ ...} 

này hoạt động tốt nhưng khi có một lỗi nó trả về một mùa xuân tạo ra phản ứng lỗi trước khi nó đạt đến điểm cuối, vì vậy tôi không bao giờ có cơ hội để xử lý chúng. Nhưng tôi muốn có thể đăng nhập và lưu thông báo lỗi đầy đủ vào cơ sở dữ liệu. Một cách tôi phát hiện ra là làm điều gì đó như thế này trong câu hỏi khác của tôi;

Spring WS How to get all error messages when validation fails

Nhưng nó không hoạt động như tôi muốn.

Trả lời

7

bạn có thể mở rộng PayloadValidationInterceptor và xác định lại phương pháp

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

Nếu bạn nhìn vào việc thực hiện tiêu chuẩn (có sẵn here), bạn có thể xem cách nó bãi tất cả các lỗi phân tích cú pháp; bạn cũng có thể đổ thông điệp đến vì bạn có quyền truy cập vào messageContext và phương thức getRequest() của nó. Lớp học của bạn xould giống như

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 nhưng bạn có thể mở rộng câu trả lời bằng mã và kết hợp với câu trả lời mà tôi đã mô tả trên liên kết của mình không? Nó nghe có vẻ giống như một lý thuyết hơn là một giải pháp làm việc – Spring

+1

@Spring: thêm một lớp mẫu – Giovanni

+0

Tôi đã thử nhưng handleRequestValidationErrors không bao giờ được gọi là – Spring

Các vấn đề liên quan