Spring mvc 使用 jackson2 返回json格式数据时数据的构成

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
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值