springmvc對於JSON對象的處理


1、常見的json    jar包,及其優缺點(開發中可以一起使用)

 

  json-lib           缺點:依賴第三方的包
  jackson        SpringMVC內置的json裝換工具,依賴包較少
  GSON          谷歌開源jar包,功能最強大,不依賴任何包
  fastjson        阿里巴巴開源jar包,效率最高,不依賴任何報

2、在需要返回的方法前面加上     @ResponseBody注解

 

 1 @RequestMapping(value = "/user/userview.html", method = RequestMethod.POST)
 2     @ResponseBody
 3     public Object getUserById(@RequestParam String uid) {
 4         String cjson = "";
 5         if (StringUtils.isNullOrEmpty(uid)) {
 6             return "nodata";
 7         } else {
 8             try {
 9                 User user = userService.getUserById(uid);
10                 cjson = JSON.toJSONString(user);
11             } catch (Exception e) {
12                 e.printStackTrace();
13                 return "failed";
14             }
15         }
16         return cjson;
17     }

 

3、對日期格式的處理

[{"address":"北京海淀","age":5,"birthday":1338307200000,"createdBy":0,"gender":2,"id":19,"modifyBy":0,"phone":"13878907654","userCode":"admin","userName":"系統管理員","userType":1}]

在查看日期格式的內容時,我們發現日期格式不是我們想要的

 解決辦法1:

在對應實體類的日期加上注解:針對阿里巴巴的  fastjson---------------@JSONField(format="yyyy-MM-dd"),缺點是依賴性太強,強耦合

解決方法2:

配置視圖解析圖

在springMVC配置文件中配置 

 1 <!-- 配置多視圖解析器,允許用同樣的內容數據來呈現不同的view -->
 2         <bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
 3             <!-- 支持參數匹配 -->
 4             <property name="favorParameter" value="true"/>
 5             <!-- contentType 以何種格式進行展示 -->
 6             <property name="mediaTypes">
 7                 <map>
 8                     <entry key="html" value="text/html;charset=UTF-8"/>
 9                     <entry key="xml" value="application/xml;charset=UTF-8"/>
10                     <entry key="json" value="application/json;charset=UTF-8"/>
11                 </map>
12             </property>
13             <property name="viewResolvers">
14                 <list>
15                     <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
16                         <property name="prefix" value="/WEB-INF/jsp/"/>
17                         <property name="suffix" value=".jsp"/>
18                     </bean>
19                 </list>
20             </property>
21         </bean>
@RequestMapping(value = "/user/userview2", method = RequestMethod.GET)
    @ResponseBody
    public Object getUserById2(@RequestParam String uid) {
        User user = new User();
        try {
            user = userService.getUserById(uid);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return user;
    }

訪問路徑:兩種方式:(可以訪問json、xml、html、xls....)

(1)、使用擴展名:   http://localhost:8080/ShopSystem/user/userview2.json?uid=19

(2)、使用參數(favorParameter):   http://localhost:8080/ShopSystem/user/userview2?uid=19&format=json

注:訪問xml時需要在對應實體類配置注解       @XmlRootElement

 頁面輸出:

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM