ModelAndView return data format Json

The first way:

1. custom class JacksonUtil.java, tojson method implemented in the class (type of forthcoming data into json); 
2. custom class that inherits JsonView AbstractViews 
3.XML configure the bean: JSONView 
4. configured to BeanNameViewResolver spring bean class is the viewName select the appropriate bean name 
Usage: ModelAndView mv = new ModelAndView ( " jsonView")

 

The second way:

Use annotations @ResponseBody 
need to add support jar package jackson.jar package. 
Example: public @ResponseBody String test (String arg0  )

 

The third way:

In the spring boot types, you can use

ModelAndView modelAndView = new ModelAndView(new MappingJackson2JsonView()).addAllObjects(jsonData.toMap());

Guess you like

Origin www.cnblogs.com/zsh-blogs/p/10993682.html