我有使用弹簧支架控制器的弹簧启动应用程序。 这是控制器,下面是得到的响应。我使用邮差工具向该控制器发送请求。我发送内容类型为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] .wsmsDefaultHandlerExceptionResolver:由Handler执行导致的已解决异常:org.springframework.http.converter.HttpMessageNotReadableException:I /读取输入消息时出错;嵌套异常是java.io.IOException:Stream closed
这是我的邮递员要求
{
"webapp":{"webappName":"cavion17","path":"ud1","isQA":true},
"propertyFiles":"vchannel",
"propertytText":"demo property"}
我尝试删除RequestBody注释,然后能够点击服务,但是param对象被接收为null。
那么请建议如何在restcontroller中检索对象?
答案 0 :(得分:4)
您不能在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
答案 1 :(得分:1)
根据您提供的样本邮递员有效负载,您需要:
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";
}