SpringBoot + RESTful 使用教程
一、SpringBoot入门
Spring boot来简化Spring应用开发,约定大于配置,从简去繁,just run就能创建一个独立的,产品级的应用。
优点:
- 快速创建独立运行的Spring项目开发以及主流框架集成
- 使用嵌入式的Servlet容器,应用无需打成WAR包
- Starters的自动依赖与版本控制
- 大量的自动配置,简化开发,也可修改默认值
- 无需配置xml,无代码生成,开箱即用
- 准生成环境的运行时应用监控
- 与云计算的天然集成
简而言之,三句话概括:
1 简化Spring应用开发的一个框架
2 整个Spring技术栈的一个大整合
3 J2EE开发的一站式解决方案
二、环境准备
Javaweb开发版本的Eclipse(内置Maven)
JDK1.8
Mysql mysql-5.7.17
Navicat(可视化的Mysql工具)
测试调试工具 postman
三、RESTful 做 CRUD-类别
1) RESTfulCRUD: CRUD满足REST风格
URI:/资源名称/标识符 HTTP请求方式区分对资源CRUD操作 返回http 响应 格式数据(通常为json)
普通CRUD(uri来区分操作) RESTfulCRUD
获取 /getStudent?id=××× /student/{id}---GET
添加 /addStudent?id=×××&name=×××&age=××× /student----POST
修改 /updateStudent?id=×××&name=×××&age=××× /student/{id} ----PUT
删除 /deleteStudent?id=××× /student/{id} ----DELETE
查询 /listStudent /students----GET
两个例子 也是两个理解RESTful非常好的网站:
知乎网站:https://www.zhihu.com/question/28557115
csdn网站:https://blog.csdn.net/qq_21383435/article/details/80032375
四、常用注解
@SpringBootApplication
,在系统启动类里面,都加入了此启动注解,此注解是个组合注解,包括了 @SpringBootConfiguration 、
@EnableAutoConfiguration 和 @ComponentScan 注解。
@SpringBootConfiguration 继承至 @Configuration ,对于熟悉 spring 的开发者而言,此标注当前类是
配置类,并会将当前类内声明的一个或多个以@Bean注解标记的方法的实例纳入到srping容器中,并且实例
名就是方法名。
@EnableAutoConfiguration 这个注解就是 springboot 能自动进行配置的魔法所在了。主要是通过此注
解,能所有符合自动配置条件的bean的定义加载到 spring 容器中,比如根据 spring-boot-starter-web
,来判断你的项目是否需要添加了 webmvc 和 tomcat ,就会自动的帮你配置web项目中所需要的默认配
置。具体的使用,会在后期自定义实现一个自动启动类时,会讲解到它的一些机制。此章节就不深入了,只
需要它是这个用途即可,一般上也单独使用不要这个注解,但比如需要排除一些无需自动配置的类时,可利
用 exclude 进行排除。
@ComponentScan 这个熟悉 spring 的开发者也应该熟悉,会扫描当前包及其子包下被@Component,
@Controller,@Service,@Repository等注解标记的类并纳入到spring容器中进行管理。
@Controller 和 @RestController
@RestController 是Spring4之后加入的注解,原来在@Controller中返回json需要@ResponseBody来配合,如果
直接用@RestController替代@Controller就不需要再配置@ResponseBody,默认返回json格式。而@Controller
是用来创建处理http请求的对象,一般结合 @RequestMapping 使用。
@RequestMapping
一个用来处理请求地址映射的注解,可用于类或方法上。用于类上,表示类中的所有响应请求的方法都是以该地址
作为父路径。(经常是用于方法中)
常用属性(摘抄至网络):
value, method; value: 指定请求的实际地址,指定的地址可以是URI Template 模式(后面将会说明);
method: 指定请求的method类型, GET、POST、PUT、DELETE等;
consumes,produces; consumes: 指定处理请求的提交内容类型(Content-Type),例如
application/json, text/html; produces: 指定返回的内容类型,仅当request请求头中的(Accept)类型中包含
该指定类型才返回;
params,headers; params: 指定request中必须包含某些参数值是,才让该方法处理。 headers: 指定
request中必须包含某些指定的header值,才能让该方法处理请求。
常用的基本上就value和method了。**其简化注解有**
@GetMapping 等同于 @RequestMapping(method = RequestMethod.GET) @PostMapping 等同于
@RequestMapping(method = RequestMethod.POST) @PutMapping 等同于 @RequestMapping(method =
RequestMethod.PUT) @DeleteMapping 等同于 @RequestMapping(method = RequestMethod.DELETE)
@RequestBody和@ResponseBody
@RequestBody注解允许request的参数在reqeust体中,常常结合前端POST请求,进行前后端交互。
@ResponseBody注解支持将的参数在reqeust体中,通常返回json格式给前端。
@PathVariable、@RequestParam
@PathVariable 汉语来说 就是路径变量 用来接收参数,如/path/001,可接收001作为参数
@RequestParam 汉语来说 请求参数 用来接收URL中的参数,如/param?id=001,可接收001作为参数
@Autowired注解
注入DAO层服务
五、创建SpringBoo项目,实现RESTful风格
实验目标 是给学生课程 创建增删改查的restful方法
1. 新建stundnts数据库和student表,并插入两条数据。
2. 新建一个maven项目,填写各种参数
3. 修改pom.xml
CREATE DATABASE students;
use students;
CREATE TABLE `student` (
`id` int(11) NOT NULL,
`name` varchar(255) DEFAULT NULL,
`age` int(11) DEFAULT NULL,
PRIMARY KEY (id)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
INSERT INTO `student` VALUES ('2', '陈二', '20');
INSERT INTO `student` VALUES ('3', '张三', '19');
<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>cus.soa</groupId>
<artifactId>testRESTful</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>testRESTful</name>
<description>testRESTful</description>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
</dependencies>
<properties>
<java.version>1.8</java.version>
</properties>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>