Skip to content

Commit 3cc1af4

Browse files
Jay Bryantgregturn
Jay Bryant
authored andcommitted
DATAREST-1246 - Full edit of the reference docs.
I edited for grammar, punctuation, spelling, usage, and corporate voice. I also added the epub-cover image (and the SVG file from which it can be generated).
1 parent 8346e84 commit 3cc1af4

26 files changed

+718
-500
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,13 @@
11
[[customizing-sdr.adding-sdr-to-spring-mvc-app]]
2-
= Adding Spring Data REST to an existing Spring MVC Application
2+
= Adding Spring Data REST to an Existing Spring MVC Application
33

4-
NOTE: The following steps are unnecessary if you are using Spring Boot. Adding *spring-boot-starter-data-rest* will cause it to automatically to get added to your application.
4+
NOTE: The following steps are unnecessary if you use Spring Boot. For Boot applications, adding `spring-boot-starter-data-rest` automatically adds Spring Data REST to your application.
55

6-
If you have an existing Spring MVC application and you'd like to integrate Spring Data REST, it's actually very easy.
7-
8-
Somewhere in your Spring MVC configuration (most likely where you configure your MVC resources) add a bean reference to the JavaConfig class that is responsible for configuring the `RepositoryRestController`. The class name is `org.springframework.data.rest.webmvc.RepositoryRestMvcConfiguration`.
6+
You can integrate Spring Data REST with an existing Spring MVC application. In your Spring MVC configuration (most likely where you configure your MVC resources), add a bean reference to the Java configuration class that is responsible for configuring the `RepositoryRestController`. The class name is `org.springframework.data.rest.webmvc.RepositoryRestMvcConfiguration`. The following example shows how to use an `@Import` annotation to add the proper reference:
97

108
In Java, this would look like:
119

10+
====
1211
[source,java]
1312
----
1413
import org.springframework.context.annotation.Import;
@@ -21,24 +20,27 @@ public class MyApplicationConfiguration {
2120
2221
}
2322
----
23+
====
2424

25-
In XML this would look like:
25+
The following example shows the corresponding XML configuration:
2626

27+
====
2728
[source,xml]
2829
----
2930
<bean class="org.springframework.data.rest.webmvc.config.RepositoryRestMvcConfiguration"/>
3031
----
32+
====
3133

32-
When your ApplicationContext comes across this bean definition it will bootstrap the necessary Spring MVC resources to fully-configure the controller for exporting the repositories it finds in that `ApplicationContext` and any parent contexts.
34+
When your ApplicationContext comes across this bean definition, it bootstraps the necessary Spring MVC resources to fully configure the controller for exporting the repositories it finds in that `ApplicationContext` and any parent contexts.
3335

34-
== More on required configuration
36+
== More on Required Configuration
3537

36-
There are a couple Spring MVC resources that Spring Data REST depends on that must be configured correctly for it to work inside an existing Spring MVC application. We've tried to isolate those resources from whatever similar resources already exist within your application, but it may be that you want to customize some of the behavior of Spring Data REST by modifying these MVC components.
38+
Spring Data REST depends on a couple Spring MVC resources that must be configured correctly for it to work inside an existing Spring MVC application. We tried to isolate those resources from whatever similar resources already exist within your application, but it may be that you want to customize some of the behavior of Spring Data REST by modifying these MVC components.
3739

38-
The most important things that we configure especially for use by Spring Data REST include:
40+
You should pay special attention to configuring `RepositoryRestHandlerMapping`, covered in the next section.
3941

40-
=== RepositoryRestHandlerMapping
42+
=== `RepositoryRestHandlerMapping`
4143

42-
We register a custom `HandlerMapping` instance that responds only to the `RepositoryRestController` and only if a path is meant to be handled by Spring Data REST. In order to keep paths that are meant to be handled by your application separate from those handled by Spring Data REST, this custom `HandlerMapping` inspects the URL path and checks to see if a Repository has been exported under that name. If it has, it allows the request to be handled by Spring Data REST. If there is no Repository exported under that name, it returns `null`, which just means "let other `HandlerMapping` instances try to service this request".
44+
We register a custom `HandlerMapping` instance that responds only to the `RepositoryRestController` and only if a path is meant to be handled by Spring Data REST. In order to keep paths that are meant to be handled by your application separate from those handled by Spring Data REST, this custom `HandlerMapping` class inspects the URL path and checks to see if a repository has been exported under that name. If it has, the custom `HandlerMapping` class lets the request be handled by Spring Data REST. If there is no Repository exported under that name, it returns `null`, which means "`let other `HandlerMapping` instances try to service this request`".
4345

44-
The Spring Data REST `HandlerMapping` is configured with `order=(Ordered.LOWEST_PRECEDENCE - 100)` which means it will usually be first in line when it comes time to map a URL path. Your existing application will never get a chance to service a request that is meant for a repository. For example, if you have a repository exported under the name "person", then all requests to your application that start with `/person` will be handled by Spring Data REST and your application will never see that request. If your repository is exported under a different name, however (like "people"), then requests to `/people` will go to Spring Data REST and requests to "/person" will be handled by your application.
46+
The Spring Data REST `HandlerMapping` is configured with `order=(Ordered.LOWEST_PRECEDENCE - 100)`, which means it is usually first in line when it comes time to map a URL path. Your existing application never gets a chance to service a request that is meant for a repository. For example, if you have a repository exported under the name of `person`, then all requests to your application that start with `/person` are handled by Spring Data REST, and your application never sees that request. If your repository is exported under a different name (such as `people`), however, then requests to `/people` go to Spring Data REST and requests to `/person` are handled by your application.

src/main/asciidoc/configuring-cors.adoc

+20-11
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,42 @@
11
[[customizing-sdr.configuring-cors]]
22
= Configuring CORS
33

4-
For security reasons, browsers prohibit AJAX calls to resources residing outside the current origin. When working with client-side HTTP requests issued by a browser you want to enable specific HTTP resources to be accessible.
4+
For security reasons, browsers prohibit AJAX calls to resources residing outside the current origin. When working with client-side HTTP requests issued by a browser, you want to enable specific HTTP resources to be accessible.
55

6-
Spring Data REST supports as of 2.6 http://en.wikipedia.org/wiki/Cross-origin_resource_sharing[Cross-origin resource sharing] (CORS) through http://docs.spring.io/spring/docs/{springVersion}/spring-framework-reference/web.html#mvc-cors[Spring's CORS] support.
6+
Spring Data REST, as of 2.6, supports http://en.wikipedia.org/wiki/Cross-origin_resource_sharing[Cross-Origin Resource Sharing] (CORS) through http://docs.spring.io/spring/docs/{springVersion}/spring-framework-reference/web.html#mvc-cors[Spring's CORS] support.
77

88

9-
== Repository interface CORS configuration
9+
== Repository Interface CORS Configuration
1010

11-
You can add a `@CrossOrigin` annotation to your repository interfaces to enable CORS for the whole repository. By default `@CrossOrigin` allows all origins and HTTP methods:
11+
You can add a `@CrossOrigin` annotation to your repository interfaces to enable CORS for the whole repository. By default, `@CrossOrigin` allows all origins and HTTP methods. The following example shows a cross-origin repository interface definition:
1212

13+
====
1314
[source, java]
1415
----
1516
@CrossOrigin
1617
interface PersonRepository extends CrudRepository<Person, Long> {}
1718
----
19+
====
1820

19-
In the above example CORS support is enabled for the whole `PersonRepository`. `@CrossOrigin` provides attributes to configure CORS support.
21+
In the preceding example, CORS support is enabled for the whole `PersonRepository`. `@CrossOrigin` provides attributes to configure CORS support, as the following example shows:
2022

23+
====
2124
[source, java]
2225
----
2326
@CrossOrigin(origins = "http://domain2.com",
2427
methods = { RequestMethod.GET, RequestMethod.POST, RequestMethod.DELETE },
2528
maxAge = 3600)
2629
interface PersonRepository extends CrudRepository<Person, Long> {}
2730
----
31+
====
2832

29-
This example enables CORS support for the whole `PersonRepository` providing one origin, restricted to `GET`, `POST` and `DELETE` methods with a max age of 3600 seconds.
33+
The preceding example enables CORS support for the whole `PersonRepository` by providing one origin, restricted to the `GET`, `POST`, and `DELETE` methods and with a max age of 3600 seconds.
3034

31-
== Repository REST Controller method CORS configuration
35+
== Repository REST Controller Method CORS Configuration
3236

33-
Spring Data REST fully supports http://docs.spring.io/spring/docs/{springVersion}/spring-framework-reference/web.html#controller-method-cors-configuration[Spring Web MVC's Controller method configuration] on custom REST Controllers sharing repository base paths.
37+
Spring Data REST fully supports http://docs.spring.io/spring/docs/{springVersion}/spring-framework-reference/web.html#controller-method-cors-configuration[Spring Web MVC's controller method configuration] on custom REST controllers that share repository base paths, as the following example shows:
3438

39+
====
3540
[source, java]
3641
----
3742
@RepositoryRestController
@@ -44,15 +49,19 @@ public class PersonController {
4449
}
4550
}
4651
----
52+
====
4753

4854
NOTE: Controllers annotated with `@RepositoryRestController` inherit `@CrossOrigin` configuration from their associated repositories.
4955

50-
== Global CORS configuration
56+
== Global CORS Configuration
5157

52-
In addition to fine-grained, annotation-based configuration you’ll probably want to define some global CORS configuration as well. This is similar to Spring Web MVC'S CORS configuration but can be declared within Spring Data REST and combined with fine-grained `@CrossOrigin` configuration. By default all origins and `GET`, `HEAD`, and `POST` methods are allowed.
58+
In addition to fine-grained, annotation-based configuration, you probably want to define some global CORS configuration as well. This is similar to Spring Web MVC'S CORS configuration but can be declared within Spring Data REST and combined with fine-grained `@CrossOrigin` configuration. By default, all origins and `GET`, `HEAD`, and `POST` methods are allowed.
5359

5460
NOTE: Existing Spring Web MVC CORS configuration is not applied to Spring Data REST.
5561

62+
The following example sets an allowed origin, adds the PUT and DELETE HTTP methods, adds and exposes some headers, and sets a maximum age of an hour:
63+
64+
====
5665
[source, java]
5766
----
5867
@Component
@@ -70,4 +79,4 @@ public class SpringDataRestCustomization extends RepositoryRestConfigurerAdapter
7079
}
7180
}
7281
----
73-
82+
====

0 commit comments

Comments
 (0)