Springboot构建RESTFUL风格接口
- 1.RESTFUL简介
- 2.快速构建RESTFUL应用
- 3.Spring Data Rest各种定制操作
- 4.快速构建RESTFUL应用(mongodb)
- 5.快速构建RESTFUL应用(ElasticSearch)
1.RESTFUL简介
1.REST全称为Representational State Transfer,是一种web软件架构风格,注意它是一种风格而不是一种标准,匹配或者兼容这种架构风格的服务称为REST服务。REST服务简介并且有层次,它通常基于HTTTP,URI,XML以及HTML这些现有的流行的标准。在REST中,资源是由URI来指定,对资源的增删改查操作可以通过HTTP协议提供的GET,POST,DELETE,UPDATE等方法来实现,使用REST可以更高效地利用缓存来提供响应速度,同时REST中的通信会话状态由客户端来维护,这可以让不同的服务器处理一系列请求中的不同请求,进而提高服务器的扩展性。在前后端分离项目中,一个设计良好的web软件架构必然要符合REST风格,在springmvc中,开发者可以通过@RestController注解来开发一个REST服务,springboot对此提供了自动化配置方案,只要添加相关依赖就能快速构建REST服务应用了。
2.快速构建RESTFUL应用
1.pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.yl</groupId>
<artifactId>restful</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>restful</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-rest</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>mysql</groupId>
<artifactId>mysql-connector-java</artifactId>
<scope>runtime</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2.application.properties
# 数据库的配置
spring.datasource.username=root
spring.datasource.password=root
spring.datasource.url=jdbc:mysql://localhost:3306/test?useUnicode=true&characterEncoding=utf-8&serverTimeZone=Asia/shanghai
# jpa的配置
spring.jpa.show-sql=true
spring.jpa.hibernate.ddl-auto=update
spring.jpa.database-platform=mysql
spring.jpa.database=mysql
spring.jpa.properties.hibernate.dialect=org.hibernate.dialect.MySQL57Dialect
3.实体类
package com.yl.restful.domain;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import java.io.Serializable;
@Entity(name = "user")
public class User implements Serializable {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Integer id;
private String username;
private String password;
public Integer getId() {
return id;
}
public void setId(Integer id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
@Override
public String toString() {
return "User{" +
"id=" + id +
", username='" + username + '\'' +
", password='" + password + '\'' +
'}';
}
}
4.dao
package com.yl.restful.dao;
import com.yl.restful.domain.User;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.data.rest.core.annotation.RepositoryRestResource;
import org.springframework.data.rest.core.annotation.RestResource;
import java.util.List;
@RepositoryRestResource(path = "human",collectionResourceRel = "us",itemResourceRel = "u")
public interface UserDao extends JpaRepository<User,Integer> {
@RestResource(path = "getByName")
List<User> findUserByUsernameIs(@Param("username") String username);
}
5.测试
1)查找id为1的user,实体类名加上s就代表接口,GET请求代表查找数据
2)分页查询数据,不传参数就查所有数据,也可以传参数,或者排序
3)添加数据,请求改为POST,在请求体传参即可,插入数据成功后会返回当前数据
3)修改数据,请求改为PUT,在请求体传参即可,更新数据成功后会返回当前数据
4)删除数据,请求改为DELETE
3.Spring Data Rest各种定制操作
1.根据用户名查找用户
2.在application.properties可以配置rest的一些参数
3.如何自定义接口的名称长度
4.快速构建RESTFUL应用(mongodb)
1.pom.xml
<?xml version="1.0" encoding="UTF-8"?>
<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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.6.3</version>
<relativePath/> <!-- lookup parent from repository -->
</parent>
<groupId>com.yl</groupId>
<artifactId>restful_mongodb</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>restful_mongodb</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>11</java.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-rest</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2.application.properties
# mongodb端口号
spring.data.mongodb.port=27017
# 主机
spring.data.mongodb.host=192.168.244.135
spring.data.mongodb.username=admin
spring.data.mongodb.password=123456
# 新建一个数据库test用于保存数据
spring.data.mongodb.database=test
# 认证用户名和密码时使用admin数据库
spring.data.mongodb.authentication-database=admin
3.实体类
package com.yl.restful_mongodb.domain;
import java.io.Serializable;
public class Student implements Serializable {
private Integer id;
private String name;
private Integer age;
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 Integer getAge() {
return age;
}
public void setAge(Integer age) {
this.age = age;
}
@Override
public String toString() {
return "Student{" +
"id=" + id +
", name='" + name + '\'' +
", age=" + age +
'}';
}
}
4.dao
package com.yl.restful_mongodb.dao;
import com.yl.restful_mongodb.domain.Student;
import org.springframework.data.mongodb.repository.MongoRepository;
public interface StudentDao extends MongoRepository<Student,Integer> {
}
5.测试
1)插入数据
2)查找数据
5.快速构建RESTFUL应用(ElasticSearch)
after.