java Spring MVC-REST POST-错误请求 400

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27070950/
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-02 11:09:40  来源:igfitidea点击:

Spring MVC- REST POST - Bad Request 400

javaspringrestpost

提问by Alan

I am trying to post a request to my service, but it's not working. I am getting 400 Bad Request. I have GET requests that are working perfectly in the same controller.

我正在尝试向我的服务发布请求,但它不起作用。我得到400 Bad Request. 我有在同一个控制器中完美运行的 GET 请求。

Here is the method:

这是方法:

@RequestMapping(value = "/assign", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE)
    @ResponseBody
    public Form5398Obj arriveTrip(@PathVariable String siteId,
                @RequestBody ErrorMsg anError) throws Exception {

        System.out.println(anError.toString());

    }

The ErrorMessage java class is as follows:

ErrorMessage java类如下:

public class ErrorMsg {

    private String code;
    private String msg;
    private String request;

    public ErrorMsg(String code, String msg, String request)
    {
        this.code = code;
        this.msg = msg;
        this.request = request;
    }
    public String getCode() {
        return code;
    }
    public void setCode(String code) {
        this.code = code;
    }
    public String getMsg() {
        return msg;
    }
    public void setMsg(String msg) {
        this.msg = msg;
    }
    public String getRequest() {
        return request;
    }
    public void setRequest(String request) {
        this.request = request;
    }

}

I did not configure anything else. What else do I need to do to get it to work? I am using JavaConfig, do I need to add any bean declarations?

我没有配置任何其他东西。我还需要做什么才能让它工作?我正在使用 JavaConfig,是否需要添加任何 bean 声明?

I am sending: with Content-Type: application/json

我正在发送:内容类型:应用程序/json

{
                "code" : "101",
                "msg" : "Hello Test",
                "request" : "1"
}

回答by Adam

I believe you need a no-argument constructor for ErrorMsg so that Hymanson can instantiate an object to populate for the incoming request. Otherwise it would not know how the parameters in your 3 parameter constructor should be populated.

我相信您需要一个 ErrorMsg 的无参数构造函数,以便 Hymanson 可以实例化一个对象来填充传入的请求。否则它不会知道应该如何填充 3 参数构造函数中的参数。

Try adding the following

尝试添加以下内容

public ErrorMsg() {
}