一.SpringMVC之JSON数据返回
1.在pom.xml导入Json依赖
<dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.9.3</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.9.3</version> </dependency> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> <version>2.9.3</version> </dependency>
2.配置(spring-mvc.xml)处理中文乱码以及避免IE执行AJAX时,返回JSON出现下载文件
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter"> <property name="messageConverters"> <list> <ref bean="mappingJackson2HttpMessageConverter"/> </list> </property> </bean> <bean id="mappingJackson2HttpMessageConverter" class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter"> <!--处理中文乱码以及避免IE执行AJAX时,返回JSON出现下载文件--> <property name="supportedMediaTypes"> <list> <value>text/html;charset=UTF-8</value> <value>text/json;charset=UTF-8</value> <value>application/json;charset=UTF-8</value> </list> </property> </bean>
3.@ResponseBody注解使用
@ResponseBody
注解的作用是将Controller的方法返回的对象通过适当的转换器转换为指定的格式之后,写入到response对象的body区,通常用来返回JSON
数据或者是XML数据。注意:在使用此注解之后不会再走视图解析器,而是直接将数据写入到输入流中,他的效果等同于通过response对象输出指定格式的数据。
4.导入JsonController(模拟数据生成)
package com.lz.controller; import com.lz.Biz.BookBiz; import com.lz.model.Book; import com.lz.utlis.PageBean; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.ResponseBody; import javax.servlet.http.HttpServletRequest; import java.util.HashMap; import java.util.List; import java.util.Map; @Controller @RequestMapping("/book/json") public class JsonController { @Autowired private BookBiz bookBiz; /** * 返回List<T> * @param req * @param book * @return */ @ResponseBody @RequestMapping("/list") public List<Book> list(HttpServletRequest req, Book book){ PageBean pageBean = new PageBean(); pageBean.setRequest(req); List<Book> lst = this.bookBiz.listPager(book, pageBean); return lst; } /** * 返回T * @param req * @param book * @return */ @ResponseBody @RequestMapping("/load") public Book load(HttpServletRequest req, Book book){ if(book.getBid() != null){ List<Book> lst = this.bookBiz.listPager(book, null); return lst.get(0); } return null; } /** * 返回List<Map> * @param req * @param book * @return */ @ResponseBody @RequestMapping("/mapList") public List<Map> mapList(HttpServletRequest req,Book book){ PageBean pageBean = new PageBean(); pageBean.setRequest(req); List<Map> lst = this.bookBiz.mapListPager(book, pageBean); return lst; } /** * 返回Map * @param req * @param book * @return */ @ResponseBody @RequestMapping("/mapLoad") public Map mapLoad(HttpServletRequest req, Book book){ if(book.getBid() != null){ List<Map> lst = this.bookBiz.mapListPager(book, null); return lst.get(0); } return null; } @ResponseBody @RequestMapping("/all") public Map all(HttpServletRequest req, Book clazz){ PageBean pageBean = new PageBean(); pageBean.setRequest(req); List<Book> lst = this.bookBiz.listPager(clazz, pageBean); Map map = new HashMap(); map.put("lst",lst); map.put("pageBean",pageBean); return map; } @ResponseBody @RequestMapping("/jsonStr") public String jsonStr(HttpServletRequest req, Book book){ return "clzEdit"; } }
4.1查询所有Json数据以数组形式输出
4.2查询单个Json对象
4.3通过mapList查询所有Json数据以数组形式输出
4.4通过mapLoad查询单个Json对象
4.5查询混合对象Json对象含有Json数组
4.6返回jsso字符串
5.优化将每一个方法的@ResponseBody注释
5.1注释后情况