Java Spring引导rest控制器未将请求正文转换为自定义对象
我有一个使用SpringREST控制器的SpringBoot应用程序。 这是控制器,下面是得到的响应。我正在使用邮递员工具向该控制器发送请求。我将内容类型作为application/json发送Java Spring引导rest控制器未将请求正文转换为自定义对象,java,spring,spring-boot,spring-data-rest,Java,Spring,Spring Boot,Spring Data Rest,我有一个使用SpringREST控制器的SpringBoot应用程序。 这是控制器,下面是得到的响应。我正在使用邮递员工具向该控制器发送请求。我将内容类型作为application/json发送 @RequestMapping(value = "/test", method = RequestMethod.POST) public String test(@RequestBody WebApp webapp, @RequestBody String propertyFiles, @Req
@RequestMapping(value = "/test", method = RequestMethod.POST)
public String test(@RequestBody WebApp webapp, @RequestBody String propertyFiles, @RequestBody String) {
System.out.println("webapp :"+webapp);
System.out.println("propertyFiles :"+propertyFiles);
System.out.println("propertyText :"+propertyText);
return "ok good";
}
2018-03-21 12:18:47.732 WARN 8520---[nio-8099-exec-3].w.s.m.s.DefaultHandlerExceptionResolver:由处理程序执行引起的已解决异常:org.springframework.http.converter.httpMessageEndableException:读取输入消息时发生I/O错误;嵌套异常为java.io.IOException:流已关闭
这是我的邮递员请求
{
"webapp":{"webappName":"cavion17","path":"ud1","isQA":true},
"propertyFiles":"vchannel",
"propertytText":"demo property"}
我尝试删除RequestBody注释,然后能够点击服务,但是param对象被接收为null
因此,请建议如何在restcontroller中检索对象?在Spring中不能使用多个
@RequestBody
注释。您需要将所有这些包装在一个对象中
有些像这样
// some imports here
public class IncomingRequestBody {
private Webapp webapp;
private String propertryFiles;
private String propertyText;
// add getters and setters here
}
在你的控制器里
@RequestMapping(value = "/test", method = RequestMethod.POST)
public String test(@RequestBody IncomingRequestBody requestBody) {
System.out.println(requestBody.getPropertyFiles());
// other statement
return "ok good";
}
在这里阅读更多
根据您提供的邮递员有效载荷样本,您需要:
public class MyObject {
private MyWebapp webapp;
private String propertyFiles;
private String propertytText;
// your getters /setters here as needed
}
及
然后在控制器上将其更改为:
@RequestMapping(value = "/test", method = RequestMethod.POST)
public String test(@RequestBody MyObject payload) {
// then access the fields from the payload like
payload.getPropertyFiles();
return "ok good";
}
假设您只想为PropertyFiles单独赋值。如何接受?
@RequestMapping(value = "/test", method = RequestMethod.POST)
public String test(@RequestBody MyObject payload) {
// then access the fields from the payload like
payload.getPropertyFiles();
return "ok good";
}