spring: return JSON from controller as ModelAndVIew

前端 未结 2 1187
南旧
南旧 2020-12-14 07:55

How can I return JSON from spring controller as a view or ModelAndView? I am not interested in using @ResponseBody annotation. Is there any other w

相关标签:
2条回答
  • 2020-12-14 08:28

    Yes, you can return a MappingJacksonJsonView or assign it to a ModelAndView object using mav.setView(new MappingJacksonJsonView()).

    Update: In Spring 4 MappingJacksonJsonView is deprecated. You might want to upgrade to MappingJackson2JsonView

    0 讨论(0)
  • 2020-12-14 08:53

    You can use org.codehaus.jackson.map.ObjectMapper class for writing json strings to output stream. Use the following code snippet in method of your controller:

    ObjectMapper mapper = new ObjectMapper();
    mapper.writeValue(outputStream, model);
    outputStream.flush();
    
    0 讨论(0)
提交回复
热议问题