views:

13

answers:

1

In my webapp, all my message converters are in place, and if I change getContent below to return a bean/pojo, it returns as " application/json;charset=UTF-8", which is expected, but I now want to serve JSON "as is".

E.g. I have a simple stub web service with which users can PUT a blob of JSON content which is persisted somewhere, and then an equivalent GET call to read it back.

@Controller
public class StubController {

    @Autowired
    @Qualifier("keyValueStore")
    private KVStore kv;

    @RequestMapping(value = "/stub/{id}", method = RequestMethod.GET)
    public @ResponseBody
    String getContent(@PathVariable("id") final String id) {
        return kv.get(id);
    }

    @RequestMapping(value = "/stub/{id}", method = RequestMethod.PUT)
    public String putContent(@PathVariable("id") final String id, @RequestBody String body) {
        kv.set(id, body);
        return "redirect:/stub/"+id;
    }

}

However, the getter returns header "Content-Type: text/html;charset=UTF-8" if I call http://host/stub/123.json in the browser. My guess that this is happening is because I'm not returning anything that is "converted" by the Jackson converter, hence the return header isn't modified.

I need it to be application/json -- any ideas what to do? Perhaps an annotation with which I can specify the return headers?

A: 

I managed to get around this by adding an HttpServletResponse param to my getContent() method and setting the content type directly.

http://forum.springsource.org/showthread.php?t=97140

    @RequestMapping(value = "/stub/{id}", method = RequestMethod.GET)
    public @ResponseBody String getContent(@PathVariable("id") final String id, HttpServletResponse response) {
        response.setContentType("application/json");
        return kv.get(id);
    }
opyate