26-SpringBoot——核心-Spring Data REST

来源:互联网 发布:pe工具 知乎 编辑:程序博客网 时间:2024/06/08 14:17

Spring Boot核心-Spring Data REST


【博文目录>>>】


【项目源码>>>】


什么是Spring Data REST


Spring Data JPA 是基于Spring Data 的repository 之上,可以将repository 自动输出为REST资源。目前Spring Data REST 支持将Spring Data JPA 、Spring Data MongoDB 、Spring Data Neo4j 、Spring Data GemFire 以及Spring Data Cassandra 的repository 自动转换成REST 服务。

SpringMVC 中配置使用Spring Data REST,Spring Data REST 的配置是定义在RepositoryRestMvcConfiguration ( org.springframework.data.rest. webmvc. Config.RepositoryRestMvcConfiguration )配置类中已经配置好了,我们可以通过继承此类或者直接在自己的配置类上@Import 此配置类。

继承方式


这里写图片描述

导入方式


这里写图片描述

Spring Boot 的支持


Spring Boot 对Spring Data REST 的自动配置放置在Rest 中,通过SpringBootRepositoryRestMvcConfiguration 类的源码我们可以得出, Spring Boot 已经为我们自动配置了RepositoryRestConfiguration ,所以在Spring Boot 中使用Spring Data REST只需引入spring-boot-starter-data-rest 的依赖,无须任何配置即可使用。Spring Boot 通过在application.properties 中配置以“ spring.data.rest ”为前缀的属性来配置Repository RestConfiguration。

这里写图片描述

【代码实现】


application.properties

spring.datasource.driverClassName=com.mysql.jdbc.Driverspring.datasource.url=jdbc\:mysql\://localhost\:3306/springbootspring.datasource.username=rootspring.datasource.password=123456#1spring.jpa.hibernate.ddl-auto=update#2spring.jpa.show-sql=truespring.data.rest.base-path= /apispring.jackson.serialization.indent_output=true

data.sql

INSERT INTO person (id, name, age, address) VALUES (1, '王俊超', 32, '合肥');INSERT INTO person (id, name, age, address) VALUES (2, 'xx', 31, '北京');INSERT INTO person (id, name, age, address) VALUES (3, 'yy', 30, '上海');INSERT INTO person (id, name, age, address) VALUES (4, 'zz', 29, '南京');INSERT INTO person (id, name, age, address) VALUES (5, 'aa', 28, '武汉');INSERT INTO person (id, name, age, address) VALUES (6, 'bb', 27, '合肥');
package org.springframework.boot.rest.domain;import javax.persistence.Entity;import javax.persistence.GeneratedValue;import javax.persistence.Id;/** * Author: 王俊超 * Date: 2017-07-18 07:38 * All Rights Reserved !!! */@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;    }    public Long getId() {        return id;    }    public void setId(Long id) {        this.id = id;    }    public String getName() {        return name;    }    public void setName(String name) {        this.name = name;    }    public Integer getAge() {        return age;    }    public void setAge(Integer age) {        this.age = age;    }    public String getAddress() {        return address;    }    public void setAddress(String address) {        this.address = address;    }}
package org.springframework.boot.rest.dao;import org.springframework.boot.rest.domain.Person;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;/** * Author: 王俊超 * Date: 2017-07-18 07:37 * All Rights Reserved !!! */@RepositoryRestResource(path = "people")public interface PersonRepository extends JpaRepository<Person, Long> {    @RestResource(path = "nameStartsWith", rel = "nameStartsWith")    Person findByNameStartsWith(@Param("name") String name);}
package org.springframework.boot.rest;import org.springframework.boot.SpringApplication;import org.springframework.boot.autoconfigure.SpringBootApplication;/** * Author: 王俊超 * Date: 2017-07-18 07:43 * All Rights Reserved !!! */@SpringBootApplicationpublic class SampleApplication {    public static void main(String[] args) {        SpringApplication.run(SampleApplication.class, args);    }}