spring mvc: json练习
本例需要用到的json包:
如下: jackson-databind jackson-core jackson-annotations <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-annotations --> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-annotations</artifactId> <version>2.7.4</version> </dependency> <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-core --> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-core</artifactId> <version>2.7.4</version> </dependency> <!-- https://mvnrepository.com/artifact/com.fasterxml.jackson.core/jackson-databind --> <dependency> <groupId>com.fasterxml.jackson.core</groupId> <artifactId>jackson-databind</artifactId> <version>2.7.4</version> </dependency>
同时需要Bean视图名的解析支持
org.springframework.web.servlet.view.BeanNameViewResolver
xml配置如下:
web.xml
<web-app> <display-name>Archetype Created Web Application</display-name> <!--配置文件路径--> <context-param> <param-name>contextConfigLocation</param-name> <param-value>/WEB-INF/applicationContext.xml</param-value> </context-param> <!-- 字符过滤器 --> <filter> <filter-name>encodingFilter</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>encodingFilter</filter-name> <url-pattern>/*</url-pattern> </filter-mapping> <!-- 监听转发 --> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener> <servlet> <servlet-name>json</servlet-name> <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>json</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> </web-app>
applicatoinContext.xml自动Bean引入
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- 默认:注解映射支持 --> <mvc:annotation-driven/> <!-- 静态资源配置 --> <mvc:resources location="/pages/**" mapping="/pages/"/> <!-- 自动扫描包名,controller --> <context:component-scan base-package="json"/> </beans>
json-servlet.xml Bean视图名的解析
<beans xmlns="http://www.springframework.org/schema/beans" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:context="http://www.springframework.org/schema/context" xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd"> <!-- bean视图解析 --> <bean class="org.springframework.web.servlet.view.BeanNameViewResolver"/> </beans>
Student.java
package json; public class Student { String name; Integer age; String gender; public String getName() { return name; } public void setName(String name) { this.name = name; } public Integer getAge() { return age; } public void setAge(Integer age) { this.age = age; } public String getGender() { return gender; } public void setGender(String gender) { this.gender = gender; } }
StudentController.java
package json; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; @Controller @RequestMapping(value="/student") public class StudentController { @RequestMapping(value="/one", method=RequestMethod.GET) @ResponseBody public Student getStudent() { Student student = new Student(); student.setAge(25); student.setGender("男"); student.setName("张三"); return student; } @RequestMapping(value="/all", method=RequestMethod.GET) @ResponseBody public Map<String, Object> getStudentAll() { Map<String, Object> maps = new HashMap<String, Object>(); List<Student> list = new ArrayList<Student>(); Student student1 = new Student(); student1.setAge(25); student1.setGender("男"); student1.setName("张三"); list.add(student1); student1.setAge(26); student1.setGender("男"); student1.setName("李四"); list.add(student1); maps.put("author", "田七"); maps.put("school", "suzhou"); maps.put("list", list); return maps; } }
访问地址:
http://localhost:8080/gugua3/student/one
{"name":"张三","age":25,"gender":"男"}
http://localhost:8080/gugua3/student/all
{"school":"suzhou","author":"田七","list":[{"name":"李四","age":26,"gender":"男"},{"name":"李四","age":26,"gender":"男"}]}