当前位置:   article > 正文

Spring Boot实现RESTful接口架构实战(包括REST的讲解、定义、REST服务测试)_spring-boot-starter-data-rest

spring-boot-starter-data-rest

觉得有帮助请点赞关注收藏~~


REST简介

REST(Representational State Transfer 表现层状态转化)是一种软件架构风格,它是一种针对网络应用的设计和开发方法,可以降低开发的复杂性。提供系统的可伸缩性。

REST是一组架构约束条件和原则 这些约束有

1:使用客户/服务器模型   客户和服务器之间通过一个统一的接口来互相通信

2:层次化的系统   在一个REST系统中 服务端并不会固定地与一个服务器打交道

3:无状态   服务端并不会保存有关客户的任何信息,客户端负责自身状态的维持

4:可缓存   REST系统需要适当的缓存请求 减少服务端和客户端之间的信息传输

5:统一的接口   一个REST系统需要一个统一的接口来完成子系统之间以及服务与用户之间的交互

满足上述约束条件和原则的应用程序或者设计就是RESTful


一、Spring Boot整合REST

在Spring Boot的Web应用中 自动支持REST 也就是说 只要spring-boot-starter-web依赖在pom.xml文件中 就支持REST

下面通过一个RESTful应用示例来讲解

假如在控制器类有如下处理方法

  1. @RequestMapping("/findArticalByAuthor_id/{id}")
  2. public List<Article>findByAuthor_id(@PathVariable("id")Integer id){
  3. return authorAndArticleService.findByAuthor_id(id);
  4. }

那么可以使用如下所示的REST风格的URL访问上述处理方法

http://localhost:8080/ch6_2/findArticleByAuthor_id/2

二、Spring Data REST

在Spring Boot应用中使用Spring Data REST只需引入spring-boot-starter-data-rest的依赖即可

下面通过一个实例讲解Spring Data REST的构建过程

1:修改pom.xml文件 添加MYSQL依赖

  1. <?xml version="1.0" encoding="UTF-8"?>
  2. -<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://maven.apache.org/POM/4.0.0">
  3. <modelVersion>4.0.0</modelVersion>
  4. -<parent>
  5. <groupId>org.springframework.boot</groupId>
  6. <artifactId>spring-boot-starter-parent</artifactId>
  7. <version>2.1.7.RELEASE</version>
  8. <relativePath/>
  9. <!-- lookup parent from repository -->
  10. </parent>
  11. <groupId>com.ch</groupId>
  12. <artifactId>ch6_7</artifactId>
  13. <version>0.0.1-SNAPSHOT</version>
  14. <name>ch6_7</name>
  15. <description>Demo project for Spring Boot</description>
  16. -<properties>
  17. <java.version>11</java.version>
  18. </properties>
  19. -<dependencies>
  20. -<dependency>
  21. <groupId>org.springframework.boot</groupId>
  22. <artifactId>spring-boot-starter-data-jpa</artifactId>
  23. </dependency>
  24. -<dependency>
  25. <groupId>org.springframework.boot</groupId>
  26. <artifactId>spring-boot-starter-data-rest</artifactId>
  27. </dependency>
  28. <!-- 添加MySQL依赖 -->
  29. -<dependency>
  30. <groupId>mysql</groupId>
  31. <artifactId>mysql-connector-java</artifactId>
  32. <version>5.1.45</version>
  33. <!-- MySQL8.x时,请使用8.x的连接器 -->
  34. </dependency>
  35. -<dependency>
  36. <groupId>org.springframework.boot</groupId>
  37. <artifactId>spring-boot-starter-test</artifactId>
  38. <scope>test</scope>
  39. </dependency>
  40. </dependencies>
  41. -<build>
  42. -<plugins>
  43. -<plugin>
  44. <groupId>org.springframework.boot</groupId>
  45. <artifactId>spring-boot-maven-plugin</artifactId>
  46. </plugin>
  47. </plugins>
  48. </build>
  49. </project>

2:设置上下文路径以及数据源配置信息

  1. server.servlet.context-path=/api
  2. ###
  3. ##数据源信息配置
  4. ###
  5. #数据库地址
  6. spring.datasource.url=jdbc:mysql://localhost:3306/springbootjpa?characterEncoding=utf8
  7. #数据库用户名
  8. spring.datasource.username=root
  9. #数据库密码
  10. spring.datasource.password=root
  11. #数据库驱动
  12. spring.datasource.driver-class-name=com.mysql.jdbc.Driver
  13. ####
  14. #JPA持久化配置
  15. ####
  16. #指定数据库类型
  17. spring.jpa.database=MYSQL
  18. #指定是否在日志中显示SQL语句
  19. spring.jpa.show-sql=true
  20. #指定自动创建、更新数据库表等配置,update表示如果数据库中存在持久化类对应的表就不创建,不存在就创建对应的表
  21. spring.jpa.hibernate.ddl-auto=update
  22. #让控制器输出的JSON字符串格式更美观
  23. spring.jackson.serialization.indent-output=true

3:创建持久化实体类Student 

部分代码如下  省略部分set和get方法

  1. package com.ch.ch6_7.entity;
  2. import java.io.Serializable;
  3. import javax.persistence.Entity;
  4. import javax.persistence.GeneratedValue;
  5. import javax.persistence.GenerationType;
  6. import javax.persistence.Id;
  7. import javax.persistence.Table;
  8. @Entity
  9. @Table(name = "student_table")
  10. public class Student implements Serializable{
  11. private static final long serialVersionUID = 1L;
  12. @Id
  13. @GeneratedValue(strategy = GenerationType.IDENTITY)
  14. private int id;//主键
  15. private String sno;
  16. private String sname;
  17. private String ssex;
  18. public Student() {
  19. super();
  20. }
  21. public Student(int id, String sno, String sname, String ssex) {
  22. super();
  23. this.id = id;
  24. this.sno = sno;
  25. this.sname = sname;
  26. this.ssex = ssex;
  27. }
  28. public int getId() {
  29. return id;
  30. }
  31. public void setId(int id) {
  32. this.id = id;
  33. }
  34. public String getSno() {
  35. return sno;
  36. }
  37. public void setSno(String sno) {
  38. ex;
  39. }
  40. }

 4:创建数据访问层

  1. package com.ch.ch6_7.repository;
  2. import java.util.List;
  3. import org.springframework.data.jpa.repository.JpaRepository;
  4. import org.springframework.data.repository.query.Param;
  5. import org.springframework.data.rest.core.annotation.RestResource;
  6. import com.ch.ch6_7.entity.Student;
  7. public interface StudentRepository extends JpaRepository<Student, Integer>{
  8. /**
  9. * 自定义接口查询方法,暴露为REST资源
  10. */
  11. @RestResource(path = "snameStartsWith", rel = "snameStartsWith")
  12. List<Student> findBySnameStartsWith(@Param("sname") String sname);
  13. }

在上述数据访问接口中 使用@RestResource注解将该方法暴露为REST资源

至此 基于Spring Data的REST资源服务已经构建完毕 接下来就是使用REST客户端测试服务

三、REST服务测试 

在Web和移动端开发时,常常会调用服务器端的RESTful的接口进行数据请求,为了调试,一般会先用工具进行测试,通过测试后才开始在开发中使用

Wisdom REST Client是用Java语言编写的REST客户端,是Github上的开源项目,可以通过http://github.com/Wisdom-Projects/rest-client地址下载 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/寸_铁/article/detail/799713
推荐阅读
相关标签
  

闽ICP备14008679号