Spring 引导休息控制器未将请求主体转换为自定义对象
Spring boot rest controller not converting request body to custom object
我有 spring 使用 spring rest controller 的启动应用程序。
这是控制器,下面是响应。我正在使用邮递员工具向该控制器发送请求。我发送的内容类型为 application/json
@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.HttpMessageNotReadableException : I/O 读取输入消息时出错;嵌套异常是 java.io.IOException: Stream closed
这是我的邮递员请求
{
"webapp":{"webappName":"cavion17","path":"ud1","isQA":true},
"propertyFiles":"vchannel",
"propertytText":"demo property"}
我尝试删除 RequestBody 注释,然后能够访问该服务,但收到的参数对象为空。
所以请建议如何在 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";
}
在这里阅读更多
Passing multiple variables in @RequestBody to a Spring MVC controller using Ajax
根据您提供的样本邮递员负载,您将需要:
public class MyObject {
private MyWebapp webapp;
private String propertyFiles;
private String propertytText;
// your getters /setters here as needed
}
和
public class MyWebapp {
private String webappName;
private String path;
private boolean isQA;
// getters setters here
}
然后在您的控制器上将其更改为:
@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";
}
我有 spring 使用 spring rest controller 的启动应用程序。 这是控制器,下面是响应。我正在使用邮递员工具向该控制器发送请求。我发送的内容类型为 application/json
@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.HttpMessageNotReadableException : I/O 读取输入消息时出错;嵌套异常是 java.io.IOException: Stream closed
这是我的邮递员请求
{
"webapp":{"webappName":"cavion17","path":"ud1","isQA":true},
"propertyFiles":"vchannel",
"propertytText":"demo property"}
我尝试删除 RequestBody 注释,然后能够访问该服务,但收到的参数对象为空。
所以请建议如何在 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";
}
在这里阅读更多 Passing multiple variables in @RequestBody to a Spring MVC controller using Ajax
根据您提供的样本邮递员负载,您将需要:
public class MyObject {
private MyWebapp webapp;
private String propertyFiles;
private String propertytText;
// your getters /setters here as needed
}
和
public class MyWebapp {
private String webappName;
private String path;
private boolean isQA;
// getters setters here
}
然后在您的控制器上将其更改为:
@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";
}