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.
This commit is contained in:
parent
0d3fa4eb70
commit
6833d4cdff
|
@ -18,10 +18,9 @@ Data Binding, Type Conversion, SpEL, AOP.
|
||||||
Spring MVC Test, WebTestClient.
|
Spring MVC Test, WebTestClient.
|
||||||
<<data-access.adoc#spring-data-tier,Data Access>> :: Transactions, DAO support, JDBC,
|
<<data-access.adoc#spring-data-tier,Data Access>> :: Transactions, DAO support, JDBC,
|
||||||
ORM, Marshalling XML.
|
ORM, Marshalling XML.
|
||||||
<<web.adoc#spring-web,Web Servlet>> :: Spring MVC web framework, WebSocket,
|
<<web.adoc#spring-web,Web Servlet>> :: Spring MVC, WebSocket, SockJS, STOMP messaging.
|
||||||
SockJS, STOMP messaging.
|
|
||||||
<<web-reactive.adoc#spring-webflux,Web Reactive>> :: Spring WebFlux,
|
<<web-reactive.adoc#spring-webflux,Web Reactive>> :: Spring WebFlux,
|
||||||
WebClient, WebSocket support.
|
WebClient, WebSocket.
|
||||||
<<integration.adoc#spring-integration,Integration>> :: Remoting, JMS, JCA, JMX, Email,
|
<<integration.adoc#spring-integration,Integration>> :: Remoting, JMS, JCA, JMX, Email,
|
||||||
Tasks, Scheduling, Cache, Dynamic languages.
|
Tasks, Scheduling, Cache, Dynamic languages.
|
||||||
<<kotlin.adoc#kotlin,Kotlin>> :: Extensions, Bean Definition DSL, WebFlux DSL.
|
<<kotlin.adoc#kotlin,Kotlin>> :: Extensions, Bean Definition DSL, WebFlux DSL.
|
||||||
|
|
|
@ -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.adoc[leveloffset=+1]
|
||||||
|
|
||||||
include::web/webmvc-view.adoc[leveloffset=+1]
|
include::web/webmvc-test.adoc[leveloffset=+1]
|
||||||
|
|
||||||
include::web/webmvc-cors.adoc[leveloffset=+1]
|
|
||||||
|
|
||||||
include::web/websocket.adoc[leveloffset=+1]
|
include::web/websocket.adoc[leveloffset=+1]
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
|
|
||||||
[[web-integration]]
|
[[web-integration]]
|
||||||
= 3rd Party Web Frameworks
|
= Other Web Frameworks
|
||||||
|
|
||||||
|
|
||||||
[[intro]]
|
[[intro]]
|
||||||
|
|
|
@ -31,6 +31,7 @@ Since CORS requests are automatically dispatched, you *do not need* to change th
|
||||||
(`false`) is the recommended approach.
|
(`false`) is the recommended approach.
|
||||||
====
|
====
|
||||||
|
|
||||||
|
[[mvc-cors-controller]]
|
||||||
== @Controller CORS
|
== @Controller CORS
|
||||||
|
|
||||||
You can add an
|
You can add an
|
||||||
|
@ -109,6 +110,7 @@ public class AccountController {
|
||||||
}
|
}
|
||||||
----
|
----
|
||||||
|
|
||||||
|
[[mvc-cors-global]]
|
||||||
== Global CORS
|
== Global CORS
|
||||||
|
|
||||||
In addition to fine-grained, annotation-based configuration you'll probably want to
|
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
|
||||||
be declared within Spring MVC and combined with fine-grained `@CrossOrigin` configuration.
|
be declared within Spring MVC and combined with fine-grained `@CrossOrigin` configuration.
|
||||||
By default all origins and `GET`, `HEAD`, and `POST` methods are allowed.
|
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:
|
Enabling CORS for the whole application is as simple as:
|
||||||
|
|
||||||
|
@ -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 following minimal XML configuration enables CORS for the `/**` path pattern with
|
||||||
the same default properties as with the aforementioned JavaConfig examples:
|
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:
|
||||||
</mvc:cors>
|
</mvc:cors>
|
||||||
----
|
----
|
||||||
|
|
||||||
|
[[mvc-cors-customizations]]
|
||||||
== Advanced Customization
|
== Advanced Customization
|
||||||
|
|
||||||
{api-spring-framework}/web/cors/CorsConfiguration.html[CorsConfiguration]
|
{api-spring-framework}/web/cors/CorsConfiguration.html[CorsConfiguration]
|
||||||
|
@ -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]
|
now does) in order to provide a {api-spring-framework}/web/cors/CorsConfiguration.html[CorsConfiguration]
|
||||||
instance for each request.
|
instance for each request.
|
||||||
|
|
||||||
|
|
||||||
|
[[mvc-cors-filter]]
|
||||||
== CORS Filter
|
== CORS Filter
|
||||||
|
|
||||||
In order to support CORS with filter-based security frameworks like
|
You can apply CORS checks through the built-in
|
||||||
http://projects.spring.io/spring-security/[Spring Security], or
|
http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/filter/CorsFilter.html[`CorsFilter`]
|
||||||
with other libraries that do not support natively CORS, Spring Framework also
|
which can be used with http://projects.spring.io/spring-security/[Spring Security] and
|
||||||
provides a http://docs.spring.io/spring/docs/current/javadoc-api/org/springframework/web/filter/CorsFilter.html[`CorsFilter`].
|
ordered ahead of its chain of filters. To configure the filter pass a
|
||||||
Instead of using `@CrossOrigin` or `WebMvcConfigurer#addCorsMappings(CorsRegistry)`, you
|
`CorsConfigurationSource` to its constructor:
|
||||||
need to register a custom filter defined like bellow:
|
|
||||||
|
|
||||||
[source,java,indent=0]
|
[source,java,indent=0]
|
||||||
----
|
----
|
||||||
import org.springframework.web.cors.CorsConfiguration;
|
|
||||||
import org.springframework.web.cors.UrlBasedCorsConfigurationSource;
|
|
||||||
import org.springframework.web.filter.CorsFilter;
|
|
||||||
|
|
||||||
public class MyCorsFilter extends CorsFilter {
|
|
||||||
|
|
||||||
public MyCorsFilter() {
|
|
||||||
super(configurationSource());
|
|
||||||
}
|
|
||||||
|
|
||||||
private static UrlBasedCorsConfigurationSource configurationSource() {
|
|
||||||
CorsConfiguration config = new CorsConfiguration();
|
CorsConfiguration config = new CorsConfiguration();
|
||||||
config.setAllowCredentials(true);
|
config.setAllowCredentials(true);
|
||||||
config.addAllowedOrigin("http://domain1.com");
|
config.addAllowedOrigin("http://domain1.com");
|
||||||
config.addAllowedHeader("*");
|
config.addAllowedHeader("*");
|
||||||
config.addAllowedMethod("*");
|
config.addAllowedMethod("*");
|
||||||
|
|
||||||
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
|
UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
|
||||||
source.registerCorsConfiguration("/**", config);
|
source.registerCorsConfiguration("/**", config);
|
||||||
return source;
|
|
||||||
}
|
CorsFilter filter = new CorsFilter(source);
|
||||||
}
|
|
||||||
----
|
----
|
||||||
|
|
||||||
You need to ensure that `CorsFilter` is ordered before the other filters, see
|
Also the information on
|
||||||
https://spring.io/blog/2015/06/08/cors-support-in-spring-framework#filter-based-cors-support[this blog post]
|
https://docs.spring.io/spring-security/site/docs/current/reference/htmlsingle/#cors[CORS]
|
||||||
about how to configure Spring Boot accordingly.
|
in the Spring Security reference.
|
|
@ -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.
|
File diff suppressed because it is too large
Load Diff
|
@ -985,7 +985,7 @@ public class WebSocketConfig extends WebSocketMessageBrokerConfigurationSupport
|
||||||
|
|
||||||
|
|
||||||
[[websocket-stomp]]
|
[[websocket-stomp]]
|
||||||
== STOMP Sub-protocol
|
== STOMP
|
||||||
The WebSocket protocol defines two types of messages, text and binary, but their
|
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
|
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.
|
a sub-protocol (i.e. a higher-level protocol) to define message semantics.
|
||||||
|
|
Loading…
Reference in New Issue