Spring 3.0 making JSON response using jackson message converter

后端 未结 8 1953
迷失自我
迷失自我 2020-12-01 05:20

i configure my messageconverter as Jackson\'s then

class Foo{int x; int y}

and in controller

@ResponseBody
public Foo metho         


        
相关标签:
8条回答
  • 2020-12-01 06:22

    I guess that 404 is not related to your HttpMessageConverter. I had same 404-issue and the reason was that I forgot that only requests matching <url-pattern> are sent to DispatcherServlet (I changed request mapping from *.do to *.json). Maybe this is your case also.

    0 讨论(0)
  • 2020-12-01 06:22

    In addition to the answers here..

    if you are using jquery on the client side, this worked for me:

    Java:

    @RequestMapping(value = "/ajax/search/sync") 
    public ModelAndView sync(@RequestBody Foo json) {
    

    Jquery (you need to include Douglas Crockford's json2.js to have the JSON.stringify function):

    $.ajax({
        type: "post",
        url: "sync", //your valid url
        contentType: "application/json", //this is required for spring 3 - ajax to work (at least for me)
        data: JSON.stringify(jsonobject), //json object or array of json objects
        success: function(result) {
            //do nothing
        },
        error: function(){
            alert('failure');
        }
    });
    
    0 讨论(0)
提交回复
热议问题