Browse Source

Spring MVC content refactoring

Easier to find (at the top level) and better summary of testing support.
Improve structure headings names.
Remove or update outdated content.
pull/1553/head
Rossen Stoyanchev 8 years ago
parent
commit
6833d4cdff
  1. 5
      src/docs/asciidoc/index.adoc
  2. 4
      src/docs/asciidoc/web.adoc
  3. 2
      src/docs/asciidoc/web/integration.adoc
  4. 57
      src/docs/asciidoc/web/webmvc-cors.adoc
  5. 33
      src/docs/asciidoc/web/webmvc-test.adoc
  6. 2123
      src/docs/asciidoc/web/webmvc.adoc
  7. 2
      src/docs/asciidoc/web/websocket.adoc

5
src/docs/asciidoc/index.adoc

@ -18,10 +18,9 @@ Data Binding, Type Conversion, SpEL, AOP. @@ -18,10 +18,9 @@ Data Binding, Type Conversion, SpEL, AOP.
Spring MVC Test, WebTestClient.
<<data-access.adoc#spring-data-tier,Data Access>> :: Transactions, DAO support, JDBC,
ORM, Marshalling XML.
<<web.adoc#spring-web,Web Servlet>> :: Spring MVC web framework, WebSocket,
SockJS, STOMP messaging.
<<web.adoc#spring-web,Web Servlet>> :: Spring MVC, WebSocket, SockJS, STOMP messaging.
<<web-reactive.adoc#spring-webflux,Web Reactive>> :: Spring WebFlux,
WebClient, WebSocket support.
WebClient, WebSocket.
<<integration.adoc#spring-integration,Integration>> :: Remoting, JMS, JCA, JMX, Email,
Tasks, Scheduling, Cache, Dynamic languages.
<<kotlin.adoc#kotlin,Kotlin>> :: Extensions, Bean Definition DSL, WebFlux DSL.

4
src/docs/asciidoc/web.adoc

@ -13,9 +13,7 @@ For reactive stack, web applications, go to <<web-reactive.adoc#spring-web-react @@ -13,9 +13,7 @@ For reactive stack, web applications, go to <<web-reactive.adoc#spring-web-react
include::web/webmvc.adoc[leveloffset=+1]
include::web/webmvc-view.adoc[leveloffset=+1]
include::web/webmvc-cors.adoc[leveloffset=+1]
include::web/webmvc-test.adoc[leveloffset=+1]
include::web/websocket.adoc[leveloffset=+1]

2
src/docs/asciidoc/web/integration.adoc

@ -1,6 +1,6 @@ @@ -1,6 +1,6 @@
[[web-integration]]
= 3rd Party Web Frameworks
= Other Web Frameworks
[[intro]]

57
src/docs/asciidoc/web/webmvc-cors.adoc

@ -31,6 +31,7 @@ Since CORS requests are automatically dispatched, you *do not need* to change th @@ -31,6 +31,7 @@ Since CORS requests are automatically dispatched, you *do not need* to change th
(`false`) is the recommended approach.
====
[[mvc-cors-controller]]
== @Controller CORS
You can add an
@ -109,6 +110,7 @@ public class AccountController { @@ -109,6 +110,7 @@ public class AccountController {
}
----
[[mvc-cors-global]]
== Global CORS
In addition to fine-grained, annotation-based configuration you'll probably want to
@ -116,7 +118,9 @@ define some global CORS configuration as well. This is similar to using filters @@ -116,7 +118,9 @@ define some global CORS configuration as well. This is similar to using filters
be declared within Spring MVC and combined with fine-grained `@CrossOrigin` configuration.
By default all origins and `GET`, `HEAD`, and `POST` methods are allowed.
=== MVC Java Config
[[mvc-cors-global-java]]
=== Java Config
Enabling CORS for the whole application is as simple as:
@ -156,7 +160,9 @@ public class WebConfig implements WebMvcConfigurer { @@ -156,7 +160,9 @@ public class WebConfig implements WebMvcConfigurer {
}
----
=== MVC XML Config
[[mvc-cors-global-xml]]
=== XML Config
The following minimal XML configuration enables CORS for the `/**` path pattern with
the same default properties as with the aforementioned JavaConfig examples:
@ -189,6 +195,7 @@ It is also possible to declare several CORS mappings with customized properties: @@ -189,6 +195,7 @@ It is also possible to declare several CORS mappings with customized properties:
</mvc:cors>
----
[[mvc-cors-customizations]]
== Advanced Customization
{api-spring-framework}/web/cors/CorsConfiguration.html[CorsConfiguration]
@ -205,40 +212,30 @@ It can be provided in various ways: @@ -205,40 +212,30 @@ It can be provided in various ways:
now does) in order to provide a {api-spring-framework}/web/cors/CorsConfiguration.html[CorsConfiguration]
instance for each request.
[[mvc-cors-filter]]
== CORS Filter
In order to support CORS with filter-based security frameworks like
http://projects.spring.io/spring-security/[Spring Security], or
with other libraries that do not support natively CORS, Spring Framework also
provides a http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/filter/CorsFilter.html[`CorsFilter`].
Instead of using `@CrossOrigin` or `WebMvcConfigurer#addCorsMappings(CorsRegistry)`, you
need to register a custom filter defined like bellow:
You can apply CORS checks through the built-in
http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/filter/CorsFilter.html[`CorsFilter`]
which can be used with http://projects.spring.io/spring-security/[Spring Security] and
ordered ahead of its chain of filters. To configure the filter pass a
`CorsConfigurationSource` to its constructor:
[source,java,indent=0]
----
import org.springframework.web.cors.CorsConfiguration;
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
import org.springframework.web.filter.CorsFilter;
CorsConfiguration config = new CorsConfiguration();
config.setAllowCredentials(true);
config.addAllowedOrigin("http://domain1.com");
config.addAllowedHeader("*");
config.addAllowedMethod("*");
public class MyCorsFilter extends CorsFilter {
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/**", config);
public MyCorsFilter() {
super(configurationSource());
}
private static UrlBasedCorsConfigurationSource configurationSource() {
CorsConfiguration config = new CorsConfiguration();
config.setAllowCredentials(true);
config.addAllowedOrigin("http://domain1.com");
config.addAllowedHeader("*");
config.addAllowedMethod("*");
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
source.registerCorsConfiguration("/**", config);
return source;
}
}
CorsFilter filter = new CorsFilter(source);
----
You need to ensure that `CorsFilter` is ordered before the other filters, see
https://spring.io/blog/2015/06/08/cors-support-in-spring-framework#filter-based-cors-support[this blog post]
about how to configure Spring Boot accordingly.
Also the information on
https://docs.spring.io/spring-security/site/docs/current/reference/htmlsingle/#cors[CORS]
in the Spring Security reference.

33
src/docs/asciidoc/web/webmvc-test.adoc

@ -0,0 +1,33 @@ @@ -0,0 +1,33 @@
[[testing]]
= Testing
[[testing-servlet-mocks]]
== Servlet API Mocks
`spring-test` provides mock implementations of Servlet API contracts for unit testing
controllers, filters, and other web components.
See <<testing.adoc#mock-objects-servlet,Servlet API>> mock objects for more details.
[[testing-testcontext]]
== TestContext Framework
`spring-test` provides support for loading Spring configuration in JUnit and TestNG tests
including efficient caching of the loaded configuration across test methods and support for
loading a `WebApplicationContext` with a `MockServletContext`.
See <<testing.adoc#testcontext-framework,TestContext Framework>> for more details.
[[testing-mockmvc]]
== Spring MVC Tests
`spring-test` provides an integration test framework for testing annotated controllers
through the `DispatcherServlet`, complete with Spring MVC infrastructure, but without an
HTTP server. See <<testing.adoc#spring-mvc-test-framework,Spring MVC Test>> for more details.
[[testing-resttemplate]]
== Client-side REST
`spring-test` provides a `MockRestServiceServer` that can be used as a mock server for
testing client-side code that internally uses the `RestTemplate`.
See <<testing.adoc#spring-mvc-test-client,Client REST Tests>> for more details.

2123
src/docs/asciidoc/web/webmvc.adoc

File diff suppressed because it is too large Load Diff

2
src/docs/asciidoc/web/websocket.adoc

@ -985,7 +985,7 @@ public class WebSocketConfig extends WebSocketMessageBrokerConfigurationSupport @@ -985,7 +985,7 @@ public class WebSocketConfig extends WebSocketMessageBrokerConfigurationSupport
[[websocket-stomp]]
== STOMP Sub-protocol
== STOMP
The WebSocket protocol defines two types of messages, text and binary, but their
content is undefined. It's expected that the client and server may agree on using
a sub-protocol (i.e. a higher-level protocol) to define message semantics.

Loading…
Cancel
Save