You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/main/asciidoc/customizing-json-output.adoc
+10-10Lines changed: 10 additions & 10 deletions
Original file line number
Diff line number
Diff line change
@@ -3,25 +3,25 @@
3
3
4
4
Sometimes in your application, you need to provide links to other resources from a particular entity. For example, a `Customer` response might be enriched with links to a current shopping cart or links to manage resources related to that entity. Spring Data REST provides integration with https://github.com/SpringSource/spring-hateoas[Spring HATEOAS] and provides an extension hook that lets you alter the representation of resources that go out to the client.
5
5
6
-
== The `ResourceProcessor` Interface
6
+
== The `RepresentationModelProcessor` Interface
7
7
8
-
Spring HATEOAS defines a `ResourceProcessor<>` interface for processing entities. All beans of type `ResourceProcessor<Resource<T>>` are automatically picked up by the Spring Data REST exporter and triggered when serializing an entity of type `T`.
8
+
Spring HATEOAS defines a `RepresentationModelProcessor<>` interface for processing entities. All beans of type `RepresentationModelProcessor<EntityModel<T>>` are automatically picked up by the Spring Data REST exporter and triggered when serializing an entity of type `T`.
9
9
10
10
For example, to define a processor for a `Person` entity, add a `@Bean` similar to the following (which is taken from the Spring Data REST tests) to your `ApplicationContext`:
11
11
12
12
====
13
13
[source,java]
14
14
----
15
15
@Bean
16
-
public ResourceProcessor<Resource<Person>> personProcessor() {
16
+
public RepresentationModelProcessor<EntityModel<Person>> personProcessor() {
17
17
18
-
return new ResourceProcessor<Resource<Person>>() {
18
+
return new RepresentationModelProcessor<EntityModel<Person>>() {
19
19
20
20
@Override
21
-
public Resource<Person> process(Resource<Person> resource) {
21
+
public EntityModel<Person> process(EntityModel<Person> model) {
@@ -32,10 +32,10 @@ IMPORTANT: The preceding example hard codes a link to `http://localhost:8080/peo
32
32
33
33
== Adding Links
34
34
35
-
You can add links to the default representation of an entity by calling `resource.add(Link)`, as the preceding example shows. Any links you add to the `Resource` are added to the final output.
35
+
You can add links to the default representation of an entity by calling `model.add(Link)`, as the preceding example shows. Any links you add to the `EntityModel` are added to the final output.
36
36
37
37
== Customizing the Representation
38
38
39
-
The Spring Data REST exporter executes any discovered `ResourceProcessor` instances before it creates the output representation. It does so by registering a `Converter<Entity, Resource>` instance with an internal `ConversionService`. This is the component responsible for creating the links to referenced entities (such as those objects under the `_links` property in the object's JSON representation). It takes an `@Entity` and iterates over its properties, creating links for those properties that are managed by a `Repository` and copying across any embedded or simple properties.
39
+
The Spring Data REST exporter executes any discovered `RepresentationModelProcessor` instances before it creates the output representation. It does so by registering a `Converter<Entity, EntityModel>` instance with an internal `ConversionService`. This is the component responsible for creating the links to referenced entities (such as those objects under the `_links` property in the object's JSON representation). It takes an `@Entity` and iterates over its properties, creating links for those properties that are managed by a `Repository` and copying across any embedded or simple properties.
40
40
41
-
If your project needs to have output in a different format, however, you can completely replace the default outgoing JSON representation with your own. If you register your own `ConversionService` in the `ApplicationContext` and register your own `Converter<Entity, Resource>`, you can return a `Resource` implementation of your choosing.
41
+
If your project needs to have output in a different format, however, you can completely replace the default outgoing JSON representation with your own. If you register your own `ConversionService` in the `ApplicationContext` and register your own `Converter<Entity, EntityModel>`, you can return a `EntityModel` implementation of your choosing.
@@ -39,20 +39,20 @@ public class ScannerController {
39
39
<1> This example uses constructor injection.
40
40
<2> This handler plugs in a custom handler for a Spring Data finder method.
41
41
<3> This handler uses the underlying repository to fetch data, but then does some form of post processing before returning the final data set to the client.
42
-
<4> The results need to be wrapped up in a Spring HATEOAS `Resources` object to return a collection but only a `Resource` for a single item.
42
+
<4> The results of type T need to be wrapped up in a Spring HATEOAS `CollectionModel<T>` object to return a collection. `EntityModel<T>` or `RepresentationModel<T>` are suitable wrappers for a single item, respectively.
43
43
<5> Add a link back to this exact method as a `self` link.
44
44
<6> Returning the collection by using Spring MVC's `ResponseEntity` wrapper ensures that the collection is properly wrapped and rendered in the proper accept type.
45
45
====
46
46
47
-
`Resources` is for a collection, while `Resource` is for a single item. These types can be combined. If you know the links for each item in a collection, use `Resources<Resource<String>>` (or whatever the core domain type is rather than `String`). Doing so lets you assemble links for each item as well as for the whole collection.
47
+
`CollectionModel` is for a collection, while `EntityModel` -- or the more general class `RepresentationModel` -- is for a single item. These types can be combined. If you know the links for each item in a collection, use `CollectionModel<EntityModel<String>>` (or whatever the core domain type is rather than `String`). Doing so lets you assemble links for each item as well as for the whole collection.
48
48
49
49
IMPORTANT: In this example, the combined path is `RepositoryRestConfiguration.getBasePath()` + `/scanners/search/listProducers`.
== @RepositoryRestResource VS. @BasePathAwareController
53
53
54
54
If you are not interested in entity-specific operations but still want to build custom operations underneath `basePath`, such as Spring MVC views, resources, and others, use `@BasePathAwareController`.
55
-
If you're using `@RepositoryRestResource` on your custom controller, it will only handle the request if your request mappings blend into the URI space used by the repository.
55
+
If you're using `@RepositoryRestController` on your custom controller, it will only handle the request if your request mappings blend into the URI space used by the repository.
56
56
It will also apply the following extra functionality to the controller methods:
57
57
58
58
. CORS configuration according as defined for the repository mapped to the base path segment used in the request mapping of the handler method.
0 commit comments