简体   繁体   中英

Spring MVC ResponseEntity Hystrix fallback

I Have a service method that returns a ResponseEntity<List<Attachment>> and its hystrix fallback method must also return a ResponseEntity<List<Attachment>> .
The problem is that I need to return a String message that clarifies the error to the user instead of returning a new Arraylist<>()

- Here is my method

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

And that's its fallback

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

Just use :

ResponseEntity<Object>

this will work for any type. Because Object is the top most class defined in java.lang

instead of:

ResponseEntity<List<AttachmentDto>>

I Made it work by doing ResponseEntity with no args instead of ResponseEntity<List<AttachmentDto>>

Thanks guys

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM