01 json环境搭建【spring + pringMVC】
1 导包
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 2 <modelVersion>4.0.0</modelVersion> 3 <groupId>cn.xiangxu.note</groupId> 4 <artifactId>testNote</artifactId> 5 <version>0.0.1-SNAPSHOT</version> 6 <packaging>war</packaging> 7 <dependencies> 8 <dependency> 9 <groupId>org.springframework</groupId> 10 <artifactId>spring-webmvc</artifactId> 11 <version>3.2.8.RELEASE</version> 12 </dependency> 13 <dependency> 14 <groupId>com.fasterxml.jackson.core</groupId> 15 <artifactId>jackson-annotations</artifactId> 16 <version>2.2.3</version> 17 </dependency> 18 <dependency> 19 <groupId>com.fasterxml.jackson.core</groupId> 20 <artifactId>jackson-core</artifactId> 21 <version>2.2.3</version> 22 </dependency> 23 <dependency> 24 <groupId>com.fasterxml.jackson.core</groupId> 25 <artifactId>jackson-databind</artifactId> 26 <version>2.2.3</version> 27 </dependency> 28 </dependencies> 29 </project>
2 配置实体类
> 有包
> 实现序列化接口【要添加序列版本号】
> 有无参构造器
> 生成set/get方法 【serialVersionUID不用实现】
> 生成toString方法
> 有id属性的实体类需要重写equals/hashcode方法 【只选id就行啦】
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 package cn.xiangxu.testNote.entity; 2 3 import java.io.Serializable; 4 5 public class User implements Serializable { 6 7 private static final long serialVersionUID = 8786891263198147115L; 8 9 private Integer id; 10 private String name; 11 private String address; 12 13 public User(Integer id, String name, String address) { 14 super(); 15 this.id = id; 16 this.name = name; 17 this.address = address; 18 } 19 public User() { 20 super(); 21 // TODO Auto-generated constructor stub 22 } 23 public Integer getId() { 24 return id; 25 } 26 public void setId(Integer id) { 27 this.id = id; 28 } 29 public String getName() { 30 return name; 31 } 32 public void setName(String name) { 33 this.name = name; 34 } 35 public String getAddress() { 36 return address; 37 } 38 public void setAddress(String address) { 39 this.address = address; 40 } 41 @Override 42 public String toString() { 43 return "User [id=" + id + ", name=" + name + ", address=" + address + "]"; 44 } 45 @Override 46 public int hashCode() { 47 final int prime = 31; 48 int result = 1; 49 result = prime * result + ((id == null) ? 0 : id.hashCode()); 50 return result; 51 } 52 @Override 53 public boolean equals(Object obj) { 54 if (this == obj) 55 return true; 56 if (obj == null) 57 return false; 58 if (getClass() != obj.getClass()) 59 return false; 60 User other = (User) obj; 61 if (id == null) { 62 if (other.id != null) 63 return false; 64 } else if (!id.equals(other.id)) 65 return false; 66 return true; 67 } 68 69 } 70 package cn.xiangxu.testNote.entity; 71 72 import java.io.Serializable; 73 74 public class User implements Serializable { 75 76 private static final long serialVersionUID = 8786891263198147115L; 77 78 private Integer id; 79 private String name; 80 private String address; 81 82 public User(Integer id, String name, String address) { 83 super(); 84 this.id = id; 85 this.name = name; 86 this.address = address; 87 } 88 public User() { 89 super(); 90 // TODO Auto-generated constructor stub 91 } 92 public Integer getId() { 93 return id; 94 } 95 public void setId(Integer id) { 96 this.id = id; 97 } 98 public String getName() { 99 return name; 100 } 101 public void setName(String name) { 102 this.name = name; 103 } 104 public String getAddress() { 105 return address; 106 } 107 public void setAddress(String address) { 108 this.address = address; 109 } 110 @Override 111 public String toString() { 112 return "User [id=" + id + ", name=" + name + ", address=" + address + "]"; 113 } 114 @Override 115 public int hashCode() { 116 final int prime = 31; 117 int result = 1; 118 result = prime * result + ((id == null) ? 0 : id.hashCode()); 119 return result; 120 } 121 @Override 122 public boolean equals(Object obj) { 123 if (this == obj) 124 return true; 125 if (obj == null) 126 return false; 127 if (getClass() != obj.getClass()) 128 return false; 129 User other = (User) obj; 130 if (id == null) { 131 if (other.id != null) 132 return false; 133 } else if (!id.equals(other.id)) 134 return false; 135 return true; 136 } 137 138 }
3 在web.xml配置文件中配置前端控制器、spring容器
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <?xml version="1.0" encoding="UTF-8"?> 2 <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" version="2.5"> 3 <display-name>testNote</display-name> 4 <welcome-file-list> 5 <welcome-file>index.html</welcome-file> 6 <welcome-file>index.htm</welcome-file> 7 <welcome-file>index.jsp</welcome-file> 8 <welcome-file>default.html</welcome-file> 9 <welcome-file>default.htm</welcome-file> 10 <welcome-file>default.jsp</welcome-file> 11 </welcome-file-list> 12 13 <!-- 配置前端控制器 --> 14 <servlet> 15 <servlet-name>mvc</servlet-name> 16 <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class> 17 <init-param> <!-- 通过参数注入的方式配置spring容器 --> 18 <param-name>contextConfigLocation</param-name> 19 <param-value>classpath:conf/spring_*.xml</param-value> 20 </init-param> 21 </servlet> 22 <servlet-mapping> 23 <servlet-name>mvc</servlet-name> 24 <url-pattern>*.do</url-pattern> 25 </servlet-mapping> 26 27 </web-app>
4 在spring.xml文件中配置组件扫描和mvc注解扫描
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
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" 4 xmlns:context="http://www.springframework.org/schema/context" 5 xmlns:jdbc="http://www.springframework.org/schema/jdbc" 6 xmlns:jee="http://www.springframework.org/schema/jee" 7 xmlns:tx="http://www.springframework.org/schema/tx" 8 xmlns:aop="http://www.springframework.org/schema/aop" 9 xmlns:mvc="http://www.springframework.org/schema/mvc" 10 xmlns:util="http://www.springframework.org/schema/util" 11 xmlns:jpa="http://www.springframework.org/schema/data/jpa" 12 xsi:schemaLocation=" 13 http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd 14 http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd 15 http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd 16 http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd 17 http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd 18 http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd 19 http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd 20 http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd 21 http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd"> 22 23 <!-- 启用mvc注解驱动 --> 24 <mvc:annotation-driven></mvc:annotation-driven> 25 26 <!-- 组件扫描 27 注意:可以同时配置多个包,包之间用逗号隔开;可以不用写到具体哪个包 --> 28 <context:component-scan base-package="cn.xiangxu.testNote,com.xiangxu"></context:component-scan> 29 30 31 </beans>
5 编写请求分发代码
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 package cn.xiangxu.testNote.web; 2 3 import org.springframework.stereotype.Controller; 4 import org.springframework.web.bind.annotation.RequestMapping; 5 import org.springframework.web.bind.annotation.ResponseBody; 6 7 import cn.xiangxu.testNote.entity.User; 8 9 @Controller 10 public class TestController { 11 12 @RequestMapping("/json.do") 13 @ResponseBody 14 public User json() { 15 User user = new User(333, "warrior", "重庆/虎门"); 16 return user; 17 } 18 }
6 启动Tomcat进行测试
7 项目结构【eclipse中的maven工程】