java Spring Boot Rest 控制器未将请求正文转换为自定义对象

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/49400507/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-03 10:21:34  来源:igfitidea点击:

Spring boot rest controller not converting request body to custom object

javaspringspring-bootspring-data-rest

提问by Ansar Samad

I have spring boot application which used spring rest controller . This is the controller , below is the response an getting. Am using postman tool for sending request to this controller. And am sending content type as application/json

我有使用弹簧休息控制器的弹簧启动应用程序。这是控制器,下面是得到的响应。正在使用邮递员工具向该控制器发送请求。并将内容类型发送为 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 : Resolved exception caused by Handler execution: org.springframework.http.converter.HttpMessageNotReadableException: I/O error while reading input message; nested exception is java.io.IOException: Stream closed

2018-03-21 12:18:47.732?警告 8520 --- [nio-8099-exec-3] .wsmsDefaultHandlerExceptionResolver : 处理程序执行引起的已解决异常: org.springframework.http.converter.HttpMessageNotReadableException: I/O error while reading input message; 嵌套异常是 java.io.IOException: Stream closed

This is my postman request

这是我的邮递员要求

{
"webapp":{"webappName":"cavion17","path":"ud1","isQA":true},
"propertyFiles":"vchannel",
"propertytText":"demo property"}

I tried by removing the RequestBody annotation, then able to hit the service , but param objects are received as null.

我尝试通过删除 RequestBody 注释,然后能够点击 service ,但 param 对象被接收为空。

So please suggest how to retrieve objects in the restcontroller?

那么请建议如何在restcontroller中检索对象?

回答by Yuvraj Jaiswal

You cannot use multiple @RequestBodyannotations in Spring. You need to wrap all these in an object.

您不能@RequestBody在 Spring 中使用多个注释。您需要将所有这些包装在一个对象中。

Some like this

有些像这样

// some imports here
public class IncomingRequestBody {
    private Webapp webapp;
    private String propertryFiles;
    private String propertyText;

    // add getters and setters here
}

And in your controller

在你的控制器中

@RequestMapping(value = "/test", method = RequestMethod.POST)
public String test(@RequestBody IncomingRequestBody requestBody) {
    System.out.println(requestBody.getPropertyFiles());
    // other statement
    return "ok good";
}

Read more here Passing multiple variables in @RequestBody to a Spring MVC controller using Ajax

在此处阅读更多信息将 @RequestBody 中的多个变量传递给使用 Ajax 的 Spring MVC 控制器

回答by geneqew

Based on the sample postman payload you gave, you will need:

根据您提供的示例邮递员负载,您将需要:

public class MyObject {

    private MyWebapp  webapp;
    private String propertyFiles;
    private String propertytText;

    // your getters /setters here as needed
}

and

public class MyWebapp {
    private String webappName;
    private String path;
    private boolean isQA;

    // getters setters here
}

Then on your controller change it to:

然后在您的控制器上将其更改为:

@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";
}