Java ResponseEntity<T> 和@ResponseBody 有什么区别?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22725143/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 17:29:55  来源:igfitidea点击:

What is the difference between ResponseEntity<T> and @ResponseBody?

javaspringspring-mvc

提问by Flavio

I have a simple handler in my controller which returns a message

我的控制器中有一个简单的处理程序,它返回一条消息

@RequestMapping(value = "/message")
@ResponseBody
public Message get() {
    return new Message(penguinCounter.incrementAndGet() + " penguin!");
}

At the same time I can use something like this

同时我可以使用这样的东西

@RequestMapping(value = "/message")
ResponseEntity<Message> get() {
    Message message = new Message(penguinCounter.incrementAndGet() + " penguin!");
    return new ResponseEntity<Message>(message, HttpStatus.OK);
}

What is the difference betweet this two approaches? Let's not take into account HttpStatus :)

这两种方法有什么区别?我们不考虑 HttpStatus :)

采纳答案by cliff.meyers

ResponseEntity will give you some added flexibility in defining arbitrary HTTP response headers. See the 4th constructor here:

ResponseEntity 将在定义任意 HTTP 响应标头时为您提供一些额外的灵活性。在此处查看第 4 个构造函数:

http://docs.spring.io/spring/docs/3.0.x/api/org/springframework/http/ResponseEntity.html

http://docs.spring.io/spring/docs/3.0.x/api/org/springframework/http/ResponseEntity.html

ResponseEntity(T body, MultiValueMap<String,String> headers, HttpStatus statusCode) 

A List of possible HTTP response headers is available here:

此处提供了可能的 HTTP 响应标头列表:

http://en.wikipedia.org/wiki/List_of_HTTP_header_fields#Responses

http://en.wikipedia.org/wiki/List_of_HTTP_header_fields#Responses

Some commonly-used ones are Status, Content-Type and Cache-Control.

一些常用的是状态、内容类型和缓存控制。

If you don't need that, using @ResponseBody will be a tiny bit more concise.

如果您不需要它,使用 @ResponseBody 会更简洁一点。

回答by Ziaullhaq Savanur

HttpEntityrepresents an HTTP requestor responseconsists of headersand body.

HttpEntity表示由headersbody组成的 HTTP请求响应

// Only talks about body & headers, but doesn't talk about status code
public HttpEntity(T body, MultiValueMap<String,String> headers)

ResponseEntityextends HttpEntity but also adds a Http status code.

ResponseEntity扩展了 HttpEntity 但也添加了 Http 状态代码。

// i.e ResponseEntity = HttpEntity + StatusCode
public ResponseEntity(T body, MultiValueMap<String,String> headers, HttpStatus statusCode)

Hence used to fullyconfigurethe HTTP response.

因此用于完全配置HTTP 响应。

For Ex:

例如:

@ControllerAdvice 
public class JavaWebExeptionHandler {

    @Autowired
    ExceptionErrorCodeMap exceptionErrorCodeMap;

    @ExceptionHandler(RuntimeException.class)
    public final ResponseEntity<ExceptionResponseBody> handleAllExceptions(Exception ex) {
        Integer expCode = exceptionErrorCodeMap.getExpCode(ex.getClass());
        // We have not added headers to response here, If you want you can add by using respective constructor
        return new ResponseEntity<ExceptionResponseBody>(new ExceptionResponseBody(expCode, ex.getMessage()),
                HttpStatus.valueOf(expCode));
    }

}

@ResponseBodyindicates that returnvalue of methodon which it is used is bound to the response body(Mean the return value of method is treated as Http response body)

@ResponseBody表示使用它的方法的返回值绑定到响应(意味着方法的返回值被视为Http响应体)