@ResponseBody

您可以在方法上使用 @ResponseBody 注解,通过 HttpMessageWriter 将返回序列化到响应主体。以下示例说明了如何执行此操作:

You can use the @ResponseBody annotation on a method to have the return serialized to the response body through an HttpMessageWriter. The following example shows how to do so:

  • Java

  • Kotlin

@GetMapping("/accounts/{id}")
@ResponseBody
public Account handle() {
	// ...
}
@GetMapping("/accounts/{id}")
@ResponseBody
fun handle(): Account {
	// ...
}

@ResponseBody 也受支持用于类级别,在这种情况下,它会被所有控制器方法继承。这是 @RestController 的效果,它只不过是一个使用 @Controller@ResponseBody 进行标记的元注解。

@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.

@ResponseBody 支持响应类型,这意味着您可以返回 Reactor 或 RxJava 类型,并让它们产生的异步值呈现到响应中。有关更多详细信息,请参见 StreamingJSON rendering

@ResponseBody supports reactive types, which means you can return Reactor or RxJava types and have the asynchronous values they produce rendered to the response. For additional details, see Streaming and JSON rendering.

您可以将 @ResponseBody 方法与 JSON 序列化视图结合使用。有关详细信息,请参阅 Jackson JSON

You can combine @ResponseBody methods with JSON serialization views. See Jackson JSON for details.

您可以使用 WebFlux ConfigHTTP message codecs 选项来配置或自定义消息写入。

You can use the HTTP message codecs option of the WebFlux Config to configure or customize message writing.