Spring Data JPA构建Restful服务应用Demo
昨天介绍了什么是Rest和Restful,没有看到的同学可以参考我的这篇文章:深入理解REST与RESTfulAPI
那么说了这么多咱就实战一把,如何用SpringBoot来实现Rest,只需要Spring DataJPA和Spring Data Rest两个依赖包,快速开发一个restful应用。
创建项目
pom.xml
<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>com.alibaba</groupId> <artifactId>druid</artifactId> <version>1.1.9</version> </dependency> <dependency> <groupId>mysql</groupId> <artifactId>mysql-connector-java</artifactId> <scope>runtime</scope> </dependency> </dependencies>
创建完成后就可以在数据库properties文件中进行数据库的配置,数据库的配置和之前文章中讲的配置是一样的,大家可以参考一下我的这篇文章。包括其中的实体这两篇文章也是相同的
解读SpringBoot整合持久层技术之搭建并整合Spring Data JPA项目实战
数据库
创建BookRespository
这里和之前文章中讲到的整合JPA中的mapper类是相同的,这里可以什么内容都不写,只继承JpaRespository就可以实现对数据的增删改查操作,代码如下:
public interface BookDao extends JpaRepository<Book,Integer> {}
有人会说了,这样就可以了吗?给你看一下jpaRepository的源码,我也是第一次看哈
@NoRepositoryBean public interface JpaRepository<T, ID> extends PagingAndSortingRepository<T, ID>, QueryByExampleExecutor<T> { List<T> findAll(); List<T> findAll(Sort var1); List<T> findAllById(Iterable<ID> var1); <S extends T> List<S> saveAll(Iterable<S> var1); void flush(); <S extends T> S saveAndFlush(S var1); void deleteInBatch(Iterable<T> var1); void deleteAllInBatch(); T getOne(ID var1); <S extends T> List<S> findAll(Example<S> var1); <S extends T> List<S> findAll(Example<S> var1, Sort var2); }
经过上面几步我们就可以运行测试了,这里我们选择postMan来进行测试,这样看起来比较直观。
测试
RESTful服务创建成功之后,默认的请求路径是实体类名小写再加上后缀。
在测试的时候一定要以json的格式进行数据的交互,post默认的request请求形式是以test的形式,所以我们要改成application/json的形式,否则就会报错:
org.springframework.http.converter.HttpMessageNotReadableException异常
改完之后就可以在body处进行json数据形式的编写
返回的结果如下:
{ "name": "SpringBoot开发实战", "author": "我", "price": 20.0, "description": "good", "_links": { "self": { "href": "http://localhost:8080/books/36" }, "book": { "href": "http://localhost:8080/books/36" } } }
表示成功增加一条数据信息,然后我们测试一下查询,查询就是把提交形式改成GET。
{ "_embedded": { "books": [ { "name": "呐喊", "author": "鲁迅", "price": 23.0, "description": null, "_links": { "self": { "href": "http://localhost:8080/books/1" }, "book": { "href": "http://localhost:8080/books/1" } } }, { "name": "朝花夕拾", "author": "鲁迅1", "price": 24.0, "description": null, "_links": { "self": { "href": "http://localhost:8080/books/2" }, "book": { "href": "http://localhost:8080/books/2" } } }, { "name": "简单生活学", "author": "鲁迅2", "price": 30.0, "description": null, "_links": { "self": { "href": "http://localhost:8080/books/3" }, "book": { "href": "http://localhost:8080/books/3" } } }, { "name": "XML基础与案例教程", "author": "鲁迅3", "price": 31.0, "description": null, "_links": { "self": { "href": "http://localhost:8080/books/4" }, "book": { "href": "http://localhost:8080/books/4" } } }, { "name": "物联网之源", "author": "鲁迅4", "price": 44.0, "description": null, "_links": { "self": { "href": "http://localhost:8080/books/5" }, "book": { "href": "http://localhost:8080/books/5" } } }, { "name": "狂人日记", "author": "鲁迅", "price": 23.0, "description": null, "_links": { "self": { "href": "http://localhost:8080/books/27" }, "book": { "href": "http://localhost:8080/books/27" } } }, { "name": "其他书籍", "author": "张闲闲", "price": null, "description": null, "_links": { "self": { "href": "http://localhost:8080/books/29" }, "book": { "href": "http://localhost:8080/books/29" } } }, { "name": "java开发程序设计", "author": "张闲闲", "price": 80.0, "description": null, "_links": { "self": { "href": "http://localhost:8080/books/32" }, "book": { "href": "http://localhost:8080/books/32" } } }, { "name": "狂人日记", "author": "鲁迅", "price": 23.0, "description": null, "_links": { "self": { "href": "http://localhost:8080/books/33" }, "book": { "href": "http://localhost:8080/books/33" } } }, { "name": "SpringBoot开发实战", "author": "我", "price": 20.0, "description": null, "_links": { "self": { "href": "http://localhost:8080/books/36" }, "book": { "href": "http://localhost:8080/books/36" } } } ] }, "_links": { "self": { "href": "http://localhost:8080/books" }, "profile": { "href": "http://localhost:8080/profile/books" }, "search": { "href": "http://localhost:8080/books/search" } }, "page": { "size": 20, "totalElements": 10, "totalPages": 1, "number": 0 } }
会为我们自动返回一个paga信息,如果按照id来查询,在请求路径后面加上id就可以。如果是修改的话就把请求改成PUT形式,修改单条的话就在路径后面加上id,删除的话就是DELETE请求,搭建起来试试把,这里我就不再进行测试了。