【发布时间】:2018-06-17 04:06:17
【问题描述】:
我的 Spring Boot 应用程序中有以下代码:
@RestController
@RequestMapping("/execute")
public class RestCommandExecutor {
@PostMapping
public Response executeCommand(@RequestBody Command command,
HttpEntity<String> httpEntity) {
System.out.println(command);
System.out.println("********* payload is: " + httpEntity.getBody());
return new Response("Hello text", new Long(123));
}
}
当 POST 请求带有正确的 Command 时,我收到 I/O 错误:
.w.s.m.s.DefaultHandlerExceptionResolver : Failed to read HTTP message: org.springframework.http.converter.HttpMessageNotReadableException: I/O error while reading input message; nested exception is java.io.IOException: Stream closed
我希望 Spring 将请求正文转换为 Command,但由于 JSON 包含的内容超过了 Command 类的一部分,因此我也希望获得完整的原始 JSON。
有没有办法通过方法中的映射来实现呢?我知道我总是可以这样做public Response executeCommand(HttpEntity<String> httpEntity),然后使用 Jackson 手动将其转换为 Command,但我宁愿不必手动这样做。
这可能吗?
【问题讨论】:
-
为什么要转成Command?
-
检索其
type属性。然后,我会将其反序列化为可以转换所有命令属性的类型。
标签: java spring spring-restcontroller spring-boot-actuator