2

Client code:

$.ajax({
        type: "POST",
        url: "../web/zittles",
        data: jsonformatdata,
        contentType: "application/json",
        dataType: "json",
    success: function(data)
        {
        alert("data from server : "+data);
        },
        error: function(jqXHR, textStatus, errorThrown)
        {
            alert("jqXHR.status = "+jqXHR.status); //getting status code 400 here
        }        
});

Output json data:

{
        "id": 1,
        "No": "1234",
        "Desc": "Testing"
}

Java class:

public class Fizzle implements Serializable
{
    private String id;
    private String No;
    private String Desc;
    // getters and setters 
}

Spring 3 Controller:

@RequestMapping(value = '/zittles', method = RequestMethod.POST, headers ="Content-Type=application/json")
    public @ResponseBody void doSomeThing (@RequestBody Fizzle fizzle) {

        //do something here
}

app-servlet.xml has

<mvc:annotation-driven/>

/lib folder of tomcat has

jackson-core-lgpl-1.9.10.jar
jackson-mapper-lgpl-1.9.10.jar

Getting error with status code 400 -

"The request sent by the client was syntactically incorrect"

When I change the Controller code as shown below, it takes the json data as string.

public @ResponseBody void doSomeThing (@RequestBody String fizzle) {}

Ideally Jackson should automatically map the json data to Fizzle object.

What is it that I am missing here. Is there anything else that has to be done to configure the Jackson parser correctly?

Help please.

2
  • @RequestBody dumps the body in that variable, remove it and it should work i think. Commented Sep 26, 2012 at 10:34
  • 2
    he wants to convert input json to POJO, so @RequestBody is correct Commented Sep 26, 2012 at 13:50

2 Answers 2

2

Id must be long or int, not a String

 private int id;

Edit

Maybe try this JSON:

{
        "id": 1,
        "no": "1234",
        "desc": "Testing"
}

and change properties to lowercase in Fizzle class

Edit2 Test if this is Jakson issue or Spring issue. Save your JSON to file and try to convert it to object:

ObjectMapper mapper = new ObjectMapper();
Fizzle fizzle = mapper.readValue(new File("c:\\fizzle.json"), Fizzle .class);
Sign up to request clarification or add additional context in comments.

1 Comment

Even changing the properties to lowercase did not work. This is really frustating. As I said in my post, it works when I accept the json data as String. Is there a way I can manually map this json String into my Fizzle object?
0

I am answering my own question since it worked for me. Thanks "vacuum" for giving me the direction. I am using List since I am receving an array of json data.

@JsonTypeName
@RequestMapping(value = '/zittles', method = RequestMethod.POST, headers ="Content-Type=application/json")     
public @ResponseBody void doSomeThing (@RequestBody String fizzles) 
{          
     //do something here
     ObjectMapper mapper = new ObjectMapper();
     ArrayList list = mapper.readValue(new StringReader(fizzles), ArrayList.class);
     List<Fizzle> fizzles1 = mapper.convertValue(list, new TypeReference<List<Fizzle>>() {});
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.