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">
<modelVersion>4.0.0</modelVersion>
<groupId>cn.xiangxu.note</groupId>
<artifactId>testNote</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<dependencies>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-webmvc</artifactId>
<version>3.2.8.RELEASE</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.2.3</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.2.3</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.2.3</version>
</dependency>
</dependencies>
</project>

jar包的pom.xml

2 配置实体类

  > 有包

  > 实现序列化接口【要添加序列版本号】

  > 有无参构造器

  > 生成set/get方法 【serialVersionUID不用实现】

  > 生成toString方法

  > 有id属性的实体类需要重写equals/hashcode方法 【只选id就行啦】

 package cn.xiangxu.testNote.entity;

 import java.io.Serializable;

 public class User implements Serializable {

     private static final long serialVersionUID = 8786891263198147115L;

     private Integer id;
private String name;
private String address; public User(Integer id, String name, String address) {
super();
this.id = id;
this.name = name;
this.address = address;
}
public User() {
super();
// TODO Auto-generated constructor stub
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "User [id=" + id + ", name=" + name + ", address=" + address + "]";
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((id == null) ? 0 : id.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
User other = (User) obj;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
return true;
} }
package cn.xiangxu.testNote.entity; import java.io.Serializable; public class User implements Serializable { private static final long serialVersionUID = 8786891263198147115L; private Integer id;
private String name;
private String address; public User(Integer id, String name, String address) {
super();
this.id = id;
this.name = name;
this.address = address;
}
public User() {
super();
// TODO Auto-generated constructor stub
}
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAddress() {
return address;
}
public void setAddress(String address) {
this.address = address;
}
@Override
public String toString() {
return "User [id=" + id + ", name=" + name + ", address=" + address + "]";
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + ((id == null) ? 0 : id.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
User other = (User) obj;
if (id == null) {
if (other.id != null)
return false;
} else if (!id.equals(other.id))
return false;
return true;
} }

实体类

3 在web.xml配置文件中配置前端控制器、spring容器

  

 <?xml version="1.0" encoding="UTF-8"?>
<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">
<display-name>testNote</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list> <!-- 配置前端控制器 -->
<servlet>
<servlet-name>mvc</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param> <!-- 通过参数注入的方式配置spring容器 -->
<param-name>contextConfigLocation</param-name>
<param-value>classpath:conf/spring_*.xml</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>mvc</servlet-name>
<url-pattern>*.do</url-pattern>
</servlet-mapping> </web-app>

web.xml

4 在spring.xml文件中配置组件扫描和mvc注解扫描

  

 <?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:context="http://www.springframework.org/schema/context"
xmlns:jdbc="http://www.springframework.org/schema/jdbc"
xmlns:jee="http://www.springframework.org/schema/jee"
xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:aop="http://www.springframework.org/schema/aop"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xmlns:util="http://www.springframework.org/schema/util"
xmlns:jpa="http://www.springframework.org/schema/data/jpa"
xsi:schemaLocation="
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.2.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.2.xsd
http://www.springframework.org/schema/jdbc http://www.springframework.org/schema/jdbc/spring-jdbc-3.2.xsd
http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.2.xsd
http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.2.xsd
http://www.springframework.org/schema/data/jpa http://www.springframework.org/schema/data/jpa/spring-jpa-1.3.xsd
http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.2.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-3.2.xsd"> <!-- 启用mvc注解驱动 -->
<mvc:annotation-driven></mvc:annotation-driven> <!-- 组件扫描
注意:可以同时配置多个包,包之间用逗号隔开;可以不用写到具体哪个包 -->
<context:component-scan base-package="cn.xiangxu.testNote,com.xiangxu"></context:component-scan> </beans>

spring_mvc.xml

5 编写请求分发代码

  

 package cn.xiangxu.testNote.web;

 import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody; import cn.xiangxu.testNote.entity.User; @Controller
public class TestController { @RequestMapping("/json.do")
@ResponseBody
public User json() {
User user = new User(333, "warrior", "重庆/虎门");
return user;
}
}

请求分发代码

6 启动Tomcat进行测试

  

7 项目结构【eclipse中的maven工程】

  

01 json环境搭建【spring + pringMVC】的更多相关文章

  1. 01 json环境搭建

    1 导包 <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.o ...

  2. SpringBoot2.0基础案例(01):环境搭建和RestFul风格接口

    一.SpringBoot 框架的特点 1.SpringBoot2.0 特点 1)SpringBoot继承了Spring优秀的基因,上手难度小 2)简化配置,提供各种默认配置来简化项目配置 3)内嵌式容 ...

  3. golang(01) linux环境搭建和编码

    1 在自己的工作目录下建立一个goproject文件夹 /home/secondtonone/goproject 2 在文件夹下建立如下三个文件 bin pkg srcbin 保存执行go insta ...

  4. [转]SAPUI5 (01) - OpenUI5环境搭建

    本文转自:http://blog.csdn.net/stone0823/article/details/53750094 版权声明:本文为博主原创文章,转载请注明出处:http://blog.csdn ...

  5. Vulkan Tutorial 01 开发环境搭建之Windows

    操作系统:Windows8.1 显卡:Nivida GTX965M 开发工具:Visual Studio 2017 相信很多人在开始学习Vulkan开发的起始阶段都会在开发环境的配置上下一些功夫,那么 ...

  6. 【Vue 学习系列 - 01】- 环境搭建(Win7)

    1. 根据系统下载Node.js 下载地址:http://nodejs.cn/download 2. 安装Node.js 点击安装Node.js,在安装目录D:\Program Files\nodej ...

  7. Flask学习笔记01之环境搭建

    使用pycharm搭建Flask运行环境 1. 打开pycharm ,创建一个新的工程 2. 选择创建Flask项目 3. Flask项目创建成功,结构如下 4. 运行项目 5. 发送请求 over!

  8. eclipse Spring环境搭建 spring tool suite

    1.期初用intellij社区版,发现收费版才能开发Java EE. 2.使用eclipse按照网上的教程,在help->eclipse marketplace中搜索sts安装spring工具套 ...

  9. 消息中间件--"rocketmq"01之环境搭建

    前置知识 ssh工具 连接linux工具SecureCRT 颜色设置,参考 中文乱码,参考 Linux相关知识 centos7 防火墙firewalld的基本使用,参考 启动: systemctl s ...

随机推荐

  1. DSD, DFF, DSF, DST概念解析

    DSD = Direct Stream Digital;  DST = D DSD是技术原理. DSDIFF简称DFF 是文件后缀名 DSF也是文件后缀名,他们是一个意思,只是一个是飞利浦的叫法 一个 ...

  2. wiretiger引擎支持行、列存储、LSM,mongodb用的哪个?

    来自 http://source.wiredtiger.com/ WiredTiger is an high performance, scalable, production quality, No ...

  3. HihoCoder 1190连通性·四

    连通性·四 时间限制:10000ms 单点时限:1000ms 内存限制:256MB 描述 小Hi和小Ho从约翰家回到学校时,网络所的老师又找到了小Hi和小Ho. 老师告诉小Hi和小Ho:之前的分组出了 ...

  4. 部署你的分布式调用链跟踪框架skywalking

    使用docker-compose 一键部署你的分布式调用链跟踪框架skywalking https://www.cnblogs.com/huangxincheng/p/9666930.html 一旦你 ...

  5. 使用window.print()后,未关闭打印页面,原网页不能操作

    使用window.print()后,未关闭打印页面,原网页不能操作,此时可以试着用window.location.reload()重新加载页面解决问题.

  6. Http请求状态码

    1xx - 信息提示   这些状态代码表示临时的响应.客户端在收到常规响应之前,应准备接收一个或多个 1xx 响应.    ·0 - 本地响应成功.   · 100 - Continue 初始的请求已 ...

  7. 浅析BMP位图文件结构(含Demo)

    浅析BMP位图文件结构(含Demo) 作者:一点一滴的Beer http://beer.cnblogs.com/   关于BMP位图格式在网上可以找到比较详细的相关文档,有兴趣的可以搜索标题为“BMP ...

  8. 基于BP神经网络的字符识别研究

    基于BP神经网络的字符识别研究 原文作者:Andrew Kirillov. http://www.codeproject.com/KB/cs/neural_network_ocr.aspx 摘要:本文 ...

  9. es6字符串的扩展学习笔记

    1. 传统上,JavaScript只有indexOf方法,可以用来确定一个字符串是否包含在另一个字符串中.ES6又提供了三种新方法. includes():返回布尔值,表示是否找到了参数字符串. st ...

  10. TFSEclipsePlugin-UpdateSiteArchive 手动拷贝