我有使用弹簧休息控制器的弹簧启动应用程序。 这是控制器,下面是响应和获取。我正在使用邮递员工具向此控制器发送请求。并且正在将内容类型作为应用程序/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 警告 8520 --- [nio-8099-exec-3] .w.s.m.s.DefaultHandlerExceptionResolver :已解决由处理程序执行引起的异常:org.springframework.http.converter.HttpMessageNotReadableException:读取输入消息时出现 I/O 错误;嵌套异常为 java.io.IOException:流已关闭
这是我的邮递员要求
{
"webapp":{"webappName":"cavion17","path":"ud1","isQA":true},
"propertyFiles":"vchannel",
"propertytText":"demo property"}
我尝试删除 RequestBody 注释,然后能够命中服务 ,但参数对象被接收为空。
那么请建议如何在恢复控制器中检索对象?
你不能在 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";
}
在此处阅读更多内容使用 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";
}