【问题标题】:Spring MVC ResponseEntity Hystrix fallbackSpring MVC ResponseEntity Hystrix 回退
【发布时间】:2018-04-05 09:47:31
【问题描述】:

我有一个返回 ResponseEntity<List<Attachment>> 的服务方法,它的 hystrix fallback 方法也必须返回 ResponseEntity<List<Attachment>>
问题是我需要返回一个字符串消息来向用户澄清错误,而不是返回一个新的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);
}

【问题讨论】:

    标签: java spring hystrix fallback


    【解决方案1】:

    我通过不带 args 的 ResponseEntity 而不是 ResponseEntity&lt;List&lt;AttachmentDto&gt;&gt; 使其工作

    谢谢大家

    【讨论】:

      【解决方案2】:

      只需使用:

      ResponseEntity<Object>
      

      这适用于任何类型。因为 Object 是 java.lang

      中定义的最顶层的类

      代替:

      ResponseEntity<List<AttachmentDto>>
      

      【讨论】:

        猜你喜欢
        • 2020-05-23
        • 2017-06-18
        • 2017-04-15
        • 1970-01-01
        • 2013-12-18
        • 2017-10-10
        • 2020-11-22
        • 2017-03-13
        • 2017-05-03
        相关资源
        最近更新 更多