Java spring framework - how to set content type?

 @RequestMapping(value = "jsonDemoDude", method = RequestMethod.GET)
    public void getCssForElasticSearchConfiguration(HttpServletResponse response) throws IOException {        
        String jsonContent= ...;
        HttpServletResponseWrapper wrapper = new HttpServletResponseWrapper(response);
        wrapper.setContentType("application/json;charset=UTF-8");
        wrapper.setHeader("Content-length", "" + jsonContent.getBytes().length);
        response.getWriter().print(jsonContent);
}

You can also add the aditional X bytes or whatever for "callback" part in case you want JSONP ( cross site json request ) .


Pass the HttpServletResponse to your action method and set the content type there:

public String yourAction(HttpServletResponse response) {
    response.setContentType("application/json");
}

Did you try using the MappingJacksonJsonView?

Spring-MVC View that renders JSON content by serializing the model for the current request using Jackson's ObjectMapper.

It sets the content-type to: application/json.