Spring Data REST
Spring Data REST基于Spring Data的repository之上并自动将其导出为REST资源。目前Spring Data REST支持Spring Data JPA、Spring Data MongoDB、Spring Data Neo4j、Spring Data GemFire、Spring Data Cassandra的 repository 自动转换成REST服务。简单点说,Spring Data REST把我们需要编写的大量REST模版接口做了自动化实现.
利用spring boot + spring-data-rest + spring-data-jpa实现基本的增删改查api
dependencies { compile 'com.alibaba:druid:1.1.17' compile 'org.springframework.boot:spring-boot-starter-data-jpa' compile 'org.springframework.boot:spring-boot-starter-data-rest' developmentOnly 'org.springframework.boot:spring-boot-devtools' runtimeOnly 'mysql:mysql-connector-java' }
配置spring boot
server.port = 80 spring.datasource.type=com.alibaba.druid.pool.DruidDataSource spring.datasource.driver-class-name = com.mysql.cj.jdbc.Driver spring.datasource.url = jdbc:mysql://192.168.134.128:3306/db_book?serverTimezone=CTT&useSSL=false&useUnicode=true&characterEncoding=UTF-8&allowPublicKeyRetrieval=true spring.datasource.username = root spring.datasource.password = spring.jpa.database=mysql spring.jpa.hibernate.ddl-auto=update spring.jpa.show-sql=true spring.jpa.database-platform=org.hibernate.dialect.MySQL8Dialect
创建Book实体类:
package com.wise.tiger.domain; import javax.persistence.*; import java.time.LocalDate; @Entity @Table(name = "tb_book") public class Book { /** * id */ @Id @GeneratedValue(strategy = GenerationType.IDENTITY) private Integer id; /** * 图书名称 */ @Column(length = 12,nullable = false) private String title; /** * 图书作者 */ @Column(length = 12) private String author; /** * 图书价格 */ private float price; /** * 出版社信息 */ @Column(length = 25) private String publisher; /** * 图书简介 */ @Lob private String intro; /** * 出版日期 */ private LocalDate publishDate = LocalDate.now(); // ******************setter and getter**********// }
创建BookRepository接口
package com.wise.tiger.repository; import com.wise.tiger.domain.Book; import org.springframework.data.jpa.repository.JpaRepository; import org.springframework.data.rest.core.annotation.RepositoryRestResource; @RepositoryRestResource(path = "book") public interface BookRepository extends JpaRepository<Book,Integer> { }
在Repository接口上添加@RepositoryRestResource注解,并添加了一个Path为book。这样即可实现Book实体类的RESTFul风格的所有(基于Repository接口定义的api)接口。没有service层也没有Controller。
启动项目spring boot进行测试
测试
- 发现服务:http://localhost:80/
"http://localhost/book{?page,size,sort}"为资源路径,其中page,size,sort为分页排序参数 - 以GET方式请求所有资源列表http://localhost/book发现book列表为空
- 以POST方式请求资源:http://localhost/book,提交json数据作为参数(可接收application/hal+json或application/json类型数据)添加一本图书信息
可以继续多添加几本图书后再次以GET方式请求 - PUT方式请求修改图书信息
- DELETE请求删除图书信息:http://localhost/book/2
- 分页和排序
分页参数:要在自己的查询方法中使用分页,需要更改方法签名以接受其他可分页参数,并返回一个页面而不是列表。例如,以下查询方法导出到/book/search/titleStartsWith并支持分页:
page:当前页,从0开始,默认值0
size:每页显示记录数,默认值20
可通过参数指定:http://localhost/book/?page=1&size=5
排序参数
sort
格式:http://localhost/book/?page=1&size=5&sort=title,desc
其中title为排序属性名称,desc为降序,升序为asc@RestResource(path = "titleStartsWith", rel = "titleStartsWith") Page findByTitleStartsWith(@Param("title") String title, Pageable p);
- 自定义Controller
相关推荐
Springboot + Spring MVC大大简化了Web应用的RESTful开发,而Spring DataREST更简单。Spring Data REST是建立在DataRepository之上的,它能直接把resository以HATEOAS风格暴露成Web服务,而不需要再手写Controller层...
使用Spring Boot,Spring Data JPA,MySQL构建的RESTful API 安装 本地运行 确保已安装 。 克隆或下载存储库。 $ git clone https://github.com/bizarrecode/rest-api.git $ cd rest-api 编辑数据库配置。 $ ...
khs-spring-data-rest Spring MVC Restful API 示例实现 Spring MVC REST API Primer 这是一个 Eclipse Maven Web 应用程序。 单击以获取安装和启动说明。
data JPA Spring HATEOAS Spring REST Docs Spring Security OAuth2 TDD 二、发展目标 三、开发过程 3.1. 开发环境设置 mvn package 3.2. Event域实现 创建Event类 测试 使用@Builder创建测试用例 @Test public void...
Spring REST is a practical guide for designing and developing RESTful APIs using the Spring Framework. This book walks you through the process of designing and building a REST application while taking...
通过spring-data + hibernate + bonecp + mysql RESTful API通过spring-data-rest进行JPA的实践 下载并进行Maven安装 git clone 光盘spring-data-jpa-rest.git mvn安装 准备 准备一个名为“ spring-data-jpa-with...
自述文件这个仓库是什么一个“简单”的入门项目自定义RESTful API。 该项目使用Spring Boot,与数据库的连接和与邮件服务器的连接。先决条件全局安装的 , (至少8个)和 。 该项目应使用以下数据库: / 根据您需要...
Spring Boot,MySQL,JPA,Hibernate Rest API教程使用Spring Boot,Mysql,JPA和Hibernate为简单的记笔记应用程序构建Restful CRUD API。要求Java-1.8.x Maven-3.xx MySQL的-5.xx设定步骤1.克隆应用程序git clone ...
一个示例应用程序,使用AnguarJS / Bootstrap作为前端,使用Spring MVC作为REST API生产者。 有关源代码的更多详细信息,请阅读在线GitBook: 。 注意:此项目正在维护中,以后不再添加任何新功能。 如果您正在...
RESTful API,可让您管理仓库该项目使用Java和以下工具构建: 作为服务器端框架作为构建自动化工具为ORM / JPA实施作为数据库实现作为Hibernate的顶层作为数据库迁移工具作为动态查询的替代框架应用结构模型域模型是...
Spring Data REST从那里开始使用Spring MVC来创建一个超媒体驱动的RESTful接口,该接口允许您创建,读取,更新和删除数据。 此示例应用程序演示了如何声明一个旨在存储图像数据的后端如何使上传图片然后转身显示在...
= 8 (+ Spring Data REST和REST文档) (超文本应用程序语言) (异步发布/订阅) Web客户端的 Web客户端的 用于Web客户端UI功能测试的用于Java单元和集成测试的作为构建系统为正在运行的应用程序创建容器要求已...
在基于Spring的应用程序中,Spring Security是出色的身份验证和授权解决方案,它提供了多种保护REST API的选项。 最简单的方法是利用HTTP Basic,它在引导基于Spring Boot的应用程序时默认情况下被激活。 它适合于...
Spring Data REST + AngularJS 示例 此示例使用通过编写模型和存储库(接口)轻松创建 RESTful Web 服务。 在前端,此应用程序使用 AngularJS 和来处理 Spring HATEOAS 响应。 确保您按照创建这样的应用程序。
这是用于任务管理器的简单RESTful API后端,用户可以在其中执行基本的CRUD操作,用户也可以共享其任务并通过电子邮件接收通知。 具有注册/授权和基于角色的界面(USER,ADMIN)。 使用Spring MVC测试和Spring安全...
Develop RESTful API applications for XML and JSON data transfers with non-blocking asynchronous capabilities Explore Spring's comprehensive transaction support for declarative Transaction Management ...
Spring Boot MongoDB CRUD示例-Restful CRUD API 有关更多详细信息,请访问: 多练:异常处理:安全:全栈: 在一个地方同时运行后端和前端:运行Spring Boot应用程序mvn spring-boot:run
带有Spring MVC的RESTful API 该项目是使用Spring MVC框架的Java RESTful API的示例实现。入门克隆此项目,并使用gradle包装器使用gradle运行应用程序: gradlew bootRun 或您自己的Gradle安装gradle bootRun先决...
spring-boot2-jpa-crud-example Spring Boot2 JPA Crud REST API示例以下是分步文章,解释了此Spring boot 2 JPA CRUD Restful API示例:
通过完成本教程,您将能够构建基于Spring Boot的Web应用程序,该Web应用程序向客户端公开RESTful CRUD API。 将使用以下技术:•Spring Boot:通过提供默认配置和最佳实践,极大地简化了Java企业应用程序和RESTful ...