Spring mvc 使用 jackson2 返回json格式数据时数据的构成
来源:互联网 发布:mac pro 15寸壁纸尺寸 编辑:程序博客网 时间:2024/05/21 22:26
xml:
<bean id="viewResolver" class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver"> <property name="contentNegotiationManager" ref="cnManager"/> <property name="viewResolvers"><!-- 针对freemarker的视图配置 --> <list> <bean class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver"> <property name="viewClass" value="org.springframework.web.servlet.view.freemarker.FreeMarkerView"/> <!-- <property name="viewNames" value=".ftl"/>--> <property name="contentType" value="text/html; charset=utf-8"/> <property name="cache" value="true" /> <property name="prefix" value="" /> <property name="suffix" value=".ftl" /> <property name="order" value="2"/> </bean> <!--<bean id="liteDeviceDelegatingViewResolver" class="org.springframework.mobile.device.view.LiteDeviceDelegatingViewResolver">--> <!--<constructor-arg>--> <!--<bean--> <!--class="org.springframework.web.servlet.view.freemarker.FreeMarkerViewResolver">--> <!--<property name="cache" value="true"/>--> <!--<property name="requestContextAttribute" value="rc"/>--> <!--<property name="prefix" value=""/>--> <!--<property name="suffix" value=".ftl"/>--> <!--<property name="exposeSpringMacroHelpers" value="true"/>--> <!--<property name="exposeRequestAttributes" value="true"/>--> <!--<property name="exposeSessionAttributes" value="true"/>--> <!--<property name="contentType" value="text/html;charset=UTF-8"/>--> <!--</bean>--> <!--</constructor-arg>--> <!--<property name="mobilePrefix" value="mobile/" />--> <!--<property name="tabletPrefix" value="tablet/" />--> <!--<property name="enableFallback" value="true" />--> <!--</bean>--> <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver"> <property name="prefix" value="/WEB-INF/view/jsp/"></property><!-- 配置页面路径 --> <property name="suffix" value=".jsp"></property><!-- 文件以value值结尾 --> </bean> </list> </property> <property name="defaultViews"> <list> <bean class="org.springframework.web.servlet.view.json.MappingJackson2JsonView"> <!-- <property name="extractValueFromSingleKeyModel" value="true"/> --> <property name="prettyPrint" value="true"/> <property name="contentType" value="text/plain"/> </bean> <bean class="org.springframework.web.servlet.view.xml.MarshallingView"> <constructor-arg ref="jaxb2Marshaller"> </constructor-arg> </bean> <bean class="com.wonders.stpt.bid.controller.JXLExcelView"/> </list> </property> </bean> <bean id="jaxb2Marshaller" class="org.springframework.oxm.jaxb.Jaxb2Marshaller"> <property name="classesToBeBound"> <list> <value>com.wonders.stpt.bid.domain.Route</value> <value>com.wonders.stpt.bid.domain.Dictionary</value> </list> </property> </bean>
方法1:
@RequestMapping(method = RequestMethod.GET, value = "/dictionaries") public void dictionaries( Model model, String id) throws Exception { Dictionary dictionary = new Dictionary(); dictionary.setParentNo(id); List<Dictionary> dictionaries = dictionaryService.getDictionaries(dictionary); List list = new ArrayList(); for (Dictionary dict : dictionaries) { Map map = new HashMap(); map.put("id", dict.getDictId()); map.put("name", dict.getDictName()); map.put("dictType", dict.getDictType()); map.put("dictNo", dict.getDictNo()); map.put("dictOrder", dict.getDictOrder()); if (dict.getParentNo() == null) map.put("isParent", true); else map.put("isParent", false); list.add(map); } model.addAttribute("root", list); model.addAttribute("maxNo", dictionaryService.getMaxDictNo()); }
json1:
{ "root" : [ { "id" : "7", "dictNo" : "7", "dictOrder" : 1, "name" : "行业", "isParent" : true, "dictType" : "trade" }, { "id" : "5", "dictNo" : "5", "dictOrder" : 2, "name" : "集团", "isParent" : true, "dictType" : "groups" }, { "id" : "6", "dictNo" : "6", "dictOrder" : 3, "name" : "线路", "isParent" : true, "dictType" : "routeType" } ], "currentUser" : { "userId" : "2", "loginName" : "G00100000123", "password" : null, "userName" : "李晴阳", "roles" : [ { "roleId" : null, "roleName" : "ROLE_EDITOR", "description" : null, "removed" : null, "createTime" : null, "creator" : null, "updateTime" : null, "updater" : null } ] }, "maxNo" : 20
方法2:
@RequestMapping(method = RequestMethod.GET, value = "/dictionaries") public void dictionaries(Dictionary dictionary, Model model, String id) throws Exception { dictionary.setParentNo(id); List<Dictionary> dictionaries = dictionaryService.getDictionaries(dictionary); List list = new ArrayList(); for (Dictionary dict : dictionaries) { Map map = new HashMap(); map.put("id", dict.getDictId()); map.put("name", dict.getDictName()); map.put("dictType", dict.getDictType()); map.put("dictNo", dict.getDictNo()); map.put("dictOrder", dict.getDictOrder()); if (dict.getParentNo() == null) map.put("isParent", true); else map.put("isParent", false); list.add(map); } model.addAttribute("root", list); model.addAttribute("maxNo", dictionaryService.getMaxDictNo()); }
json2:
{ "dictionary" : { "dictId" : null, "dictNo" : null, "dictName" : null, "dictType" : null, "dictOrder" : null, "parentNo" : null, "createTime" : null, "updateTime" : null, "removed" : null, "creator" : null, "updater" : null }, "root" : [ { "id" : "7", "dictNo" : "7", "dictOrder" : 1, "name" : "行业", "isParent" : true, "dictType" : "trade" }, { "id" : "5", "dictNo" : "5", "dictOrder" : 2, "name" : "集团", "isParent" : true, "dictType" : "groups" }, { "id" : "6", "dictNo" : "6", "dictOrder" : 3, "name" : "线路", "isParent" : true, "dictType" : "routeType" } ], "currentUser" : { "userId" : "2", "loginName" : "G00100000123", "password" : null, "userName" : "李晴阳", "roles" : [ { "roleId" : null, "roleName" : "ROLE_EDITOR", "description" : null, "removed" : null, "createTime" : null, "creator" : null, "updateTime" : null, "updater" : null } ] }, "maxNo" : 20}
可以看到,除了方法中的Model被转换了,另一个参数 Dictionary也被转换了。
1 0
- Spring mvc 使用 jackson2 返回json格式数据时数据的构成
- Spring MVC返回JSON格式的数据
- Spring MVC返回JSON格式的数据
- 使用spring MVC返回json格式的数据
- 使用spring-mvc 框架返回json格式数据 报406
- 封装Spring MVC 返回JSON 格式数据
- spring mvc 返回json格式数据的方式
- spring mvc 对返回json格式数据的支持
- Spring mvc + jackson2 返回json格式(包含日期格式解析)
- Spring MVC返回XML格式的数据
- Spring MVC返回XML格式的数据
- Spring MVC返回json数据的方法
- Spring MVC返回json数据的方法
- spring mvc返回json 类型的 数据
- Spring MVC接受JSON格式的数据
- Spring MVC接受JSON格式的数据
- spring mvc实现Restful返回json格式数据
- spring mvc实现Restful返回json格式数据
- onscroll如何实现层随页面滚动
- 问题 : 用指针求两数的和与两数的差
- 第十五周OJ平台 指针当形参
- leetcode——4Sum
- 2014-12-04-sqlserver管理器之sqlserver服务无法启动
- Spring mvc 使用 jackson2 返回json格式数据时数据的构成
- Android属性
- Hibernate中用hql查询部分字段 可解决异常java.lang.ClassCastException: cannot be cast to
- stm32的ucosII加上ucGUI学习
- 第十五周项目1打入“内部”寻“内幕”
- 可以旋转文字的验证码
- 程序员的出路
- 交叉编译busybox
- 指针当形参-变量交换