roark
roark

Reputation: 830

POSTing JSON to Spring MVC Controller Returns 400 Bad Request

I'd like to POST JSON and have Jackson convert it to a POJO, but for now I can't even POST a simple String parameter.

  1. I'm using Jackson 2.3.2 (jackson-core and jackson-databaind)

  2. I'm using Spring 3.2.8.RELEASE (spring-context, spring-webmvc)

  3. My Controller looks like this:

    @RequestMapping(value="add-name", method=RequestMethod.POST, headers = "Accept=application/json")
    public String addName(@RequestParam(value = "name", required = false) String name) {
        LOG.info(String.format("name: %s", name));
        return "name";
    }
    
  4. I POST to this Controller with data like this:

    { "name" : "my_name" }
    
  5. The response I receive is 400 Bad Request

Note: I'm submitting the POST via jQuery.post(), but to remove that as a variable, I'm also submitting POST via FF Poster add-on specifying the URL, Content Type (application/json), and data as above.

Edit: I think the issue may be that the Jackson library isn't even being called. I took it out of the dependencies and nothing changes, no different error/exception, I still simply get a 400 Bad Request response.

Edit 2: Even if I take out the parameter to addName completely and post an empty body {} I still get a 400 Bad Request

Upvotes: 2

Views: 10193

Answers (2)

roark
roark

Reputation: 830

I downgraded to Spring 3.1.1.RELEASE and Jackson 1.9.9 and it works now. Seems like a hack. I thought Spring 3.2 and Jackson 2 were compatible.

Upvotes: 2

hsluo
hsluo

Reputation: 1702

You are posting raw data in the body, but your controller is looking for a parameter named name. Either post with a parameter name whose value is my_name, or use @RequestBody in the handler to pass in the JSON object.

Upvotes: 3

Related Questions