Java 响应性<;T>;还有@ResponseBody?

Java 响应性<;T>;还有@ResponseBody?,java,spring,spring-mvc,Java,Spring,Spring Mvc,我的控制器中有一个返回消息的简单处理程序 @RequestMapping(value = "/message") @ResponseBody public Message get() { return new Message(penguinCounter.incrementAndGet() + " penguin!"); } 同时我也可以用这样的东西 @RequestMapping(value = "/message") ResponseEntity<Message> ge

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

@RequestMapping(value = "/message")
@ResponseBody
public Message get() {
    return new Message(penguinCounter.incrementAndGet() + " penguin!");
}
同时我也可以用这样的东西

@RequestMapping(value = "/message")
ResponseEntity<Message> get() {
    Message message = new Message(penguinCounter.incrementAndGet() + " penguin!");
    return new ResponseEntity<Message>(message, HttpStatus.OK);
}
@RequestMapping(value=“/message”)
ResponseEntity get(){
消息消息=新消息(penguinCounter.incrementAndGet()+“penguin!”);
返回新的响应属性(消息,HttpStatus.OK);
}

这两种方法之间的区别是什么?我们不考虑HttpStatus:)

ResponseEntity将为您定义任意HTTP响应头提供一些额外的灵活性。请参见此处的第四个构造函数:

ResponseEntity(T体、多值映射头、HttpStatus状态码)
此处提供了可能的HTTP响应头列表:

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


如果您不需要,那么使用@ResponseBody将更加简洁。

HttpEntity表示一个HTTP请求响应标题正文组成

// Only talks about body & headers, but doesn't talk about status code
public HttpEntity(T body, MultiValueMap<String,String> headers)
//只讨论正文和标题,不讨论状态代码
公共HttpEntity(T体,多值映射头)
ResponseEntity扩展了HttpEntity,但也添加了Http状态代码

// i.e ResponseEntity = HttpEntity + StatusCode
public ResponseEntity(T body, MultiValueMap<String,String> headers, HttpStatus statusCode)
//即ResponseEntity=HttpEntity+StatusCode
公共响应性(T体、多值映射头、HttpStatus状态码)
因此用于完全配置HTTP响应

例如:

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

}
@ControllerAdvice
公共类JavaWebExceptionHandler{
@自动连线
ExceptionErrorCodeMap ExceptionErrorCodeMap;
@ExceptionHandler(RuntimeException.class)
公共最终响应处理例外情况(例外情况除外){
整数expCode=exceptionErrorCodeMap.getExpCode(例如getClass());
//我们没有在这里为响应添加头,如果您需要,可以使用相应的构造函数添加
返回新的ResponseEntity(新的ExceptionResponseBy(expCode,例如getMessage()),
HttpStatus.valueOf(expCode));
}
}
@ResponseBody表示使用它的方法的返回值绑定到响应主体
(意味着方法的返回值被视为Http响应体)

头中传输的是什么内容?有什么例子吗?
@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));
    }

}