0

My spring3.1 mvc server can response right json result by add @ResponseBody, should no problem on spring setting file. The servlet.xml included

<mvc:annotation-driven/>

I put jackson jars in classpath.

And my html file also set contentType to 'application/json" in .ajaxSetup() method. And I can see post header is right in Firebug.

I also tried include json2.js to call JSON.stringify method.

The json format sample:

{"link":"http:xxxxx","ticker":"xxxxx"}

The responseBody class is Document:

public class Document implements Serializable{
   public String getLink(){...}
   public String setLink(String link){...}
   public String getTicker(){...}
   public String setTicker(String ticker){...}
}

The controller code:

public class LoadFile  {                                                                                                                                                                                                                                                                                                                                                                         

    @RequestMapping(value="/loadFile",method=RequestMethod.POST)                                                                                                                                                  
    public @ResponseBody Document loadFile(@RequestBody Document document){ 
        ...
    }
}

The html code:

$(function() {                                                                                                                                                                                                   
    $.ajaxSetup({                                                                                                                                                                                               
        contentType:"application/json;charset=utf-8" 
    });                                                                                                                                                       
    $.post( url, { link: link, ticker:ticker },                                                                                                                                            
        function( data ) {  
           ....
        },"json"
    );
}

Document class can return as a json object when added @ResponseBody, but when as a @RequestBody, the server returns 400 error. Not sure what happens.

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
atu0830
  • 405
  • 7
  • 15
  • can you provide more code, javascript and controller code ? – insomiac Oct 16 '12 at 17:40
  • fixed by reference [link] http://stackoverflow.com/questions/5908466/jquery-spring-mvc-requestbody-and-json-making-it-work-together/5908847#5908847 I guess gae jackson lib have some conflict with spring, so have to explicit the converter package name. But still use RequestBody, not ModelAttribute – atu0830 Oct 17 '12 at 17:03

2 Answers2

0

I've had a similar problem recently. For me, it was adding the "" around the variable names which gave me the 400 error. Try it like this:

{link:"http:xxxxx",ticker:"xxxxx"}

Also make sure that your variables correspond to your database, depending how Spring3.1 handles the data. (e.g in the correct order)

I hope that helps you.

Dean Marsden
  • 207
  • 1
  • 9
  • sorry, my json sample is from firebug. but code is {link:"http:xxxxx",ticker:"xxxxx"}, so should not be the cause – atu0830 Oct 16 '12 at 18:32
0

replace @RequestBody by @ModelAttribute

storm_buster
  • 7,362
  • 18
  • 53
  • 75
  • the method called but document's property is null. Base spring document, it can create document from form or session. How can create document from jquery post? – atu0830 Oct 16 '12 at 20:31
  • it should work, make sure that link and ticker have a value. you can look at them on firebug – storm_buster Oct 16 '12 at 21:23
  • firebug show the two parameter have right value but server side's document still do not have any property. – atu0830 Oct 16 '12 at 23:49