Search code examples
jsonrestjacksonspring2.x

how to create jackson based json rest service with Spring 2


Spring 3 has native json support for returning json response using @ResponseBody spring 3 annotation.

My app is based on spring 2 and need to create jackson based rest service which will return json when client make http rest request using browser.

I am exploring how to achieve this. Any body suggestions on this is appreciated.

Thanks


Solution

  • In Spring 3 we have default converter which converts any object (using Jackson) to JSON. We can override this default converter to define some specials settings. In Spring 2 we can implement View. You can implement this view using Jackson library to convert any object to JSON.

    Your controller could look like that:

    public ModelAndView generateJson(.....) {
         //business logic
    
         return new ModelAndView(new JsonView(objectToConvert);
    }