Data access Spring Boot (b) Spring Data REST

What is the Spring Data REST

Spring Data REST is on the Spring Data repository based repository can be automatically output as REST resources, currently supports Spring Data JPA, Spring Data MongoDB, Spring Data Neo4j, Spring Data GemFire, Spring Data Cassandra's repository is automatically converted into a REST service. Note that automatic. Simply put, Spring Data REST our need to write a lot of templates REST interface to automate done.

Real

1: New Spring Boot item, and adding a dependency JPA Rest Repositories, and import database-driven
2: configuration properties
application.properties

//数据源
spring.datasource.driver-class-name=com.mysql.jdbc.Driver
spring.datasource.url=jdbc:mysql://localhost:3306/book
spring.datasource.username=root
spring.datasource.password=123456

//JPA
spring.jpa.hibernate.ddl-auto=update
spring.jpa.show-sql=true
spring.jackson.serialization.indent-output=true

Person entity class

@Entity
public 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;
	}

}

Entity classes Repository

public interface PersonRepository extends JpaRepository<Person, Long> {
	
	Person findByNameStartsWith(String name);

}

Installation Postman
https://www.getpostman.com/
Write pictures described here
Write pictures described here
REST service test
list:
Write pictures described here

Gets a single object (object id get 1)
Write pictures described here

Query:
above custom entity classes Repository defined findByNameStartsWith method, this method if you want to be exposed as REST resources, need to be modified as follows:

//Spring Data REST默认规则是在实体类之后加上"s"来形成路径,我们可以通过@RepositoryRestResource注解的path属性进行修改!
@RepositoryRestResource(path = "people")
public interface PersonRepository extends JpaRepository<Person, Long> {
	
	@RestResource(path = "nameStartsWith", rel = "nameStartsWith")
	Person findByNameStartsWith(@Param("name")String name);

}

Write pictures described here

Paging:
Write pictures described here

Sort by:
Write pictures described here

Save:
Write pictures described here
As can be seen by output, after successfully saved, id our new data is 45;

Update (update just added data id 45):
Write pictures described here

Delete (delete the data just added id 45):
Write pictures described here
At this point then GET access data id 45: the
Write pictures described here
show REST resource being accessed does not exist;

custom made

Custom root path:
In the example above, the path of our REST resources are accessed in the root directory, ie http: // localhost: 8080 / people , if we need to customize the root words, just under application.properties you can add the following definition:

spring.data.rest.base-path=/api

At this point the path REST resources becomes http: // localhost: 8080 / api / people;
custom node path:
the Spring the Data REST default rule is to add "s" after the entity classes to form a path, we can @RepositoryRestResource comment the path attribute modification!
The default is http: // localhost: 8080 / persons

@RepositoryRestResource(path = "people")

At this point the path REST resources becomes http: // localhost: 8080 / people;

Reference books: Spring Boot combat
more than just learning notes made, for future reference! ! !

Guess you like

Origin blog.csdn.net/z1790424577/article/details/81127709