SpringMVC返回json數據

《Spring學習筆記-MVC》系列文章,講解返回json數據的文章共有3篇,分別爲:
  1. 【Spring學習筆記-MVC-3】SpringMVC返回Json數據-方式1:http://www.cnblogs.com/ssslinppp/p/4528892.html 
  2. 【Spring學習筆記-MVC-4】返回Json數據-方式2:http://www.cnblogs.com/ssslinppp/p/4530002.html 
  3. 【Spring學習筆記-MVC-3.1】SpringMVC返回Json數據-方式1-擴展:http://www.cnblogs.com/ssslinppp/p/4675495.html 
文章的內容主要如下:
  1. 方式1:講解如果返回單個對象的json;==>使用@ResponseBody來實現;註解方式
  2. 方式2:講解如果返回多個對象的json;==>使用MappingJacksonJsonView來實現;xml配置方式
  3. 方式1-擴展:講解如果返回多個對象的json;==>使用@ResponseBody來實現;註解方式
個人認爲,使用 @ResponseBody方式來實現json數據的返回比較方便,推薦使用。



摘要


使用Spring MVC,實現json數據的返回。
主要步驟:
  1. 額外添加2個jar包;
  2. 使用 @ResponseBody聲明返回值;
  3. 配置<mvc:annotation-driven />;==>需要引入:xmlns:mvc="http://www.springframework.org/schema/mvc";

@ResponseBody:

作用:  
該註解用於將Controller方法返回的對象,通過適當的HttpMessageConverter轉換爲指定格式後(如:json格式),寫入到Response對象的body數據區。  
使用時機:
返回的數據不是html標籤的頁面,而是其他某種格式的數據時(如json、xml等)使用;

<mvc:annotation-driven /> :

<mvc:annotation-driven /> 會自動註冊
  1. DefaultAnnotationHandlerMapping
  2. AnnotationMethodHandlerAdapter 
兩個bean,這兩個bean是spring MVC爲@Controllers分發請求所必須的。 並提供了數據綁定支持,@NumberFormatannotation支持,@DateTimeFormat支持,@Valid支持,讀寫XML的支持(JAXB),讀寫JSON的支持(Jackson)。


需要的jar包


上面兩個都是必須的。




項目結構




程序代碼


Shop.java
 
    
  1. package com.ll.model;
  2. public class Shop {
  3. String name;
  4. String staffName[];
  5. public String getName() {
  6. return name;
  7. }
  8. public void setName(String name) {
  9. this.name = name;
  10. }
  11. public String[] getStaffName() {
  12. return staffName;
  13. }
  14. public void setStaffName(String[] staffName) {
  15. this.staffName = staffName;
  16. }
  17. }

JSONController.java

 
    
  1. package com.ll.controller;
  2. import org.springframework.stereotype.Controller;
  3. import org.springframework.web.bind.annotation.PathVariable;
  4. import org.springframework.web.bind.annotation.RequestMapping;
  5. import org.springframework.web.bind.annotation.RequestMethod;
  6. import org.springframework.web.bind.annotation.ResponseBody;
  7. import com.ll.model.Shop;
  8. @Controller
  9. @RequestMapping("/kfc/brands")
  10. public class JSONController {
  11. @RequestMapping(value="{name}", method = RequestMethod.GET)
  12. public @ResponseBody Shop getShopInJSON(@PathVariable String name) {
  13. System.out.println("-----請求json數據--------");
  14. Shop shop = new Shop();
  15. shop.setName(name);
  16. shop.setStaffName(new String[]{"mkyong1", "mkyong2"});
  17. return shop;
  18. }
  19. }

添加: @ResponseBody作爲返回值。


web.xml

 
    
  1. <web-app id="WebApp_ID" version="2.4"
  2. xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  3. xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
  4. http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
  5. <display-name>Spring Web MVC Application</display-name>
  6. <context-param>
  7. <param-name>contextConfigLocation</param-name>
  8. <param-value>classpath:applicationContext.xml</param-value>
  9. </context-param>
  10. <listener>
  11. <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
  12. </listener>
  13. <servlet>
  14. <servlet-name>mvc-dispatcher</servlet-name>
  15. <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
  16. <load-on-startup>1</load-on-startup>
  17. </servlet>
  18. <servlet-mapping>
  19. <servlet-name>mvc-dispatcher</servlet-name>
  20. <url-pattern>/rest/*</url-pattern>
  21. </servlet-mapping>
  22. </web-app>



mvc-dispatcher-servlet.xml

 
    
  1. <beans xmlns="http://www.springframework.org/schema/beans"
  2. xmlns:context="http://www.springframework.org/schema/context"
  3. xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  4. xsi:schemaLocation="
  5. http://www.springframework.org/schema/beans
  6. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  7. http://www.springframework.org/schema/context
  8. http://www.springframework.org/schema/context/spring-context-3.0.xsd
  9. http://www.springframework.org/schema/mvc
  10. http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
  11. <context:component-scan base-package="com.ll.controller" />
  12. <mvc:annotation-driven />
  13. </beans>

開啓:<mvc:annotation-driven />



applicationContext.xml
 
    
  1. <?xml version="1.0" encoding="UTF-8" ?>
  2. <beans xmlns="http://www.springframework.org/schema/beans"
  3. xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
  4. xmlns:context="http://www.springframework.org/schema/context"
  5. xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
  6. xsi:schemaLocation="http://www.springframework.org/schema/beans
  7. http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
  8. http://www.springframework.org/schema/context
  9. http://www.springframework.org/schema/context/spring-context-3.0.xsd
  10. http://www.springframework.org/schema/tx
  11. http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
  12. http://www.springframework.org/schema/aop
  13. http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
  14. <!-- 掃描類包,將標註Spring註解的類自動轉化Bean,同時完成Bean的注入 -->
  15. <context:component-scan base-package="com.ll.model"/>
  16. </beans>


運行



參考網站

http://www.mkyong.com/spring-mvc/spring-3-mvc-and-json-example/