【问题标题】:Spring automatically wrap rest controller response in ResponseEntitySpring 自动将 rest 控制器响应包装在 ResponseEntity 中
【发布时间】:2017-12-04 12:10:48
【问题描述】:

这是我想要实现的目标: 我有许多 spring rest 反应控制器返回各种对象,例如 Mono、Flux

我想将所有这些响应包装在一个 ResponseEntity 中,因此总是返回如下内容:

Mono<ResponseEntity<MyObjects>>

有什么建议吗?

【问题讨论】:

  • 返回ResponseEntity&lt;Mono&lt;MyObjects&gt;&gt; ?
  • 这样或那样,但这不是问题,我正在寻找一种方法来自动将我的响应包装在 ResponseEntity 中,例如使用过滤器或响应处理程序或类似的东西

标签: spring spring-rest reactor


【解决方案1】:

只需在控制器上使用 @ResponseBody 注释

当您在方法上使用@ResponseBody 注解时,Spring 会自动转换返回值并将其写入http 响应。 Controller 类中的每个方法都必须使用@ResponseBody 进行注解。

the example

【讨论】:

  • 您的意思是它会自动将我的响应包装在 ResponseEntity.ok() 中吗?
  • 那种。它返回具有正确状态的方法返回对象的 json 表示
  • 没必要! @RestController 根据您提供的链接自动使用 @ResponsyBody 注释方法:By annotating the controller class with @RestController annotation, you no longer need to add @ResponseBody to all the request mapping methods. The @ResponseBody annotation is active by default.
猜你喜欢
  • 2017-01-07
  • 2020-04-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-09-12
  • 2020-05-09
  • 1970-01-01
  • 2023-03-15
相关资源
最近更新 更多