Java Hystrix-如何注册ExceptionMapper
我的Java Hystrix-如何注册ExceptionMapper,java,hystrix,spring-cloud-netflix,Java,Hystrix,Spring Cloud Netflix,我的Hystrix/Feign应用程序调用其他web服务 我想传播来自这些web服务的错误代码/消息 我实现了ErrorDecoder,它正确地解码返回的异常并重新显示它们 不幸的是,这些异常被HystrixRuntimeException包装,返回的JSON不是我想要的(一般错误消息,总是500 http状态) 很可能我需要一个异常apper,我创建了这样一个: @Provider public class GlobalExceptionHandler implements Excep
Hystrix/Feign
应用程序调用其他web服务
我想传播来自这些web服务的错误代码/消息
我实现了ErrorDecoder
,它正确地解码返回的异常并重新显示它们
不幸的是,这些异常被HystrixRuntimeException
包装,返回的JSON
不是我想要的(一般错误消息,总是500 http状态)
很可能我需要一个异常apper
,我创建了这样一个:
@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();
}
}
@Provider
公共类GlobalExceptionHandler实现
例外情况{
@凌驾
公众响应(可丢弃的e){
系统输出打印项次(“ABCD 1”);
if(e实例of HystrixRuntimeException){
系统输出打印项次(“ABCD 2”);
如果(e.getCause()!=null和&e.getCause()instanceof HttpStatusCodeException)
{
系统输出打印项次(“ABCD 3”);
HttpStatusCodeException exc=(HttpStatusCodeException)e.getCause();
返回Response.status(exc.getStatusCode().value())
.entity(exc.getMessage())
.type(MediaType.APPLICATION_JSON).build();
}
}
返回Response.status(500).entity(“内部服务器错误”).build();
}
}
不幸的是,我的应用程序没有提取该代码(调试语句在日志中不可见)
我如何在我的应用程序中注册它?我无法使用
异常apper
我使用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);
}
}
@EnableWebMvc
@控制器建议
公共类ServiceExceptionHandler扩展了ResponseEntityExceptionHandler{
@ExceptionHandler(HystrixRuntimeException.class)
@应答器
ResponseEntity HandleController异常(HttpServletRequest请求,Throwable ex){
if(例如HystrixRuntimeException的实例){
HttpStatusCodeException exc=(HttpStatusCodeException)ex.getCause();
返回新的ResponseEntity(exc.getResponseBodyAsString(),exc.getStatusCode());
}
返回新的响应属性(例如getMessage(),HttpStatus.INTERNAL_SERVER_ERROR);
}
}
我无法使用例外标记
我使用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);
}
}
@EnableWebMvc
@控制器建议
公共类ServiceExceptionHandler扩展了ResponseEntityExceptionHandler{
@ExceptionHandler(HystrixRuntimeException.class)
@应答器
ResponseEntity HandleController异常(HttpServletRequest请求,Throwable ex){
if(例如HystrixRuntimeException的实例){
HttpStatusCodeException exc=(HttpStatusCodeException)ex.getCause();
返回新的ResponseEntity(exc.getResponseBodyAsString(),exc.getStatusCode());
}
返回新的响应属性(例如getMessage(),HttpStatus.INTERNAL_SERVER_ERROR);
}
}