@ResponseBody
You can use the @ResponseBody
annotation on a method to have the return serialized to the response body through an HttpMessageConverter. The following listing shows an example:
Java
@GetMapping("/accounts/{id}")
@ResponseBody
public Account handle() {
// ...
}
@ResponseBody
is also supported at the class level, in which case it is inherited by all controller methods. This is the effect of @RestController
, which is nothing more than a meta-annotation marked with @Controller
and @ResponseBody
.
You can use @ResponseBody
with reactive types. See Asynchronous Requests and Reactive Types for more details.
You can use the Message Converters option of the MVC Config to configure or customize message conversion.
You can combine @ResponseBody
methods with JSON serialization views. See Jackson JSON for details.
ResponseEntity
ResponseEntity
is like @ResponseBody
but with status and headers. For example:
Java
@GetMapping("/something")
public ResponseEntity<String> handle() {
String body = ... ;
String etag = ... ;
return ResponseEntity.ok().eTag(etag).build(body);
}
Spring MVC supports using a single value reactive type to produce the ResponseEntity
asynchronously, and/or single and multi-value reactive types for the body.
Jackson JSON
Spring offers support for the Jackson JSON library.
以上为spring官网原文 <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core --> <!-- <dependency>--> <!-- <groupId>com.fasterxml.jackson.core</groupId>--> <!-- <artifactId>jackson-core</artifactId>--> <!-- <version>2.11.2</version>--> <!-- </dependency>--> <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-annotations --> <!-- <dependency>--> <!-- <groupId>com.fasterxml.jackson.core</groupId>--> <!-- <artifactId>jackson-annotations</artifactId>--> <!-- <version>2.11.2</version>--> <!-- </dependency>--> <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind --> <!-- <dependency>--> <!-- <groupId>com.fasterxml.jackson.core</groupId>--> <!-- <artifactId>jackson-databind</artifactId>--> <!-- <version>2.11.2</version>--> <!-- </dependency>-->