Spring Boot构建REST服务

1.基础版

  1. 依赖

    <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>
        <version>5.1.27</version>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
    </dependency>
    
  2. 配置

    spring:
      datasource:
        username: root
        password: root
        url: jdbc:mysql://127.0.0.1:3306/jpa?useUnicode=true&useSSL=false&serverTimezone=Asia/Shanghai
        type: com.zaxxer.hikari.HikariDataSource
    
      jpa:
        database: mysql #连接数据库类型
        database-platform: org.hibernate.dialect.MySQL57Dialect #配置默认引擎
        hibernate:
          ddl-auto: update
        show-sql: true
    
  1. 实体类

    @Data
    @Entity(name = "user_jpa")
    public class User {
        @Id
        @GeneratedValue(strategy = GenerationType.IDENTITY)
        public Integer id;
        public String name;
        public Integer age;
    }
    
  2. dao

    public interface UserDao extends JpaRepository<User, Integer> {
    }
    
  3. 测试

    1. 查询(GET请求)

      批量查询

      批量查询
      批量查询

      分页查询

      分页查询
      分页查询

      分页+排序

      分页+排序
      分页+排序

      单个查询

      单个查询
      单个查询

    2. 保存(POST请求)

      保存
      保存

    3. 更新(PUT请求)

      更新
      更新

    4. 删除(DELETE请求)

      删除
      删除

到此就是一个基础版的REST服务了。

2.进阶版

1.自定义查询方法

修改dao

public interface UserDao extends JpaRepository<User, Integer> {
    List<User> findByNameContaining(@Param("name") String name);
}

重启项目,访问http://localhost:8080/users/search

此时便可以看到所有的自定义查询方法(默认路径就是方法名)。

访问自定义查询方法findByNameContaining

由于JPA的关键字命名一般较长,为了更加便捷,可以在自定义查询方法上使用@RestResource注解

@RestResource参数:

  • exported:是否暴露,默认为true

  • path:访问自定义查询方法的路径

  • rel:在生成到此资源的链接时要使用的rel值。
public interface UserDao extends JpaRepository<User, Integer> {
    @RestResource(path = "byname", rel = "findByName")
    List<User> findByNameContaining(@Param("name") String name);
}

2.自定义请求路径、结果 key 值

(1)默认情况下请求路径都是实体类名小写加 @RepositoryRestResource

@RepositoryRestResource参数:

  • path:表示将所有请求路径中的 (例:)uesrs,如 http://localhost:8080/us
  • collectionResourceRel:表示将返回的 JSON 集合中的(例:)uesrs 集合的 key
  • itemResourceRel :表示将返回的 JSON 集合中的单个(例:)uesrkey
  • exported:是否暴露,默认为true
@RepositoryRestResource(collectionResourceRel = "userlist", itemResourceRel = "u")
public interface UserDao extends JpaRepository<User, Integer> {
    @RestResource(path = "byname", rel = "findByName")
    List<User> findByNameContaining(@Param("name") String name);
}

3.配置REST

  1. 配置文件
#rest 配置
  data:
    rest:
      #添加统一前缀
      base-path: api

配置后http://localhost:8080/users-->http://localhost:8080/api/users

#rest 配置
  data:
    rest:
      #添加统一前缀
      base-path: api
      #是否在创建实体后返回记录,默认为true
      return-body-on-create: true
      #是否在更新实体后返回记录,默认为true
      return-body-on-update: true
      #每页的默认大小,默认20
      default-page-size: 20
      ......
  1. 编写配置类(优先级更高)
/**
 * rest的配置类
 */
@Configuration
public class RestConfig implements RepositoryRestConfigurer {
    @Override
    public void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) {
        config.setBasePath("api").setDefaultPageSize(20);//链式编程,可以配置多个
    }
}

4.配置 CORS(解决跨域请求问题)

具体参考Spring Boot通过CROS实现跨域)

  1. 单独配置:添加@CrossOrigin注解

  2. 全局配置:编写配置类,重写addCorsMappings方法