SpringBoot 之实现Restful服务

目录

一.运行springboot_restful工程

    1.项目获取

    2.数据库准备

    3.springboot_restful工程项目结构介绍

    4.改数据库配置

    5.运行工程


二.springboot_restful工程控制层实现详解

    1.Spring对REST支持实现

    2.HTTP知识补充


三.小结

一.运行springboot_restful工程

    1.项目获取

    git clone 下载工程:[email protected]:snail-wj/springboot_repository.git

    项目地址见GitHub:https://github.com/snail-wj/springboot_repository

    2.数据库准备

        a.创建数据库springbootdb:

            create database springboot;

        b.创建表person:

        drop table if exists 'person';
	create TABLE person(
	    id int AUTO_INCREMENT PRIMARY KEY,
	    person_name VARCHAR(50) NOT NULL ,
	    person_gender VARCHAR(50) NOT NULL ,
	    description VARCHAR(50) NOT NULL
	);

    3.springboot_restful工程项目结构介绍

     org.spring.springboot
.controller - controller层
.dao - 数据操作层
.domain - 实体类
.service - 业务逻辑层
Application - 应用启动类

application.properties - 应用配置文件,应用启动会自动读取配置

    4.改数据库配置

        打开application.properties文件,修改相应的数据源配置,比如数据源地址,账号,密码等(

如果不是用mysql,自动添加连接驱动pom,修改驱动名配置)

    5.运行工程

        右键运行spring_restful工程Application应用启动类的main函数.

        用postman工具可以有如下操作。

        添加人员

POST http://localhost:8080/api/person

        SpringBoot 之实现Restful服务_第1张图片

        根据id,获取人员信息

GET http://localhost:8080/api/person/1

        SpringBoot 之实现Restful服务_第2张图片

        获取人员列表

GET http://localhost:8080/api/person

        SpringBoot 之实现Restful服务_第3张图片

        更新人员信息

PUT http://localhost:8080/api/person

        SpringBoot 之实现Restful服务_第4张图片

        删除人员

DELETE http://localhost:8080/api/person/1

        SpringBoot 之实现Restful服务_第5张图片


二.springboot_restful工程控制层实现详解

        1.Spring对REST支持实现

        PersonRestController.java实现了Restful HTTP服务

        

	@RestController
	public class PersonRestController {

		@Autowired
		private PersonService personService;

		@RequestMapping(value = "/api/person" ,method = RequestMethod.GET)
		public List findAll(){
			return personService.findAll();
		}

		@RequestMapping(value = "/api/person/{id}", method = RequestMethod.GET)
		public Person findOne(@PathVariable("id") Long id){
			return personService.findOne(id);
		}

		@RequestMapping(value = "/api/person", method = RequestMethod.POST)
		public void savePerson(@RequestBody Person person){
			personService.savePerson(person);
		}

		@RequestMapping(value = "/api/person", method = RequestMethod.PUT)
		public void updatePerson(@RequestBody Person person){
			personService.updatePerson(person);
		}

		@RequestMapping(value = "/api/person/{id}", method = RequestMethod.DELETE)
		public void deletePerson(@PathVariable("id") Long id){
			personService.deletePerson(id);
		}
	}


        代码详解

@RequestMapping处理请求地址映射
method - 指定请求的方法的类型: POST/GET?DELETE/PUT等
value - 指定实际的请求地址
consumes - 指定处理请求的提交内容类型,例如Content-Type头部设置application/json,text/html
produces - 指定返回的内容类型

@PathVariable URL映射时,用于绑定请求参数到方法参数
@RequestBody 这里注解用于读取请求体body的数据,通过HttpMessageConverter解析到对象中

    2.HTTP知识补充

        GET - 请求获取Request-URI所标识的资源
POST - 在Request-URI所标识的资源后附加新的数据
HEAD - 请求获取由Request-URI所标识的资源的响应消息报头
PUT - 请求服务器存储一个资源,并用Request-URI作为其标识
DELETE - 请求服务器删除Request-URI所标识的资源
TRACE - 请求服务器收到的请求信息,主要用于测试或诊断
CONNECT - 保留将来使用

OPTIONS - 请求查询服务器的性能,或者查询与资源相关的和需求

    图解http协议:https://www.bysocket.com/?p=282


三.小结

    感谢资料:https://www.bysocket.com/?p=1627

    再次感谢泥瓦匠BYSocket文章的支持

你可能感兴趣的:(Spring,Boot)