赞
踩
觉得有帮助请点赞关注收藏~~
REST(Representational State Transfer 表现层状态转化)是一种软件架构风格,它是一种针对网络应用的设计和开发方法,可以降低开发的复杂性。提供系统的可伸缩性。
REST是一组架构约束条件和原则 这些约束有
1:使用客户/服务器模型 客户和服务器之间通过一个统一的接口来互相通信
2:层次化的系统 在一个REST系统中 服务端并不会固定地与一个服务器打交道
3:无状态 服务端并不会保存有关客户的任何信息,客户端负责自身状态的维持
4:可缓存 REST系统需要适当的缓存请求 减少服务端和客户端之间的信息传输
5:统一的接口 一个REST系统需要一个统一的接口来完成子系统之间以及服务与用户之间的交互
满足上述约束条件和原则的应用程序或者设计就是RESTful
在Spring Boot的Web应用中 自动支持REST 也就是说 只要spring-boot-starter-web依赖在pom.xml文件中 就支持REST
下面通过一个RESTful应用示例来讲解
假如在控制器类有如下处理方法
- @RequestMapping("/findArticalByAuthor_id/{id}")
- public List<Article>findByAuthor_id(@PathVariable("id")Integer id){
- return authorAndArticleService.findByAuthor_id(id);
- }
那么可以使用如下所示的REST风格的URL访问上述处理方法
http://localhost:8080/ch6_2/findArticleByAuthor_id/2
下面通过一个实例讲解Spring Data REST的构建过程
- <?xml version="1.0" encoding="UTF-8"?>
-
- -<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">
-
- <modelVersion>4.0.0</modelVersion>
-
-
- -<parent>
-
- <groupId>org.springframework.boot</groupId>
-
- <artifactId>spring-boot-starter-parent</artifactId>
-
- <version>2.1.7.RELEASE</version>
-
- <relativePath/>
-
- <!-- lookup parent from repository -->
-
-
- </parent>
-
- <groupId>com.ch</groupId>
-
- <artifactId>ch6_7</artifactId>
-
- <version>0.0.1-SNAPSHOT</version>
-
- <name>ch6_7</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>
-
- <!-- 添加MySQL依赖 -->
-
-
-
- -<dependency>
-
- <groupId>mysql</groupId>
-
- <artifactId>mysql-connector-java</artifactId>
-
- <version>5.1.45</version>
-
- <!-- MySQL8.x时,请使用8.x的连接器 -->
-
-
- </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>
- server.servlet.context-path=/api
- ###
- ##数据源信息配置
- ###
- #数据库地址
- spring.datasource.url=jdbc:mysql://localhost:3306/springbootjpa?characterEncoding=utf8
- #数据库用户名
- spring.datasource.username=root
- #数据库密码
- spring.datasource.password=root
- #数据库驱动
- spring.datasource.driver-class-name=com.mysql.jdbc.Driver
- ####
- #JPA持久化配置
- ####
- #指定数据库类型
- spring.jpa.database=MYSQL
- #指定是否在日志中显示SQL语句
- spring.jpa.show-sql=true
- #指定自动创建、更新数据库表等配置,update表示如果数据库中存在持久化类对应的表就不创建,不存在就创建对应的表
- spring.jpa.hibernate.ddl-auto=update
- #让控制器输出的JSON字符串格式更美观
- spring.jackson.serialization.indent-output=true
-
部分代码如下 省略部分set和get方法
- package com.ch.ch6_7.entity;
- import java.io.Serializable;
- import javax.persistence.Entity;
- import javax.persistence.GeneratedValue;
- import javax.persistence.GenerationType;
- import javax.persistence.Id;
- import javax.persistence.Table;
- @Entity
- @Table(name = "student_table")
- public class Student implements Serializable{
- private static final long serialVersionUID = 1L;
- @Id
- @GeneratedValue(strategy = GenerationType.IDENTITY)
- private int id;//主键
- private String sno;
- private String sname;
- private String ssex;
- public Student() {
- super();
- }
- public Student(int id, String sno, String sname, String ssex) {
- super();
- this.id = id;
- this.sno = sno;
- this.sname = sname;
- this.ssex = ssex;
- }
- public int getId() {
- return id;
- }
- public void setId(int id) {
- this.id = id;
- }
- public String getSno() {
- return sno;
- }
- public void setSno(String sno) {
- ex;
- }
- }
- package com.ch.ch6_7.repository;
- import java.util.List;
- import org.springframework.data.jpa.repository.JpaRepository;
- import org.springframework.data.repository.query.Param;
- import org.springframework.data.rest.core.annotation.RestResource;
- import com.ch.ch6_7.entity.Student;
- public interface StudentRepository extends JpaRepository<Student, Integer>{
- /**
- * 自定义接口查询方法,暴露为REST资源
- */
- @RestResource(path = "snameStartsWith", rel = "snameStartsWith")
- List<Student> findBySnameStartsWith(@Param("sname") String sname);
- }
在上述数据访问接口中 使用@RestResource注解将该方法暴露为REST资源
至此 基于Spring Data的REST资源服务已经构建完毕 接下来就是使用REST客户端测试服务
在Web和移动端开发时,常常会调用服务器端的RESTful的接口进行数据请求,为了调试,一般会先用工具进行测试,通过测试后才开始在开发中使用
Wisdom REST Client是用Java语言编写的REST客户端,是Github上的开源项目,可以通过http://github.com/Wisdom-Projects/rest-client地址下载
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。