8.3 Spring Data REST
来源:互联网 发布:航测软件 编辑:程序博客网 时间:2024/05/16 03:03
8.3 Spring Data REST
8.3.1 点睛Spring Data REST
(1)什么是Spring Data REST
Spring Data JPA是基于Spring Data的repository之上,可以将repository自动输出为REST资源。目前Spring Data REST支持将Spring Data JPA、Spring Data MongoDb…的repository自动转换为REST服务。
(2)Spring MVC中配置使用Spring Data REST
Spring Data REST的配置是定义在RepositoryRestMvcConfiguration配置类中配置好了,可以通过继承此类或者直接在自己的配置类上@Import此配置类。
1)继承方式演示
@Configurationpublic class MyRepositoryRestMvcConfiguration extends RepositoryRestMvcConfiguration { @Override public RepositoryRestConfiguration config() { return super.config(); }}
2)导入方式演示:
@Configuration@Import(RepositoryRestMvcConfiguration.class)public class AppConfig {}
8.3.2 Spring Boot的支持
在Spring Boot中使用Spring Data REST只需引入spring-boot-starter-data-rest的依赖,无须任何配置即可使用。
在application.properties中配置以“spring.data.rest”为前缀的属性来配置RepositoryRestConfiguration.
8.3.3 实战
实体类
package com.wisely.ch8_3.domain;import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.Id;@Entitypublic class Person { @Id @GeneratedValue private Long id; private String name; private Integer age; private String address; public Person() { super(); } public Person(Long id, String name, Integer age, String address) { super(); this.id = id; this.name = name; this.age = age; this.address = address; } //get..set..}
实体类的Repository
package com.wisely.ch8_3.dao;import org.springframework.data.jpa.repository.JpaRepository;import org.springframework.data.repository.query.Param;import org.springframework.data.rest.core.annotation.RepositoryRestResource;import org.springframework.data.rest.core.annotation.RestResource;import com.wisely.ch8_3.domain.Person;@RepositoryRestResource(path = "people")public interface PersonRepository extends JpaRepository<Person, Long> { @RestResource(path = "nameStartsWith", rel = "nameStartsWith") Person findByNameStartsWith(@Param("name")String name);}
在google浏览器安装Postman REST Client插件
打开Postman:
get:
post:传json
5. REST服务测试
(3)列表
使用http://localhost:8080/persons,获得列表数据。(persons为实体类复数)
(4)获取单一对象
访问http://localhost:8080/persons/1,获得id为1的对象
(5)查询
自定义实体类Repository中定义了findByNameStartsWith方法,若想此方法也暴露为REST资源,需做如下修改:
@RestResource(path = "nameStartsWith", rel = "nameStartsWith")Person findByNameStartsWith(@Param("name")String name);
访问localhost:8080/persons/search/nameStartsWith?name=汪
(6)分页
访问localhost:8080/persons/?page=1&size=2
(7)排序
访问localhost:8080/persons/?sort=age
(8)保存
POST方法访问localhost:8080/persons/,保存的数据放置在请求体中,数据类型为JSON
{
“name”:”cc”,
“address”:”成都”,
“age”:24
}
(9)更新
Put方法访问localhost:8080/persons/6,修改的数据在请求体中,格式为json.
{
“name”:”cc”,
“address”:”成都”,
“age”:23
}
(10)删除
用DELETE方法访问localhost:8080/persons/7,删除id为7的数据
6.定制
(1)定制根路径
定制访问REST资源的路径,可以在Spring Boot的application.properties下增加如下定义即可:
spring.data.rest.base-path= /api
REST资源的路径由http://localhost:8080/persons/变成了http://localhost:8080/api/persons/
(2)定制节点路径
节点路径http://localhost:8080/persons,在实体类之后加“s”来形成路径。
可以在实体类Repository上使用@RepositoryRestResource注解的path属性进行修改。
@RepositoryRestResource(path = "people")public interface PersonRepository extends JpaRepository<Person, Long> { @RestResource(path = "nameStartsWith", rel = "nameStartsWith") Person findByNameStartsWith(@Param("name")String name);}
此时,REST服务地址变成http://localhost:8080/api/people。
- 8.3 Spring Data REST
- Spring Data REST
- Spring-data-rest 和Spring-data-jpa
- spring boot data rest 3
- SpringBoot之Spring Data REST
- spring boot 使用 spring data rest
- Spring Data REST + GemFire + AngularJS Integration
- Spring Data Rest如何暴露ID字段
- Spring Data Rest学习篇----@Excerpts
- Spring Data Rest学习篇----@Projection
- Spring Data Rest学习篇----@Repository
- Web框架Spring-rest-data解读
- Spring Data Rest Projection不起效果
- SpringBoot23-spingboot数据访问-Spring Data REST
- Spring Data Rest遇到复合主键
- Spring Boot示例 - 2. 使用Spring Boot + Spring Data REST + Spring Data JPA构建符合HATEOAS规范的REST应用
- Spring-Data-Rest 构建Spring-boot Web应用—— rest风格访问JPA数据
- Spring Data REST入门(二):环境搭建+实战演练
- 16.12.10 C组总结
- 在Jetson TX1上用YOLO训练VOC2007
- Unable to load configuration的解决方法
- 概率图论的 D-分隔
- cin、cin.get()、getline()
- 8.3 Spring Data REST
- ionic分页
- 【已解决】携带cookies的跨域访问
- 宏定义中有return导致至少浪费15分钟
- NBUT 1007 第几天 C来写,代码思路清晰
- 【Memcached】数据缓存一致性问题详解
- centos双网卡服务器一内一外,配置两张网站同时上网
- ecshop 退出登录 点着点着就退出了
- vim中浏览c++代码使用ctags和cscope进行函数跳转时vimrc的配置