Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
1 of 86 4/14/2020, 11:41 AM
Spring Data REST Reference Guide https://docs.spring.io/spring-data/rest/docs/current/reference/html/#reposi...
GET
HEAD
POST
HEAD
PUT
PATCH
DELETE
GET
PUT
POST
DELETE
GET
HEAD
GET
HEAD
2 of 86 4/14/2020, 11:41 AM
Spring Data REST Reference Guide https://docs.spring.io/spring-data/rest/docs/current/reference/html/#reposi...
If-Modified-Since
ApplicationListener
@Pre @Post
3 of 86 4/14/2020, 11:41 AM
Spring Data REST Reference Guide https://docs.spring.io/spring-data/rest/docs/current/reference/html/#reposi...
rel
RepositoryRestHandlerMapping
ResourceProcessor
ObjectMapper
4 of 86 4/14/2020, 11:41 AM
Spring Data REST Reference Guide https://docs.spring.io/spring-data/rest/docs/current/reference/html/#reposi...
5 of 86 4/14/2020, 11:41 AM
Spring Data REST Reference Guide https://docs.spring.io/spring-data/rest/docs/current/reference/html/#reposi...
<dependencyManagement />
XML
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-releasetrain</artifactId>
<version>Moore-SR6</version>
<scope>import</scope>
<type>pom</type>
</dependency>
</dependencies>
</dependencyManagement>
Moore-SR6
${name}-${release}
BUILD-SNAPSHOT
M1 M2
RC1 RC2
RELEASE
SR1 SR2
<dependencies />
XML
<dependencies>
<dependency>
<groupId>org.springframework.data</groupId>
6 of 86 4/14/2020, 11:41 AM
Spring Data REST Reference Guide https://docs.spring.io/spring-data/rest/docs/current/reference/html/#reposi...
<artifactId>spring-data-jpa</artifactId>
</dependency>
<dependencies>
spring-data-releasetrain.version
7 of 86 4/14/2020, 11:41 AM
Spring Data REST Reference Guide https://docs.spring.io/spring-data/rest/docs/current/reference/html/#reposi...
GROOVY
dependencies {
...
compile("org.springframework.boot:spring-boot-starter-data-rest")
...
}
XML
<dependencies>
...
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-rest</artifactId>
</dependency>
...
</dependencies>
8 of 86 4/14/2020, 11:41 AM
Spring Data REST Reference Guide https://docs.spring.io/spring-data/rest/docs/current/reference/html/#reposi...
spring-data-rest-webmvc
GROOVY
dependencies {
… other project dependencies
compile("org.springframework.data:spring-data-rest-webmvc:3.2.6.RELEASE")
}
spring-data-rest-webmvc
XML
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-rest-webmvc</artifactId>
<version>3.2.6.RELEASE</version>
</dependency>
RepositoryRestMvcConfiguration
9 of 86 4/14/2020, 11:41 AM
Spring Data REST Reference Guide https://docs.spring.io/spring-data/rest/docs/current/reference/html/#reposi...
@SpringBootApplication @EnableAutoConfiguration
RepositoryRestConfigurer
RepositoryRestConfigurerAdapter configure…
RepositoryDetectionStrategy
RepositoryDiscoveryStrategies
DEFAULT exported
@(Repository)RestResource
ALL
10 of 86 4/14/2020, 11:41 AM
Spring Data REST Reference Guide https://docs.spring.io/spring-data/rest/docs/current/reference/html/#reposi...
ANNOTATION @(Repository)RestResource
exported false
VISIBILITY
application.properties
PROPERTIES
spring.data.rest.basePath=/api
JAVA
@Configuration
class CustomRestMvcConfiguration {
@Bean
public RepositoryRestConfigurer repositoryRestConfigurer() {
@Override
public void configureRepositoryRestConfiguration(RepositoryRestConfiguration config)
config.setBasePath("/api");
}
};
}
}
RepositoryRestConfigurer
JAVA
@Component
11 of 86 4/14/2020, 11:41 AM
Spring Data REST Reference Guide https://docs.spring.io/spring-data/rest/docs/current/reference/html/#reposi...
@Override
public void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) {
config.setBasePath("/api");
}
}
/api
basePath
defaultPageS
ize
maxPageSize
pageParamNam
e
limitParamNa
me
sortParamNam
e
defaultMedia
Type
returnBodyOn
Create
12 of 86 4/14/2020, 11:41 AM
Spring Data REST Reference Guide https://docs.spring.io/spring-data/rest/docs/current/reference/html/#reposi...
returnBodyOn
Update
13 of 86 4/14/2020, 11:41 AM
Spring Data REST Reference Guide https://docs.spring.io/spring-data/rest/docs/current/reference/html/#reposi...
/orders
/orders/{id}
CrudRepository
CrudRepository
@RestResource(exported
= false)
14 of 86 4/14/2020, 11:41 AM
Spring Data REST Reference Guide https://docs.spring.io/spring-data/rest/docs/current/reference/html/#reposi...
200 OK GET
RepositoryRestConfiguration.returnBodyOnUpdate
PUT 200 OK 201 Created
PUT
RepositoryRestConfiguration.returnBodyOnUpdate
RepositoryRestConfiguration.returnBodyCreate) null
GET
curl -v http://localhost:8080/
15 of 86 4/14/2020, 11:41 AM
Spring Data REST Reference Guide https://docs.spring.io/spring-data/rest/docs/current/reference/html/#reposi...
{ "_links" : {
"orders" : {
"href" : "http://localhost:8080/orders"
},
"profile" : {
"href" : "http://localhost:8080/api/alps"
}
}
}
profile
@RepositoryRestResource
GET
findAll(…)
findAll(Pageable)
16 of 86 4/14/2020, 11:41 AM
Spring Data REST Reference Guide https://docs.spring.io/spring-data/rest/docs/current/reference/html/#reposi...
findAll(Sort)
findAll()
page
size
sort ($propertyname,)+[asc|desc]
GET
GET
application/hal+json
application/json
GET
search
HEAD
HEAD
17 of 86 4/14/2020, 11:41 AM
Spring Data REST Reference Guide https://docs.spring.io/spring-data/rest/docs/current/reference/html/#reposi...
findAll(Pageable)
findAll(Sort)
findAll()
POST
POST
save(…)
POST
POST
18 of 86 4/14/2020, 11:41 AM
Spring Data REST Reference Guide https://docs.spring.io/spring-data/rest/docs/current/reference/html/#reposi...
GET
findById(…)
GET
GET
@RestResource
19 of 86 4/14/2020, 11:41 AM
Spring Data REST Reference Guide https://docs.spring.io/spring-data/rest/docs/current/reference/html/#reposi...
HEAD
HEAD
findById(…)
PUT
PUT
save(…)
PUT
PUT
20 of 86 4/14/2020, 11:41 AM
Spring Data REST Reference Guide https://docs.spring.io/spring-data/rest/docs/current/reference/html/#reposi...
PATCH
PATCH PUT
save(…)
PATCH
PATCH
DELETE
DELETE
21 of 86 4/14/2020, 11:41 AM
Spring Data REST Reference Guide https://docs.spring.io/spring-data/rest/docs/current/reference/html/#reposi...
delete(T)
delete(ID)
delete(Iterable)
DELETE
@RestResource
GET
GET
GET
22 of 86 4/14/2020, 11:41 AM
Spring Data REST Reference Guide https://docs.spring.io/spring-data/rest/docs/current/reference/html/#reposi...
PUT
PUT
PUT
PUT
POST
POST
POST
DELETE
DELETE
POST
23 of 86 4/14/2020, 11:41 AM
Spring Data REST Reference Guide https://docs.spring.io/spring-data/rest/docs/current/reference/html/#reposi...
@RestResource
GET
GET
GET
GET
HEAD
HEAD
24 of 86 4/14/2020, 11:41 AM
Spring Data REST Reference Guide https://docs.spring.io/spring-data/rest/docs/current/reference/html/#reposi...
GET
GET
GET
page
size
sort ($propertyname,)+[asc|desc]
GET
application/hal+json
application/json
HEAD
HEAD
25 of 86 4/14/2020, 11:41 AM
Spring Data REST Reference Guide https://docs.spring.io/spring-data/rest/docs/current/reference/html/#reposi...
PagingAndSortingRepository<T, ID>
size
http://localhost:8080/people/?size=5
JAVA
@RestResource(path = "nameStartsWith", rel = "nameStartsWith")
public Page findByNameStartsWith(@Param("name") String name, Pageable p);
Page
prev next
prev next
26 of 86 4/14/2020, 11:41 AM
Spring Data REST Reference Guide https://docs.spring.io/spring-data/rest/docs/current/reference/html/#reposi...
curl localhost:8080/people?size=5
JAVASCRIPT
{
"_links" : {
"self" : {
"href" : "http://localhost:8080/persons{&sort,page,size}", 1
"templated" : true
},
"next" : {
"href" : "http://localhost:8080/persons?page=1&size=5{&sort}", 2
"templated" : true
}
},
"_embedded" : {
... data ...
},
"page" : { 3
"size" : 5,
"totalElements" : 50,
"totalPages" : 10,
"number" : 0
}
}
_links
1 self
2 next
curl &
27 of 86 4/14/2020, 11:41 AM
Spring Data REST Reference Guide https://docs.spring.io/spring-data/rest/docs/current/reference/html/#reposi...
next
$ curl "http://localhost:8080/persons?page=1&size=5"
JAVASCRIPT
{
"_links" : {
"self" : {
"href" : "http://localhost:8080/persons{&sort,projection,page,size}",
"templated" : true
},
"next" : {
"href" : "http://localhost:8080/persons?page=2&size=5{&sort,projection}", 1
"templated" : true
},
"prev" : {
"href" : "http://localhost:8080/persons?page=0&size=5{&sort,projection}", 2
"templated" : true
}
},
"_embedded" : {
... data ...
},
"page" : {
"size" : 5,
"totalElements" : 50,
"totalPages" : 10,
"number" : 1 3
}
}
next
1
self
2 prev
28 of 86 4/14/2020, 11:41 AM
Spring Data REST Reference Guide https://docs.spring.io/spring-data/rest/docs/current/reference/html/#reposi...
next
sort
, asc desc
findByNameStartsWith PersonRepository
Person “ ”
name
curl -v "http://localhost:8080/people/search/nameStartsWith?name=K&sort=name,desc"
sort=PROPERTY
Pageable
Accept
29 of 86 4/14/2020, 11:41 AM
Spring Data REST Reference Guide https://docs.spring.io/spring-data/rest/docs/current/reference/html/#reposi...
ObjectMapper
JAVA
@Entity
public class MyEntity {
@OneToMany
private List<MyInterface> interfaces;
}
Module
30 of 86 4/14/2020, 11:41 AM
Spring Data REST Reference Guide https://docs.spring.io/spring-data/rest/docs/current/reference/html/#reposi...
ObjectMapper
configureJacksonObjectMapper ObjectMapper
PersistentEntity
JAVA
@Override
protected void configureJacksonObjectMapper(ObjectMapper objectMapper) {
objectMapper.registerModule(new SimpleModule("MyCustomModule") {
@Override
public void setupModule(SetupContext context) {
context.addAbstractTypeResolver(
new SimpleAbstractTypeResolver().addMapping(MyInterface.class,
MyInterfaceImpl.class)
);
}
});
}
SetupContext Module
Module
ObjectMapper
setupModule
JAVA
@Override
public void setupModule(SetupContext context) {
SimpleSerializers serializers = new SimpleSerializers();
SimpleDeserializers deserializers = new SimpleDeserializers();
context.addSerializers(serializers);
context.addDeserializers(deserializers);
}
31 of 86 4/14/2020, 11:41 AM
Spring Data REST Reference Guide https://docs.spring.io/spring-data/rest/docs/current/reference/html/#reposi...
JAVA
@Entity
public class Person {
@Id @GeneratedValue
private Long id;
private String firstName, lastName;
@OneToOne
private Address address;
…
}
Person
id
firstName lastName
address
JAVA
interface PersonRepository extends CrudRepository<Person, Long> {}
32 of 86 4/14/2020, 11:41 AM
Spring Data REST Reference Guide https://docs.spring.io/spring-data/rest/docs/current/reference/html/#reposi...
firstName lastName
address Address
JAVA
interface AddressRepository extends CrudRepository<Address, Long> {}
Person address
Address “ ”
JAVASCRIPT
{
"firstName" : "Frodo",
"lastName" : "Baggins",
"_links" : {
"self" : {
"href" : "http://localhost:8080/persons/1"
},
"address" : {
"href" : "http://localhost:8080/persons/1/address"
}
}
}
Address
Person
JAVASCRIPT
{
"firstName" : "Frodo",
"lastName" : "Baggins",
"address" : {
"street": "Bag End",
"state": "The Shire",
"country": "Middle Earth"
},
"_links" : {
"self" : {
"href" : "http://localhost:8080/persons/1"
}
}
33 of 86 4/14/2020, 11:41 AM
Spring Data REST Reference Guide https://docs.spring.io/spring-data/rest/docs/current/reference/html/#reposi...
address
id
JAVA
@Projection(name = "noAddresses", types = { Person.class }) 1
interface NoAddresses { 2
String getFirstName(); 3
String getLastName(); 4
}
@Projection name
1 types
Person
3 firstName
4 lastName
JAVASCRIPT
{
"firstName" : "Frodo",
"lastName" : "Baggins",
"_links" : {
"self" : {
"href" : "http://localhost:8080/persons/1{?projection}", 1
"templated" : true 2
},
"address" : {
"href" : "http://localhost:8080/persons/1/address"
34 of 86 4/14/2020, 11:41 AM
Spring Data REST Reference Guide https://docs.spring.io/spring-data/rest/docs/current/reference/html/#reposi...
}
}
}
1 {?projection}
2 self
http://localhost:8080/persons
/1?projection=noAddresses
projection
@Projection(name = "noAddress")
@Projection
RepositoryRestConfiguration.getProjectionConfiguration().addProjection(…)
@Projection
profile
Person
/alps/persons Person
35 of 86 4/14/2020, 11:41 AM
Spring Data REST Reference Guide https://docs.spring.io/spring-data/rest/docs/current/reference/html/#reposi...
GET
JAVASCRIPT
{ …
"id" : "get-person", 1
"name" : "person",
"type" : "SAFE",
"rt" : "#person-representation",
"descriptors" : [ {
"name" : "projection", 2
"doc" : {
"value" : "The projection that shall be applied when rendering the response. Acceptable values
"format" : "TEXT"
},
"type" : "SEMANTIC",
"descriptors" : [ {
"name" : "noAddresses", 3
"type" : "SEMANTIC",
"descriptors" : [ {
"name" : "firstName", 4
"type" : "SEMANTIC"
}, {
"name" : "lastName", 4
"type" : "SEMANTIC"
} ]
} ]
} ]
},
…
1 GET Person
2 projection
3 noAddresses
4 firstName lastName
36 of 86 4/14/2020, 11:41 AM
Spring Data REST Reference Guide https://docs.spring.io/spring-data/rest/docs/current/reference/html/#reposi...
@Projection
RepositoryRestConfiguration.getProjectionConfiguration().addProjection(…)
@JsonIgnore
JAVA
@Entity
public class User {
@Id @GeneratedValue
private Long id;
private String name;
@JsonIgnore password
1
User
UserRepository password
@JsonIgnore password
@JsonIgnore
37 of 86 4/14/2020, 11:41 AM
Spring Data REST Reference Guide https://docs.spring.io/spring-data/rest/docs/current/reference/html/#reposi...
JAVA
@Projection(name = "passwords", types = { User.class })
interface PasswordProjection {
String getPassword();
}
@JsonIgnore
User.password
JAVA
@Entity
public class Person {
...
private String firstName;
private String lastName;
...
}
38 of 86 4/14/2020, 11:41 AM
Spring Data REST Reference Guide https://docs.spring.io/spring-data/rest/docs/current/reference/html/#reposi...
JAVA
@Projection(name = "virtual", types = { Person.class })
public interface VirtualProjection {
@Value("#{target.firstName} #{target.lastName}") 1
String getFullName();
@Value
1 firstName lastName
fullName
PersonRepository
JAVA
@RepositoryRestResource(excerptProjection = NoAddresses.class)
interface PersonRepository extends CrudRepository<Person, Long> {}
NoAddresses
Person
39 of 86 4/14/2020, 11:41 AM
Spring Data REST Reference Guide https://docs.spring.io/spring-data/rest/docs/current/reference/html/#reposi...
Person Address
address
GET
JAVA
@Projection(name = "inlineAddress", types = { Person.class }) 1
interface InlineAddress {
String getFirstName();
String getLastName();
Address getAddress(); 2
}
1 inlineAddress
getAddress Address
2
PersonRepository
JAVA
@RepositoryRestResource(excerptProjection = InlineAddress.class)
interface PersonRepository extends CrudRepository<Person, Long> {}
JAVASCRIPT
{
"firstName" : "Frodo",
"lastName" : "Baggins",
"address" : { 1
"street": "Bag End",
"state": "The Shire",
40 of 86 4/14/2020, 11:41 AM
Spring Data REST Reference Guide https://docs.spring.io/spring-data/rest/docs/current/reference/html/#reposi...
1 address
Address
2
@RepositoryRestResource(excerptProjection=…)
ETag
41 of 86 4/14/2020, 11:41 AM
Spring Data REST Reference Guide https://docs.spring.io/spring-data/rest/docs/current/reference/html/#reposi...
JAVA
public class Sample {
Sample(Long version) {
this.version = version;
}
}
@Version
1 org.springframework.data.annotation.Version
ETag
ETag If-Match
ETag
ETag
If-Match
412 Precondition Failed
“ ”
ETag
42 of 86 4/14/2020, 11:41 AM
Spring Data REST Reference Guide https://docs.spring.io/spring-data/rest/docs/current/reference/html/#reposi...
If-None-Match If-None-
Match
GET If-None-
Match GET
304 Not
Modified “
”
ETag @Entity
@Document
@Version ETag
If-Modified-Since
If-Modified-Since
JAVA
@Document
public class Receipt {
43 of 86 4/14/2020, 11:41 AM
Spring Data REST Reference Guide https://docs.spring.io/spring-data/rest/docs/current/reference/html/#reposi...
@LastModifiedDate
1 DateTime Date
Calendar long Long
Last-Modified
Last-Modified
304 Not Modified
data-*
data-etag
44 of 86 4/14/2020, 11:41 AM
Spring Data REST Reference Guide https://docs.spring.io/spring-data/rest/docs/current/reference/html/#reposi...
Last-Modified data-last-modified
self data-uri
data-self
ETag Last-Modified
Validator
Validator
Person
Validator<Person> ApplicationContext
beforeCreatePersonValidator beforeCreate
45 of 86 4/14/2020, 11:41 AM
Spring Data REST Reference Guide https://docs.spring.io/spring-data/rest/docs/current/reference/html/#reposi...
RepositoryRestConfigurer
RepositoryRestConfigurerAdapter configureValidatingRepositoryEventListener
addValidator ValidatingRepositoryEventListener
JAVA
@Override
protected void configureValidatingRepositoryEventListener(ValidatingRepositoryEventListener v)
v.addValidator("beforeSave", new BeforeSaveValidator());
}
BeforeCreateEvent
AfterCreateEvent
BeforeSaveEvent
AfterSaveEvent
BeforeLinkSaveEvent
AfterLinkSaveEvent
BeforeDeleteEvent
AfterDeleteEvent
ApplicationListener
46 of 86 4/14/2020, 11:41 AM
Spring Data REST Reference Guide https://docs.spring.io/spring-data/rest/docs/current/reference/html/#reposi...
JAVA
public class BeforeSaveEventListener extends AbstractRepositoryEventListener {
@Override
public void onBeforeSave(Object entity) {
... logic to handle inspecting the entity before the Repository saves it
}
@Override
public void onAfterDelete(Object entity) {
... send a message that this entity has been deleted
}
}
@RepositoryEventHandler
BeanPostProcessor
BeanPostProcessor
BeforeSaveEvent
JAVA
@RepositoryEventHandler 1
public class PersonEventHandler {
@HandleBeforeSave
public void handlePersonSave(Person p) {
// … you can now deal with Person in a type-safe way
}
@HandleBeforeSave
public void handleProfileSave(Profile p) {
// … you can now deal with Profile in a type-safe way
}
}
47 of 86 4/14/2020, 11:41 AM
Spring Data REST Reference Guide https://docs.spring.io/spring-data/rest/docs/current/reference/html/#reposi...
1
@RepositoryEventHandler(Person.class)
@Component
@SpringBootApplication @ComponentScan
ApplicationContext
BeanPostProcessor RepositoryRestMvcConfiguration
Person
JAVA
@Configuration
public class RepositoryConfiguration {
@Bean
PersonEventHandler personEventHandler() {
return new PersonEventHandler();
}
}
48 of 86 4/14/2020, 11:41 AM
Spring Data REST Reference Guide https://docs.spring.io/spring-data/rest/docs/current/reference/html/#reposi...
RepositoryEntityLinks
JAVA
public class MyWebApp {
@Autowired
public MyWebApp(RepositoryEntityLinks entityLinks) {
this.entityLinks = entityLinks;
}
}
entityLinks.linkToCollectionResource(Person
.class) Person
entityLinks.linkToSingleResource(Person.cla
ss, 1)
entityLinks.linkToPagedResource(Person.clas
s, new PageRequest(…))
49 of 86 4/14/2020, 11:41 AM
Spring Data REST Reference Guide https://docs.spring.io/spring-data/rest/docs/current/reference/html/#reposi...
entityLinks.linksToSearchResources(Person.c
lass)
entityLinks.linkToSearchResource(Person.cla rel
ss, "findByLastName")
50 of 86 4/14/2020, 11:41 AM
Spring Data REST Reference Guide https://docs.spring.io/spring-data/rest/docs/current/reference/html/#reposi...
persons addresses
JAVASCRIPT
{
"_links" : {
"persons" : {
"href" : "http://localhost:8080/persons"
},
"addresses" : {
"href" : "http://localhost:8080/addresses"
},
"profile" : {
"href" : "http://localhost:8080/profile"
}
}
}
localhost:8080/profile
JAVASCRIPT
{
"_links" : {
"self" : {
"href" : "http://localhost:8080/profile"
},
"persons" : {
"href" : "http://localhost:8080/profile/persons"
},
"addresses" : {
"href" : "http://localhost:8080/profile/addresses"
}
}
}
profile
/profile
51 of 86 4/14/2020, 11:41 AM
Spring Data REST Reference Guide https://docs.spring.io/spring-data/rest/docs/current/reference/html/#reposi...
/profile/persons Person
JAVASCRIPT
{
"version" : "1.0",
"descriptors" : [ {
"id" : "person-representation", 1
"descriptors" : [ {
"name" : "firstName",
"type" : "SEMANTIC"
}, {
"name" : "lastName",
"type" : "SEMANTIC"
}, {
"name" : "id",
"type" : "SEMANTIC"
}, {
"name" : "address",
"type" : "SAFE",
"rt" : "http://localhost:8080/profile/addresses#address"
} ]
}, {
"id" : "create-persons", 2
"name" : "persons", 3
"type" : "UNSAFE", 4
"rt" : "#person-representation" 5
}, {
"id" : "get-persons",
"name" : "persons",
"type" : "SAFE",
"rt" : "#person-representation"
}, {
"id" : "delete-person",
"name" : "person",
"type" : "IDEMPOTENT",
"rt" : "#person-representation"
}, {
"id" : "patch-person",
"name" : "person",
"type" : "UNSAFE",
"rt" : "#person-representation"
}, {
"id" : "update-person",
"name" : "person",
"type" : "IDEMPOTENT",
"rt" : "#person-representation"
}, {
"id" : "get-person",
"name" : "person",
"type" : "SAFE",
"rt" : "#person-representation"
} ]
52 of 86 4/14/2020, 11:41 AM
Spring Data REST Reference Guide https://docs.spring.io/spring-data/rest/docs/current/reference/html/#reposi...
Person #person-
1
representation
2 Person
name persons
3
person
4 type UNSAFE
application/alps+json
application/hal+json
profile _links
JAVASCRIPT
{
"_links" : {
"self" : {
"href" : "http://localhost:8080/persons" 1
},
... other links ...
"profile" : {
"href" : "http://localhost:8080/profile/persons" 2
}
},
...
}
1 Person
profile Accept
application/alps+json
53 of 86 4/14/2020, 11:41 AM
Spring Data REST Reference Guide https://docs.spring.io/spring-data/rest/docs/current/reference/html/#reposi...
HTML.SPAN HTML.INPUT
GET HEAD
PUT DELETE
POST
inlineAddress noAddresses
“ ”
get-persons
JAVASCRIPT
...
{
"id" : "get-persons",
"name" : "persons",
54 of 86 4/14/2020, 11:41 AM
Spring Data REST Reference Guide https://docs.spring.io/spring-data/rest/docs/current/reference/html/#reposi...
"type" : "SAFE",
"rt" : "#person-representation",
"descriptors" : [ { 1
"name" : "projection",
"doc" : {
"value" : "The projection that shall be applied when rendering the response. Acceptable valu
"format" : "TEXT"
},
"type" : "SEMANTIC",
"descriptors" : [ {
"name" : "inlineAddress", 2
"type" : "SEMANTIC",
"descriptors" : [ {
"name" : "address",
"type" : "SEMANTIC"
}, {
"name" : "firstName",
"type" : "SEMANTIC"
}, {
"name" : "lastName",
"type" : "SEMANTIC"
} ]
}, {
"name" : "noAddresses", 3
"type" : "SEMANTIC",
"descriptors" : [ {
"name" : "firstName",
"type" : "SEMANTIC"
}, {
"name" : "lastName",
"type" : "SEMANTIC"
} ]
} ]
} ]
}
...
descriptors
1
projection
55 of 86 4/14/2020, 11:41 AM
Spring Data REST Reference Guide https://docs.spring.io/spring-data/rest/docs/current/reference/html/#reposi...
rest-
messages.properties
PROPERTIES
rest.description.person=A collection of people
rest.description.person.id=primary key used internally to store a person (not for RESTful usage)
rest.description.person.firstName=Person's first name
rest.description.person.lastName=Person's last name
rest.description.person.address=Person's address
rest.description.* Person
person-representation
JAVASCRIPT
...
{
"id" : "person-representation",
"doc" : {
"value" : "A collection of people", 1
"format" : "TEXT"
},
"descriptors" : [ {
"name" : "firstName",
"doc" : {
"value" : "Person's first name", 2
"format" : "TEXT"
},
"type" : "SEMANTIC"
}, {
"name" : "lastName",
"doc" : {
"value" : "Person's last name", 3
"format" : "TEXT"
},
"type" : "SEMANTIC"
}, {
"name" : "id",
"doc" : {
"value" : "primary key used internally to store a person (not for RESTful usage)"
"format" : "TEXT"
},
"type" : "SEMANTIC"
}, {
"name" : "address",
"doc" : {
56 of 86 4/14/2020, 11:41 AM
Spring Data REST Reference Guide https://docs.spring.io/spring-data/rest/docs/current/reference/html/#reposi...
1 rest.description.person
2 rest.description.person.firstName firstName
3 rest.description.person.lastName lastName
rest.description.person.id id
4
5 rest.description.person.address address
doc
profile
57 of 86 4/14/2020, 11:41 AM
Spring Data REST Reference Guide https://docs.spring.io/spring-data/rest/docs/current/reference/html/#reposi...
JAVASCRIPT
{
"_links" : {
"self" : {
"href" : "http://localhost:8080/profile"
},
"persons" : {
"href" : "http://localhost:8080/profile/persons"
},
"addresses" : {
"href" : "http://localhost:8080/profile/addresses"
}
}
}
Accept application/schema+json
JAVASCRIPT
{
"title" : "org.springframework.data.rest.webmvc.jpa.Person", 1
"properties" : { 2
"firstName" : {
"readOnly" : false,
"type" : "string"
},
"lastName" : {
"readOnly" : false,
"type" : "string"
},
"siblings" : {
"readOnly" : false,
"type" : "string",
"format" : "uri"
},
"created" : {
"readOnly" : false,
"type" : "string",
"format" : "date-time"
},
"father" : {
"readOnly" : false,
"type" : "string",
"format" : "uri"
},
58 of 86 4/14/2020, 11:41 AM
Spring Data REST Reference Guide https://docs.spring.io/spring-data/rest/docs/current/reference/html/#reposi...
"weight" : {
"readOnly" : false,
"type" : "integer"
},
"height" : {
"readOnly" : false,
"type" : "integer"
}
},
"descriptors" : { },
"type" : "object",
"$schema" : "https://json-schema.org/draft-04/schema#"
}
profile _links
JAVASCRIPT
{
"_links" : {
"self" : {
"href" : "http://localhost:8080/persons" 1
},
... other links ...
"profile" : {
"href" : "http://localhost:8080/profile/persons" 2
}
},
...
}
1 Person
profile Accept
application/schema+json
59 of 86 4/14/2020, 11:41 AM
Spring Data REST Reference Guide https://docs.spring.io/spring-data/rest/docs/current/reference/html/#reposi...
@Pre @Post
JAVA
@PreAuthorize("hasRole('ROLE_USER')") 1
public interface PreAuthorizedOrderRepository extends CrudRepository<Order, UUID> {
@PreAuthorize("hasRole('ROLE_ADMIN')") 2
@Override
void deleteById(UUID aLong);
@PreAuthorize("hasRole('ROLE_ADMIN')")
@Override
void delete(Order order);
@PreAuthorize("hasRole('ROLE_ADMIN')")
@Override
void deleteAll(Iterable<? extends Order> orders);
@PreAuthorize("hasRole('ROLE_ADMIN')")
@Override
void deleteAll();
}
1 ROLE_USER
2
ROLE_ADMIN
60 of 86 4/14/2020, 11:41 AM
Spring Data REST Reference Guide https://docs.spring.io/spring-data/rest/docs/current/reference/html/#reposi...
CrudRepository
CrudRepository
@Secured
JAVA
@Secured("ROLE_USER") 1
@RepositoryRestResource(collectionResourceRel = "people", path = "people")
public interface SecuredPersonRepository extends CrudRepository<Person, UUID> {
@Secured("ROLE_ADMIN") 2
@Override
void deleteById(UUID aLong);
@Secured("ROLE_ADMIN")
@Override
void delete(Person person);
@Secured("ROLE_ADMIN")
@Override
void deleteAll(Iterable<? extends Person> persons);
@Secured("ROLE_ADMIN")
@Override
void deleteAll();
}
61 of 86 4/14/2020, 11:41 AM
Spring Data REST Reference Guide https://docs.spring.io/spring-data/rest/docs/current/reference/html/#reposi...
2 ROLE_ADMIN
@PreAuthorize
@Secured
JAVA
@Configuration 1
@EnableWebSecurity
@EnableGlobalMethodSecurity(securedEnabled = true, prePostEnabled = true) 2
public class SecurityConfiguration extends WebSecurityConfigurerAdapter { 3
...
}
@EnableGlobalMethodSecurity
2 @Secured @Pre @Post
WebSecurityConfigurerAdapter
3
62 of 86 4/14/2020, 11:41 AM
Spring Data REST Reference Guide https://docs.spring.io/spring-data/rest/docs/current/reference/html/#reposi...
XML
<dependencies>
<dependency>
<groupId>org.springframework.data</groupId>
<artifactId>spring-data-rest-hal-browser</artifactId>
</dependency>
</dependencies>
GROOVY
dependencies {
compile 'org.springframework.data:spring-data-rest-hal-browser'
}
63 of 86 4/14/2020, 11:41 AM
Spring Data REST Reference Guide https://docs.spring.io/spring-data/rest/docs/current/reference/html/#reposi...
64 of 86 4/14/2020, 11:41 AM
Spring Data REST Reference Guide https://docs.spring.io/spring-data/rest/docs/current/reference/html/#reposi...
65 of 86 4/14/2020, 11:41 AM
Spring Data REST Reference Guide https://docs.spring.io/spring-data/rest/docs/current/reference/html/#reposi...
findOne(…)
RepositoryRestConfiguration
66 of 86 4/14/2020, 11:41 AM
Spring Data REST Reference Guide https://docs.spring.io/spring-data/rest/docs/current/reference/html/#reposi...
EntityLookup
User username
Optional<User> findByUsername(String username)
JAVA
@Component
public class SpringDataRestCustomization extends RepositoryRestConfigurer {
@Override
public void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) {
config.withEntityLookup()
.forRepository(UserRepository.class)
.withIdMapping(User::getUsername)
.withLookup(UserRepository::findByUsername);
}
}
forRepository(…)
UserEntityLookup
JAVA
@Component
public class UserEntityLookup extends EntityLookupSupport<User> {
@Override
67 of 86 4/14/2020, 11:41 AM
Spring Data REST Reference Guide https://docs.spring.io/spring-data/rest/docs/current/reference/html/#reposi...
@Override
public Object lookupEntity(Serializable id) {
return repository.findByUsername(id.toString());
}
}
getResourceIdentifier(…)
lookupEntity(…) UserRepository
RepositoryDetectionStrategy
RepositoryDetectionStrategies RepositoryRestConfiguration
ALL
DEFAULT
@RepositoryRestResource exported false
VISIBILITY
ANNOTATED
@RepositoryRestResource exported false
RepositoryDetectionStrategy
68 of 86 4/14/2020, 11:41 AM
Spring Data REST Reference Guide https://docs.spring.io/spring-data/rest/docs/current/reference/html/#reposi...
CrudRepository
delete(…) DELETE
@RestResource(exported = false)
RepositoryRestConfiguration.getExposureConfiguration()
JAVA
ExposureConfiguration config = repositoryRestConfiguration.getExposureConfiguration();
config.forDomainType(User.class).disablePutForCreation(); 1
config.withItemExposure((metadata, httpMethods) -> httpMethods.disable(HttpMethod.PATCH)); 2
1 PUT
2 PATCH
CrudRepository
69 of 86 4/14/2020, 11:41 AM
Spring Data REST Reference Guide https://docs.spring.io/spring-data/rest/docs/current/reference/html/#reposi...
JAVA
interface PersonRepository extends CrudRepository<Person, Long> {}
http://localhost:8080
/persons/
@RestResource
JAVA
@RepositoryRestResource(path = "people")
interface PersonRepository extends CrudRepository<Person, Long> {}
http://localhost:8080
/people/
JAVA
interface PersonRepository extends CrudRepository<Person, Long> {
http://localhost:8080/persons/search
/findByName
search
@RestResource
70 of 86 4/14/2020, 11:41 AM
Spring Data REST Reference Guide https://docs.spring.io/spring-data/rest/docs/current/reference/html/#reposi...
JAVA
@RepositoryRestResource(path = "people")
interface PersonRepository extends CrudRepository<Person, Long> {
@RestResource(path = "names")
List<Person> findByName(String name);
}
http://localhost:8080/people
/search/names
rel
rel
http://localhost:8080
/persons/search
JAVASCRIPT
{
"_links" : {
"findByName" : {
"href" : "http://localhost:8080/persons/search/findByName"
}
}
}
JAVA
@RepositoryRestResource(path = "people")
interface PersonRepository extends CrudRepository<Person, Long> {
71 of 86 4/14/2020, 11:41 AM
Spring Data REST Reference Guide https://docs.spring.io/spring-data/rest/docs/current/reference/html/#reposi...
JAVASCRIPT
{
"_links" : {
"names" : {
"href" : "http://localhost:8080/persons/search/names"
}
}
}
rel
rel
JAVA
@RepositoryRestResource(path = "people", rel = "people")
interface PersonRepository extends CrudRepository<Person, Long> {
rel
JAVASCRIPT
{
"_links" : {
"people" : {
"href" : "http://localhost:8080/people"
},
…
}
}
72 of 86 4/14/2020, 11:41 AM
Spring Data REST Reference Guide https://docs.spring.io/spring-data/rest/docs/current/reference/html/#reposi...
search
@RestResource
JAVASCRIPT
{
"_links" : {
"names" : {
"href" : "http://localhost:8080/people/search/names"
}
}
}
path = "people"
/people
/people/search
password User
JAVA
@RepositoryRestResource(exported = false)
73 of 86 4/14/2020, 11:41 AM
Spring Data REST Reference Guide https://docs.spring.io/spring-data/rest/docs/current/reference/html/#reposi...
@RestResource(exported = false)
JAVA
@RepositoryRestResource(path = "people", rel = "people")
interface PersonRepository extends CrudRepository<Person, Long> {
@RestResource(exported = false)
List<Person> findByName(String name);
}
@RestResource(exported =
false)
JAVA
@Entity
public class Person {
@OneToMany
@RestResource(exported = false)
private Map<String, Profile> profiles;
}
CrudRepository
@RestResource(exported = false)
74 of 86 4/14/2020, 11:41 AM
Spring Data REST Reference Guide https://docs.spring.io/spring-data/rest/docs/current/reference/html/#reposi...
CrudRepository
JAVA
@RepositoryRestResource(path = "people", rel = "people")
interface PersonRepository extends CrudRepository<Person, Long> {
@Override
@RestResource(exported = false)
void delete(Long id);
@Override
@RestResource(exported = false)
void delete(Person entity);
}
delete
delete
delete
exported = false
spring-boot-starter-data-rest
RepositoryRestController
org.springframework.data.rest.webmvc.RepositoryRestMvcConfiguration
75 of 86 4/14/2020, 11:41 AM
Spring Data REST Reference Guide https://docs.spring.io/spring-data/rest/docs/current/reference/html/#reposi...
@Import
JAVA
import org.springframework.context.annotation.Import;
import org.springframework.data.rest.webmvc.RepositoryRestMvcConfiguration;
@Configuration
@Import(RepositoryRestMvcConfiguration.class)
public class MyApplicationConfiguration {
…
}
XML
<bean class="org.springframework.data.rest.webmvc.config.RepositoryRestMvcConfiguration"/>
ApplicationContext
RepositoryRestHandlerMapping
RepositoryRestHandlerMapping
HandlerMapping
76 of 86 4/14/2020, 11:41 AM
Spring Data REST Reference Guide https://docs.spring.io/spring-data/rest/docs/current/reference/html/#reposi...
RepositoryRestController
HandlerMapping
HandlerMapping
null “ HandlerMapping
”
HandlerMapping order=(Ordered.LOWEST_PRECEDENCE -
100)
person
/person
people
/people /person
@RepositoryRestController @Controller
@RestController @RepositoryRestController
RepositoryRestConfiguration.setBasePath
/api
@RepositoryRestController
JAVA
@RepositoryRestController
public class ScannerController {
@Autowired
public ScannerController(ScannerRepository repo) { 1
repository = repo;
}
77 of 86 4/14/2020, 11:41 AM
Spring Data REST Reference Guide https://docs.spring.io/spring-data/rest/docs/current/reference/html/#reposi...
//
// do some intermediate processing, logging, etc. with the producers
//
resources.add(linkTo(methodOn(ScannerController.class).getProducers()).withSelfRel());
return ResponseEntity.ok(resources); 6
}
Resources
4
Resource
5 self
ResponseEntity
6
Resources Resource
Resources<Resource<String>>
String
RepositoryRestConfiguration.getBasePath()
/scanners/search/listProducers
78 of 86 4/14/2020, 11:41 AM
Spring Data REST Reference Guide https://docs.spring.io/spring-data/rest/docs/current/reference/html/#reposi...
basePath
@BasePathAwareController @RepositoryRestResource
OpenEntityManagerInViewInterceptor
@Controller @RestController
Customer
ResourceProcessor
ResourceProcessor<>
ResourceProcessor<Resource<T>>
T
Person @Bean
ApplicationContext
JAVA
@Bean
public ResourceProcessor<Resource<Person>> personProcessor() {
79 of 86 4/14/2020, 11:41 AM
Spring Data REST Reference Guide https://docs.spring.io/spring-data/rest/docs/current/reference/html/#reposi...
@Override
public Resource<Person> process(Resource<Person> resource) {
http://localhost:8080/people
linkTo(…)
resource.add(Link)
Resource
ResourceProcessor
Converter<Entity, Resource>
ConversionService
_links
@Entity
Repository
80 of 86 4/14/2020, 11:41 AM
Spring Data REST Reference Guide https://docs.spring.io/spring-data/rest/docs/current/reference/html/#reposi...
ObjectMapper
ObjectMapper
ObjectMapper
JAVA
@Entity
public class MyEntity {
@OneToMany
private List<MyInterface> interfaces;
}
Module ApplicationContext
ObjectMapper
Module setupModule
TypeResolver
81 of 86 4/14/2020, 11:41 AM
Spring Data REST Reference Guide https://docs.spring.io/spring-data/rest/docs/current/reference/html/#reposi...
JAVA
public class MyCustomModule extends SimpleModule {
private MyCustomModule() {
super("MyCustomModule", new Version(1, 0, 0, "SNAPSHOT"));
}
@Override
public void setupModule(SetupContext context) {
context.addAbstractTypeResolver(
new SimpleAbstractTypeResolver().addMapping(MyInterface.class,
MyInterfaceImpl.class));
}
}
SetupContext Module
ObjectMapper
setupModule
JAVA
public class MyCustomModule extends SimpleModule {
@Override
public void setupModule(SetupContext context) {
context.addSerializers(serializers);
context.addDeserializers(deserializers);
}
82 of 86 4/14/2020, 11:41 AM
Spring Data REST Reference Guide https://docs.spring.io/spring-data/rest/docs/current/reference/html/#reposi...
@CrossOrigin
@CrossOrigin
JAVA
@CrossOrigin
interface PersonRepository extends CrudRepository<Person, Long> {}
PersonRepository
@CrossOrigin
JAVA
@CrossOrigin(origins = "http://domain2.example",
methods = { RequestMethod.GET, RequestMethod.POST, RequestMethod.DELETE },
maxAge = 3600)
interface PersonRepository extends CrudRepository<Person, Long> {}
PersonRepository
83 of 86 4/14/2020, 11:41 AM
Spring Data REST Reference Guide https://docs.spring.io/spring-data/rest/docs/current/reference/html/#reposi...
JAVA
@RepositoryRestController
public class PersonController {
@CrossOrigin(maxAge = 3600)
@RequestMapping(path = "/people/xml/{id}", method = RequestMethod.GET, produces = MediaType.APPLIC
public Person retrieve(@PathVariable Long id) {
// …
}
}
@RepositoryRestController @CrossOrigin
@CrossOrigin
GET HEAD POST
84 of 86 4/14/2020, 11:41 AM
Spring Data REST Reference Guide https://docs.spring.io/spring-data/rest/docs/current/reference/html/#reposi...
JAVA
@Component
public class SpringDataRestCustomization extends RepositoryRestConfigurerAdapter {
@Override
public void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) {
config.getCorsRegistry().addMapping("/person/**")
.allowedOrigins("http://domain2.example")
.allowedMethods("PUT", "DELETE")
.allowedHeaders("header1", "header2", "header3")
.exposedHeaders("header1", "header2")
.allowCredentials(false).maxAge(3600);
}
}
85 of 86 4/14/2020, 11:41 AM
Spring Data REST Reference Guide https://docs.spring.io/spring-data/rest/docs/current/reference/html/#reposi...
spring-data-examples/rest
86 of 86 4/14/2020, 11:41 AM