最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Spring MVC學(xué)習(xí)筆記之json格式的輸入和輸出

 更新時(shí)間:2017年03月02日 15:49:43   作者:種菜得瓜  
本篇文章主要介紹了Spring MVC學(xué)習(xí)筆記之json格式的輸入和輸出,這里整理了詳細(xì)的代碼,有需要的小伙伴可以參考下。

Spring mvc處理json需要使用jackson的類庫,因此為支持json格式的輸入輸出需要先修改pom.xml增加jackson包的引用

    <!-- json -->
    <dependency>
      <groupId>org.codehaus.jackson</groupId>
      <artifactId>jackson-core-lgpl</artifactId>
      <version>1.8.1</version>
    </dependency>

    <dependency>
      <groupId>org.codehaus.jackson</groupId>
      <artifactId>jackson-mapper-lgpl</artifactId>
      <version>1.8.1</version>
    </dependency>

先修改之前的helloworld.jsp,增加客戶端json格式的數(shù)據(jù)輸入。

  var cfg =   {
    type: 'POST', 
    data: JSON.stringify({userName:'winzip',password:'password',mobileNO:'13818881888'}), 
    dataType: 'json',
    contentType:'application/json;charset=UTF-8',    
    success: function(result) { 
      alert(result.success); 
    } 
  };

function doTestJson(actionName){
  cfg.url = actionName;
  $.ajax(cfg);
}

根據(jù)前面的分析,在spring mvc中解析輸入為json格式的數(shù)據(jù)有兩種方式 1:使用@RequestBody來設(shè)置輸入

  @RequestMapping("/json1")
  @ResponseBody
  public JsonResult testJson1(@RequestBody User u){
    log.info("get json input from request body annotation");
    log.info(u.getUserName());
    return new JsonResult(true,"return ok");
}

2:使用HttpEntity來實(shí)現(xiàn)輸入綁定 

  @RequestMapping("/json2")  
  public ResponseEntity<JsonResult> testJson2(HttpEntity<User> u){
    log.info("get json input from HttpEntity annotation");
    log.info(u.getBody().getUserName());
    ResponseEntity<JsonResult> responseResult = new ResponseEntity<JsonResult>( new JsonResult(true,"return ok"),HttpStatus.OK);
    return responseResult;
}

Json格式的輸出也對(duì)應(yīng)有兩種方式 1:使用@responseBody來設(shè)置輸出內(nèi)容為context body 2:返回值設(shè)置為ResponseEntity<?>類型,以返回context body 另外,第三種方式是使用ContentNegotiatingViewResolver來設(shè)置輸出為json格式,需要修改servlet context配置文件如下

  <bean
    class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
    <property name="order" value="1" />
    <property name="mediaTypes">
      <map>
        <entry key="json" value="application/json" />
      </map>
    </property>
    <property name="defaultViews">
      <list>
        <bean
          class="org.springframework.web.servlet.view.json.MappingJacksonJsonView" />
      </list>
    </property>
    <property name="ignoreAcceptHeader" value="true" />
  </bean>

但這種格式的輸出會(huì)返回{model類名:{內(nèi)容}} 的json格式, 例如,以下代碼

  @RequestMapping("/json3.json")
  public JsonResult testJson3(@RequestBody User u){
    log.info("handle json output from ContentNegotiatingViewResolver");
    return new JsonResult(true,"return ok");
  }

期望的返回是 {success:true,message:”return ok”}; 但實(shí)際返回的卻是 {"jsonResult":{"success":true,"msg":"return ok"}} 原因是MappingJacksonJsonView中對(duì)返回值的處理未考慮modelMap中只有一個(gè)值的情況,直接是按照mapName:{mapResult}的格式來返回?cái)?shù)據(jù)的。 修改方法,重載MappingJacksonJsonView類并重寫filterModel方法如下

  protected Object filterModel(Map<String, Object> model) { 
    Map<?, ?> result = (Map<?, ?>) super.filterModel(model); 
    if (result.size() == 1) { 
      return result.values().iterator().next(); 
    } else { 
      return result; 
    } 
  } 

 對(duì)應(yīng)的ContentNegotiatingViewResolver修改如下

<bean
    class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
    <property name="order" value="1" />
    <property name="mediaTypes">
      <map>
        <entry key="json" value="application/json" />
      </map>
    </property>
    <property name="defaultViews">
      <list>
        <bean
          class="net.zhepu.json.MappingJacksonJsonView" />
      </list>
    </property>
    <property name="ignoreAcceptHeader" value="true" />
  </bean>

以上就是本文的全部內(nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。

相關(guān)文章

最新評(píng)論

陈巴尔虎旗| 农安县| 普宁市| 葫芦岛市| 南安市| 宁德市| 来安县| 长汀县| 武功县| 凯里市| 莱州市| 吴桥县| 会宁县| 崇义县| 宜川县| 屯门区| 徐州市| 台山市| 阳江市| 崇信县| 仙居县| 邵阳县| 六盘水市| 祁连县| 东丰县| 罗甸县| 青海省| 灵川县| 昔阳县| 望江县| 南部县| 博罗县| 左权县| 雷波县| 甘泉县| 义马市| 龙门县| 崇州市| 许昌县| 天峨县| 宁强县|