Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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. I have GET requests that are working perfectly in the same controller.

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:

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?

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

{
                "code" : "101",
                "msg" : "Hello Test",
                "request" : "1"
}
like image 833
Alan Avatar asked Dec 19 '22 09:12

Alan


1 Answers

I believe you need a no-argument constructor for ErrorMsg so that Jackson 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.

Try adding the following

public ErrorMsg() {
}
like image 58
Adam Avatar answered Dec 29 '22 23:12

Adam