2013-06-13 45 views
9

Chúng tôi cần trả lại mã lỗi tùy chỉnh và thông báo lỗi khi ngoại lệ xảy ra trong khi thực hiện lệnh REST. Chúng tôi đã tạo một nhà cung cấp bản đồ ngoại lệ, nó hoạt động tốt cho các trường hợp ngoại lệ từ mã ứng dụng. Tuy nhiên, nó không hoạt động khi ngoại lệ xảy ra từ mã CXF (ví dụ: biểu mẫu CustomValidationInterceptor mà tôi đã viết).CXF/JAX-RS: Trả về Phản hồi tùy chỉnh từ thiết bị chặn

Ví dụ: nếu tôi yêu cầu với thông số đường dẫn không hợp lệ (ví dụ: số điện thoại không hợp lệ). Trong trường hợp này, chúng ta cần trả về một mã lỗi tùy chỉnh và thông báo lỗi ở định dạng JSON, nhưng nó không hoạt động mặc dù chúng ta có một nhà cung cấp bản đồ ngoại lệ được tạo ra để xử lý WebApplicationException.

Có cách nào để xử lý ngoại lệ từ bộ chặn đánh chặn cxf và trả lại phản hồi cho người dùng bằng một cái gì đó như sau không?

{ 
"errorDetail": { 
"errorCode": "404", 
"errorMessage": "Bad Request" 
} 
} 

Code Snippet của CustomValidationInterceptor tôi:

public class CustomValidationInterceptor extends AbstractPhaseInterceptor<Message>{ 

    public CustomValidationInterceptor() { 
     super(Phase.PRE_INVOKE); // Put this interceptor in this phase 
    } 

    public void handleMessage(Message message) { 

     MetadataMap<String, String> metadataMap = (MetadataMap<String, String>) message.get("jaxrs.template.parameters"); 

     if(null != metadataMap) { 
      List<String> list = metadataMap.get("phoneNumber"); 
      if(null != list) { 
       String phoneNumber = list.get(0); 
       boolean result = validatePhoneNumber(phoneNumber); 
       if(!result){ 
        throw new TelusServiceException(Response.status(Response.Status.BAD_REQUEST).build(), 400, "phone number not valid"); 
       } 
      } else { 
       throw new TelusServiceException(Response.status(Response.Status.BAD_REQUEST).build(), 400, "phone number not valid"); 
      } 
     } else { 
      throw new TelusServiceException(Response.status(Response.Status.BAD_REQUEST).build(), 400, "phone number not valid"); 
     } 
    } 

    public boolean validatePhoneNumber(String phoneNumber) { 

      Pattern pattern = Pattern.compile("^[1-9]\\d{9}$"); 
      Matcher matcher = pattern.matcher(phoneNumber); 

      if (!matcher.matches()) { 
       return false; 
      } 
      return true; 
    } 

} 

Code Snippet Hải quan cung cấp ngoại lệ Mapper tôi

public class TelusExceptionHandler implements ExceptionMapper<TelusServiceException> { 

    public Response toResponse(TelusServiceException exception) { 
     return Response.status(exception.getErrorDetail().getErrorCode()).entity(exception.getErrorDetail()).build(); 
    } 

} 

Code Snippet của TelusServiceException

public class TelusServiceException extends WebApplicationException{ 

// constructors and other methods 

    private ErrorDetail errorDetail = null; 

     public ErrorDetail getErrorDetail() { 
     return errorDetail; 
    } 

    public void setErrorDetail(ErrorDetail errorDetail) { 
     this.errorDetail = errorDetail; 
    } 

     public TelusServiceException(Response response, int errorCode, String errorMessage) { 
     super(response); 

     errorDetail = new ErrorDetail(); 
     errorDetail.setErrorCode(errorCode); 
     errorDetail.setErrorMessage(errorMessage); 
    } 

} 

Code Snippet của lớp ErrorDetail

@XmlRootElement(name="errorDetail") 
public class ErrorDetail { 

    private int errorCode; 
    private String errorMessage; 

    @XmlElement(name = "errorCode") 
    public int getErrorCode() { 
     return errorCode; 
    } 

    public void setErrorCode(int errorCode) { 
     this.errorCode = errorCode; 
    } 
    @XmlElement(name = "errorMessage") 
    public String getErrorMessage() { 
     return errorMessage; 
    } 

    public void setErrorMessage(String errorMessage) { 
     this.errorMessage = errorMessage; 
    } 

} 
+0

Tôi đã sửa lại JSON. Vui lòng xem – Bhuvan

+0

Mã '.getErrorDetail()' của TelusServiceException' là gì? – fge

+0

thêm đoạn mã của TelusServiceException và ErrorDetail Object – Bhuvan

Trả lời

7

Tôi tìm thấy một cách để gửi một phản ứng tùy chỉnh từ đánh chặn nhưng vẫn không thể tìm ra một cách để gọi CustomExceptionHandler của tôi từ đánh chặn

Code:

public void handleMessage(Message message) { 

     MetadataMap<String, String> metadataMap = (MetadataMap<String, String>) message.get("jaxrs.template.parameters"); 

     if(null != metadataMap) { 
      List<String> list = metadataMap.get("phoneNumber"); 
      if(null != list) { 
       String phoneNumber = list.get(0); 
       boolean result = validatePhoneNumber(phoneNumber); 
       if(!result){ 
// Create a response object and set it in the message. 
// calling getExchange() will not call your service 
        Response response = Response 
       .status(Response.Status.BAD_REQUEST) 
       .entity(new ErrorDetail(Response.Status.BAD_REQUEST.getStatusCode(), Response.Status.BAD_REQUEST.toString())) 
       .build(); 
     message.getExchange().put(Response.class, response); 
// That's it 
       } 
      } else { 
       Response response = Response 
       .status(Response.Status.BAD_REQUEST) 
       .entity(new ErrorDetail(Response.Status.BAD_REQUEST.getStatusCode(), Response.Status.BAD_REQUEST.toString())) 
       .build(); 
     message.getExchange().put(Response.class, response); 
      } 
     } else { 
      Response response = Response 
       .status(Response.Status.BAD_REQUEST) 
       .entity(new ErrorDetail(Response.Status.BAD_REQUEST.getStatusCode(), Response.Status.BAD_REQUEST.toString())) 
       .build(); 
     message.getExchange().put(Response.class, response); 
     } 
    } 
2

Tôi đã nêu một câu hỏi tương tự về nhóm người dùng cxf, xem:

http://cxf.547215.n5.nabble.com/Handling-exceptions-in-a-JAX-RS-fault-interceptor-when-using-Local-Transport-td5733958.html

Tôi đã kết thúc thay thế các máy đánh chặn của tôi với ContainerRequestFilter và ContainerResponseFilter và sau đó Exception Mapper vui vẻ xử lý cả hai ngoại lệ ứng dụng và các ngoại lệ được ném ra khỏi Bộ lọc.

Hy vọng điều này sẽ hữu ích.

+0

Có thể bạn đã gặp phải sự cố tương tự: http://stackoverflow.com/questions/37984617/avoid-exception-mapper-through-cxf-interceptor –