简体   繁体   English

Spring MVC- REST POST - 错误请求400

[英]Spring MVC- REST POST - Bad Request 400

I am trying to post a request to my service, but it's not working. 我正在尝试向我的服务发布请求,但它无效。 I am getting 400 Bad Request . 我收到400 Bad Request I have GET requests that are working perfectly in the same controller. 我有完全在同一个控制器中工作的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 我发送:使用Content-Type:application / json

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

I believe you need a no-argument constructor for ErrorMsg so that Jackson can instantiate an object to populate for the incoming request. 我相信你需要一个没有参数的ErrorMsg构造函数,以便Jackson可以实例化一个对象来填充传入的请求。 Otherwise it would not know how the parameters in your 3 parameter constructor should be populated. 否则,它不知道如何填充3参数构造函数中的参数。

Try adding the following 尝试添加以下内容

public ErrorMsg() {
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM