Spring MVC ResponseEntity Hystrix 回退
Spring MVC ResponseEntity Hystrix fallback
我有一个服务方法 return 是 ResponseEntity<List<Attachment>>
并且它的 hystrix fallback
方法也必须 return 是 ResponseEntity<List<Attachment>>
。
问题是我需要 return 一条字符串消息来向用户澄清错误,而不是 return 发送一个新的 Arraylist<>()
- 这是我的方法
@Override
@HystrixCommand(fallbackMethod = "getAttachmentsFallback")
public ResponseEntity<List<AttachmentDto>> getAttachments(IAttachable entity) {
List<AttachmentDto> attachments = client.getAttachments(entity.getAttachableId(), entity.getClassName(),
entity.getAppName());
return new ResponseEntity<List<AttachmentDto>>(attachments, HttpStatus.OK);
}
这就是它的后备
public ResponseEntity<List<AttachmentDto>> getAttachmentsFallback(IAttachable entity, Throwable e) {
//I need to return a String instead of the new Arraylist<AttachmentDto>()
return new ResponseEntity<List<AttachmentDto>>(new ArrayList<AttachmentDto>(), HttpStatus.INTERNAL_SERVER_ERROR);
}
只需使用:
ResponseEntity<Object>
这适用于任何类型。因为Object最上面class定义在java.lang
而不是:
ResponseEntity<List<AttachmentDto>>
我通过不使用参数 ResponseEntity
而不是 ResponseEntity<List<AttachmentDto>>
使其工作
谢谢大家
我有一个服务方法 return 是 ResponseEntity<List<Attachment>>
并且它的 hystrix fallback
方法也必须 return 是 ResponseEntity<List<Attachment>>
。
问题是我需要 return 一条字符串消息来向用户澄清错误,而不是 return 发送一个新的 Arraylist<>()
- 这是我的方法
@Override
@HystrixCommand(fallbackMethod = "getAttachmentsFallback")
public ResponseEntity<List<AttachmentDto>> getAttachments(IAttachable entity) {
List<AttachmentDto> attachments = client.getAttachments(entity.getAttachableId(), entity.getClassName(),
entity.getAppName());
return new ResponseEntity<List<AttachmentDto>>(attachments, HttpStatus.OK);
}
这就是它的后备
public ResponseEntity<List<AttachmentDto>> getAttachmentsFallback(IAttachable entity, Throwable e) {
//I need to return a String instead of the new Arraylist<AttachmentDto>()
return new ResponseEntity<List<AttachmentDto>>(new ArrayList<AttachmentDto>(), HttpStatus.INTERNAL_SERVER_ERROR);
}
只需使用:
ResponseEntity<Object>
这适用于任何类型。因为Object最上面class定义在java.lang
而不是:
ResponseEntity<List<AttachmentDto>>
我通过不使用参数 ResponseEntity
而不是 ResponseEntity<List<AttachmentDto>>
谢谢大家