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。

0 0
原创粉丝点击