1、SpringMVC如何返回json數(shù)據(jù)
1.1谴忧、添加jar包
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.5</version>
</dependency>
1.2、配置spring文件角虫,添加mvc命名空間和約束等
xmlns:mvc="http://www.springframework.org/schema/mvc"
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd
<mvc:annotation-driven/>
1.3沾谓、方法上添加@ResponseBody
@ResponseBody
2、例子:
2.1戳鹅、配置pom.xml
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.11</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>5.0.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>5.0.8.RELEASE</version>
</dependency>
<!-- Spring的核心工具包-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>5.0.8.RELEASE</version>
</dependency>
<!--在基礎(chǔ)IOC功能上提供擴(kuò)展服務(wù)均驶,還提供許多企業(yè)級服務(wù)的支持,有郵件服務(wù)枫虏、任務(wù)調(diào)度妇穴、遠(yuǎn)程訪問爬虱、緩存以及多種視圖層框架的支持-->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>5.0.8.RELEASE</version>
</dependency>
<!-- Spring IOC的基礎(chǔ)實現(xiàn),包含訪問配置文件腾它、創(chuàng)建和管理bean等 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
<version>5.0.8.RELEASE</version>
</dependency>
<!-- Spring context的擴(kuò)展支持跑筝,用于MVC方面 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context-support</artifactId>
<version>5.0.8.RELEASE</version>
</dependency>
<!-- Spring表達(dá)式語言 -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-expression</artifactId>
<version>5.0.8.RELEASE</version>
</dependency>
<!--springAop開發(fā)必須加入的包-->
<dependency>
<groupId>aopalliance</groupId>
<artifactId>aopalliance</artifactId>
<version>1.0</version>
</dependency>
<dependency>
<groupId>org.aspectj</groupId>
<artifactId>aspectjweaver</artifactId>
<version>1.8.13</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aspects</artifactId>
<version>5.0.8.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-aop</artifactId>
<version>5.0.8.RELEASE</version>
</dependency>
<!--jackson返回json-->
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.5</version>
</dependency>
</dependencies>
2.2、配置spring.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:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc.xsd">
<!--掃描controller-->
<context:component-scan base-package="com"></context:component-scan>
<!--視圖解析器-->
<bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
<!--jsp所在位置-->
<property name="prefix" value="/"></property>
<!--jsp文件后綴名-->
<property name="suffix" value=".jsp"></property>
</bean>
<mvc:annotation-driven /> <!--注解驅(qū)動-->
</beans>
2.3瞒滴、配置web.xml
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
<servlet>
<servlet-name>springMvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<!--加載spring配置文件曲梗,原來是通過 ApplicationContext applicationContext = new ClassPathXmlApplicationContext("applicationContext.xml");-->
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>springMvc</servlet-name>
<url-pattern>/</url-pattern>
</servlet-mapping>
<!--處理post亂碼-->
<filter>
<filter-name>characterEncodingFilter</filter-name>
<filter-class>
org.springframework.web.filter.CharacterEncodingFilter
</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>utf-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>characterEncodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
</web-app>
2.4、添加User實體類
package com.fan.entity;
import java.util.Arrays;
import java.util.Date;
public class User {
private String username;
private String password;
private int[] box;
private Date date;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public int[] getBox() {
return box;
}
public void setBox(int[] box) {
this.box = box;
}
public Date getDate() {
return date;
}
public void setDate(Date date) {
this.date = date;
}
@Override
public String toString() {
return "User{" +
"username='" + username + '\'' +
", password='" + password + '\'' +
", box=" + Arrays.toString(box) +
", date=" + date +
'}';
}
}
2.5妓忍、 TestJsonController測試類
@Controller
public class TestJsonController {
//http://localhost:8080/testjson1
@RequestMapping("/testjson1")
@ResponseBody //返回json字符串注解
public User test(){
User user=new User();
user.setUsername("malijuan");
user.setPassword("123");
user.setBox(new int[]{1,2,3});
user.setDate(new Date());
return user;
}
//http://localhost:8080/testjson2
@RequestMapping("/testjson2")
@ResponseBody //返回json字符串注解
public List test2(){
User user=new User();
List list=new ArrayList();
for(int i=0;i<5;i++){
user.setUsername("malijuan");
user.setPassword("123");
user.setBox(new int[]{1,2,3});
user.setDate(new Date());
list.add(user);
}
return list;
}
}
2.6虏两、啟動tomcat測試