2017-01-09 13 views
0

私のHystrix/Feignは、他のWebサービスを呼び出します。Hystrix - ExceptionMapperの登録方法

これらのWebサービスからエラーコード/メッセージを伝播したいと思います。

ErrorDecoderを実装しました。これは、返された例外を正しくデコードして再スローします。

残念ながら、これらの例外はHystrixRuntimeExceptionと、JSONで返されます(一般的なエラーメッセージ、常に500 httpステータス)。

私は ExceptionMapperが必要

ほとんどの場合、私はこのようなものを作成しました:

@Provider 
public class GlobalExceptionHandler implements 
    ExceptionMapper<Throwable> { 

@Override 
public Response toResponse(Throwable e) { 
    System.out.println("ABCD 1"); 
    if(e instanceof HystrixRuntimeException){ 
     System.out.println("ABCD 2"); 
     if(e.getCause() != null && e.getCause() instanceof HttpStatusCodeException) 
     { 
      System.out.println("ABCD 3"); 
      HttpStatusCodeException exc = (HttpStatusCodeException)e.getCause(); 
      return Response.status(exc.getStatusCode().value()) 
        .entity(exc.getMessage()) 
        .type(MediaType.APPLICATION_JSON).build(); 
     } 
    } 
    return Response.status(500).entity("Internal server error").build(); 
} 
} 

残念ながら、このコードは、撮像されていない、私のアプリケーションでは(デバッグ文がログに表示されません)。

私は自分のアプリケーションにどのように登録できますか?

答えて

0

私はExceptionMapperを利用できませんでした。

ResponseEntityExceptionHandlerを使用してこの問題を解決しました。ここで

はコードです:

@EnableWebMvc 
@ControllerAdvice 
public class ServiceExceptionHandler extends ResponseEntityExceptionHandler { 

    @ExceptionHandler(HystrixRuntimeException.class) 
    @ResponseBody 
    ResponseEntity<String> handleControllerException(HttpServletRequest req, Throwable ex) { 
     if(ex instanceof HystrixRuntimeException) { 
      HttpStatusCodeException exc = (HttpStatusCodeException)ex.getCause(); 
      return new ResponseEntity<>(exc.getResponseBodyAsString(), exc.getStatusCode()); 
     } 
     return new ResponseEntity<String>(ex.getMessage(), HttpStatus.INTERNAL_SERVER_ERROR); 
    } 
} 
関連する問題