2013-06-13 9 views
9

Musimy zwrócić niestandardowy kod błędu i komunikat o błędzie, gdy wystąpi wyjątek podczas wywoływania REST. Stworzyliśmy dostawcę mapperów wyjątków, działa on dobrze dla wyjątków z kodu aplikacji. Jednak nie działa, gdy wystąpi wyjątek z kodu CXF (np. Z utworzonego przeze mnie CustomValidationInterceptor).CXF/JAX-RS: Zwraca niestandardową odpowiedź z przechwytywacza

Na przykład, jeśli zażądam z niepoprawnym parametrem ścieżki (np. Nieprawidłowy numer telefonu). W takim przypadku musimy zwrócić niestandardowy kod błędu i komunikat o błędzie w formacie JSON, ale nie działa, mimo że mamy dostawcę wyjątków mappera stworzonego do obsługi wyjątku WebApplicationException.

Czy istnieje sposób na obsługę wyjątków od przechwytywaczy CXF i zwrócenie odpowiedzi dla użytkownika z czymś podobnym?

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

kod urywek mojego CustomValidationInterceptor:

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; 
    } 

} 

kod urywek niestandardowej Wyjątek Mapper Provider

public class TelusExceptionHandler implements ExceptionMapper<TelusServiceException> { 

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

} 

fragment kodu z 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); 
    } 

} 

fragment kodu klasy 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

Poprawiłem JSON. Zobacz: – Bhuvan

+0

Co to jest kod 'TelusServiceException''' .getErrorDetail() '? – fge

+0

dodano fragment kodu TelusServiceException i ErrorDetail Obiekt – Bhuvan

Odpowiedz

7

znalazłem sposób, aby wysłać odpowiedź niestandardową z kolektora, ale wciąż nie może wymyślić sposób, aby zadzwonić do mojego CustomExceptionHandler od kolektora

Kod:

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

podniosłem podobne pytanie na CXF grupy użytkowników, patrz:

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

Skończyło się na zastąpieniu moich przechwytujących kontenerami ContainerRequestFilter i ContainerResponseFilter, a następnie aplikacja Mapowanie wyjątków z przyjemnością obsługiwała zarówno wyjątki aplikacji, jak i wyjątki generowane z filtru.

Mam nadzieję, że to pomoże.

+0

Może masz podobny problem: http://stackoverflow.com/questions/37984617/avoid-exception-mapper-through-cxf-interceptor –

Powiązane problemy