《Spring學習筆記-MVC》系列文章,講解返回json數據的文章共有3篇,分別為:
- 【Spring學習筆記-MVC-3】SpringMVC返回Json數據-方式1:http://www.cnblogs.com/ssslinppp/p/4528892.html
- 【Spring學習筆記-MVC-4】返回Json數據-方式2:http://www.cnblogs.com/ssslinppp/p/4530002.html
- 【Spring學習筆記-MVC-3.1】SpringMVC返回Json數據-方式1-擴展:http://www.cnblogs.com/ssslinppp/p/4675495.html
文章的內容主要如下:
- 方式1:講解如果返回單個對象的json;==>使用@ResponseBody來實現;注解方式
- 方式2:講解如果返回多個對象的json;==>使用MappingJacksonJsonView來實現;xml配置方式
- 方式1-擴展:講解如果返回多個對象的json;==>使用@ResponseBody來實現;注解方式
個人認為,使用
@ResponseBody方式來實現json數據的返回比較方便,推薦使用。
- 【Spring學習筆記-MVC-3】SpringMVC返回Json數據-方式1:http://www.cnblogs.com/ssslinppp/p/4528892.html
- 【Spring學習筆記-MVC-4】返回Json數據-方式2:http://www.cnblogs.com/ssslinppp/p/4530002.html
- 【Spring學習筆記-MVC-3.1】SpringMVC返回Json數據-方式1-擴展:http://www.cnblogs.com/ssslinppp/p/4675495.html
文章的內容主要如下:
- 方式1:講解如果返回單個對象的json;==>使用@ResponseBody來實現;注解方式
- 方式2:講解如果返回多個對象的json;==>使用MappingJacksonJsonView來實現;xml配置方式
- 方式1-擴展:講解如果返回多個對象的json;==>使用@ResponseBody來實現;注解方式
個人認為,使用
@ResponseBody方式來實現json數據的返回比較方便,推薦使用。
摘要
使用Spring MVC,實現json數據的返回。
主要步驟:
- 額外添加2個jar包;
- 使用 @ResponseBody聲明返回值;
- 配置<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 /> 會自動注冊
- DefaultAnnotationHandlerMapping
- AnnotationMethodHandlerAdapter
兩個bean,這兩個bean是spring MVC為@Controllers分發請求所必須的。
並提供了數據綁定支持,@NumberFormatannotation支持,@DateTimeFormat支持,@Valid支持,讀寫XML的支持(JAXB),讀寫JSON的支持(Jackson)。
需要的jar包

上面兩個都是必須的。

項目結構


程序代碼
Shop.java
package com.ll.model;
public class Shop {
String name;
String staffName[];
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String[] getStaffName() {
return staffName;
}
public void setStaffName(String[] staffName) {
this.staffName = staffName;
}
}
JSONController.java
package com.ll.controller;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ll.model.Shop;
@Controller
@RequestMapping("/kfc/brands")
public class JSONController {
@RequestMapping(value="{name}", method = RequestMethod.GET)
public @ResponseBody Shop getShopInJSON(@PathVariable String name) {
System.out.println("-----請求json數據--------");
Shop shop = new Shop();
shop.setName(name);
shop.setStaffName(new String[]{"mkyong1", "mkyong2"});
return shop;
}
}
添加: @ResponseBody作為返回值。
web.xml
<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>Spring Web MVC Application</display-name>
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>mvc-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>
mvc-dispatcher-servlet.xml
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd">
<context:component-scan base-package="com.ll.controller" />
<mvc:annotation-driven />
</beans>
開啟:<mvc:annotation-driven />
applicationContext.xml
<?xml version="1.0" encoding="UTF-8" ?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:tx="http://www.springframework.org/schema/tx"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.0.xsd
http://www.springframework.org/schema/tx
http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
http://www.springframework.org/schema/aop
http://www.springframework.org/schema/aop/spring-aop-3.0.xsd">
<!-- 掃描類包,將標注Spring注解的類自動轉化Bean,同時完成Bean的注入 -->
<context:component-scan base-package="com.ll.model"/>
</beans>
運行

參考網站
http://www.mkyong.com/spring-mvc/spring-3-mvc-and-json-example/
附件列表