Skip to content

Commit 89b56da

Browse files
committed
DATAREST-1258 - Clarify difference between @RepositoryRestController and @BasePathAwareController.
1 parent dc7c6c2 commit 89b56da

File tree

1 file changed

+15
-16
lines changed

1 file changed

+15
-16
lines changed

src/main/asciidoc/overriding-sdr-response-handlers.adoc

+15-16
Original file line numberDiff line numberDiff line change
@@ -6,48 +6,47 @@ Sometimes, you may want to write a custom handler for a specific resource. To ta
66
====
77
[source,java]
88
----
9-
@RepositoryRestController
9+
@RepositoryRestController // <1>
1010
public class ScannerController {
1111
1212
private final ScannerRepository repository;
1313
14-
@Autowired
15-
public ScannerController(ScannerRepository repo) { // <1>
14+
public ScannerController(ScannerRepository repo) { // <2>
1615
repository = repo;
1716
}
1817
19-
@RequestMapping(method = GET, value = "/scanners/search/listProducers") // <2>
18+
@RequestMapping(method = GET, value = "/scanners/search/listProducers") // <3>
2019
public @ResponseBody ResponseEntity<?> getProducers() {
21-
List<String> producers = repository.listProducers(); // <3>
20+
List<String> producers = repository.listProducers(); // <4>
2221
2322
//
2423
// do some intermediate processing, logging, etc. with the producers
2524
//
2625
27-
Resources<String> resources = new Resources<String>(producers); // <4>
26+
Resources<String> resources = new Resources<String>(producers); // <5>
2827
29-
resources.add(linkTo(methodOn(ScannerController.class).getProducers()).withSelfRel()); // <5>
28+
resources.add(linkTo(methodOn(ScannerController.class).getProducers()).withSelfRel()); // <6>
3029
3130
// add other links as needed
3231
33-
return ResponseEntity.ok(resources); // <6>
32+
return ResponseEntity.ok(resources); // <7>
3433
}
3534
3635
}
3736
----
38-
39-
<1> This example uses constructor injection.
40-
<2> This handler plugs in a custom handler for a Spring Data finder method.
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.
43-
<5> Add a link back to this exact method as a `self` link.
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.
37+
<1> `@RepositoryRestController` indicates that this controller targets an existing Spring Data REST route (`/scanners/search`) tied to an exported repository.
38+
<2> This example uses constructor injection (no `@Autowired` annotation required when there is only one constructor).
39+
<3> This handler plugs in a custom handler for a Spring Data finder method.
40+
<4> This handler uses the underlying repository to fetch data, but then does specialized post processing before returning final results to the client.
41+
<5> The results need to be wrapped in a Spring HATEOAS `Resources` object to return a collection but only a `Resource` for a single item.
42+
<6> Add a link back to this exact method as a `self` link.
43+
<7> Returning the collection using Spring MVC's `ResponseEntity` wrapper ensures that the collection is wrapped and rendered in the proper accept type.
4544
====
4645

4746
`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.
4847

4948
IMPORTANT: In this example, the combined path is `RepositoryRestConfiguration.getBasePath()` + `/scanners/search/listProducers`.
5049

51-
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`.
50+
If you are not interested in entity-specific operations that target existing Spring Data REST routes, but still want to build custom operations underneath `basePath`, such as Spring MVC views and resources, use `@BasePathAwareController`.
5251

5352
WARNING: If you use `@Controller` or `@RestController` for anything, that code is totally outside the scope of Spring Data REST. This extends to request handling, message converters, exception handling, and other uses.

0 commit comments

Comments
 (0)