1、@ResponseBody注解
作用是将controller的方法返回的对象通过HttpMessageConverter
或者其他转换器转换为指定的格式之后,写入到HttpServletResponse
对象的body区,通常用来返回JSON数据或者是XML数据。
不使用@ResponseBody注解
的效果:
在不使用
@ResponseBody
的情况下会走视图处理器,如果没有对应的视图则会报404错误,不适用前后端分离或者单纯返回数据。
使用@ResponseBody注解
的效果:
在使用
@ResponseBody
的情况下不会再走视图处理器,而是直接将数据写入到输入流中,他的效果等同于通过response对象输出指定格式的数据。
/**
* 将指定消息写回响应
* @param httpServletResponse
* @param message
* @throws IOException
*/
public static void writeResponse(HttpServletResponse httpServletResponse, String message) throws IOException {
httpServletResponse.setContentType("application/json;charset=UTF-8");
httpServletResponse.addHeader("Access-Control-Allow-Origin", "*");
ServletOutputStream out = httpServletResponse.getOutputStream();
out.write(message.getBytes(StandardCharsets.UTF_8));
out.flush();
out.close();
}
2、HttpEntity<T>
允许直接返回自定义的Http响应,它支持自定义的一个HttpHeaders
,泛型可以返回指定数据、json、二进制文件流org.springframework.core.io.Resource
等
以下为官方示例:
Represents an HTTP request or response entity, consisting of headers and body.
Often used in combination with the org.springframework.web.client.RestTemplate, like so:
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.TEXT_PLAIN);
HttpEntity<String> entity = new HttpEntity<>("Hello World", headers);
URI location = template.postForLocation("https://example.com", entity);
or
HttpEntity<String> entity = template.getForEntity("https://example.com", String.class);
String body = entity.getBody();
MediaType contentType = entity.getHeaders().getContentType();
Can also be used in Spring MVC, as a return value from a @Controller method:
@GetMapping("/handle")
public HttpEntity<String> handle() {
HttpHeaders responseHeaders = new HttpHeaders();
responseHeaders.set("MyResponseHeader", "MyValue");
return new HttpEntity<>("Hello World", responseHeaders);
}
public class HttpEntity<T> {
}
2、ResponseEntity<T>
ResponseEntity
继承自HttpEntity
,在其基础上扩展了 HttpStatus
,允许直接返回自定义的Http状态码
以下为官方示例:
Extension of HttpEntity that adds an HttpStatus status code. Used in RestTemplate as well as in @Controller methods.
In RestTemplate, this class is returned by getForEntity() and exchange():
ResponseEntity<String> entity = template.getForEntity("https://example.com", String.class);
String body = entity.getBody();
MediaType contentType = entity.getHeaders().getContentType();
HttpStatus statusCode = entity.getStatusCode();
This can also be used in Spring MVC as the return value from an @Controller method:
@RequestMapping("/handle")
public ResponseEntity<String> handle() {
URI location = ...;
HttpHeaders responseHeaders = new HttpHeaders();
responseHeaders.setLocation(location);
responseHeaders.set("MyResponseHeader", "MyValue");
return new ResponseEntity<String>("Hello World", responseHeaders, HttpStatus.CREATED);
}
Or, by using a builder accessible via static methods:
@RequestMapping("/handle")
public ResponseEntity<String> handle() {
URI location = ...;
return ResponseEntity.created(location).header("MyResponseHeader", "MyValue").body("Hello World");
}
public class ResponseEntity<T> extends HttpEntity<T> {
}