Add example of explicit handler method registration

Issue: SPR-16336
This commit is contained in:
Rossen Stoyanchev
2018-06-12 15:22:53 -04:00
parent 39af1b2281
commit 928b7804c8
2 changed files with 70 additions and 0 deletions

View File

@@ -1306,6 +1306,41 @@ logic. This is a more advanced option that requires sub-classing
you can check the custom attribute and return your own `RequestCondition`.
[[webflux-ann-requestmapping-registration]]
==== Explicit Registrations
[.small]#<<web.adoc#mvc-ann-requestmapping-registration,Same in Spring MVC>>#
Handler methods can be registered programmatically which can be used for dynamic
registrations, or for advanced cases such as different instances of the same handler
under different URLs. Below is an example:
[source,java,indent=0]
[subs="verbatim,quotes"]
----
@Configuration
public class MyConfig {
@Autowired
public void setHandlerMapping(RequestMappingHandlerMapping mapping, UserHandler handler) <1>
throws NoSuchMethodException {
RequestMappingInfo info = RequestMappingInfo
.paths("/user/{id}").methods(RequestMethod.GET).build(); <2>
Method method = UserHandler.class.getMethod("getUser", Long.class); <3>
mapping.registerMapping(info, handler, method); <4>
}
}
----
<1> Inject target handler(s) and the handler mapping for controllers.
<2> Prepare the request mapping meta data.
<3> Get the handler method.
<4> Add the registration.
[[webflux-ann-methods]]
=== Handler methods

View File

@@ -1571,6 +1571,41 @@ logic. This is a more advanced option that requires sub-classing
you can check the custom attribute and return your own `RequestCondition`.
[[mvc-ann-requestmapping-registration]]
==== Explicit Registrations
[.small]#<<web-reactive.adoc#webflux-ann-requestmapping-registration,Same in Spring WebFlux>>#
Handler methods can be registered programmatically which can be used for dynamic
registrations, or for advanced cases such as different instances of the same handler
under different URLs. Below is an example:
[source,java,indent=0]
[subs="verbatim,quotes"]
----
@Configuration
public class MyConfig {
@Autowired
public void setHandlerMapping(RequestMappingHandlerMapping mapping, UserHandler handler) <1>
throws NoSuchMethodException {
RequestMappingInfo info = RequestMappingInfo
.paths("/user/{id}").methods(RequestMethod.GET).build(); <2>
Method method = UserHandler.class.getMethod("getUser", Long.class); <3>
mapping.registerMapping(info, handler, method); <4>
}
}
----
<1> Inject target handler(s) and the handler mapping for controllers.
<2> Prepare the request mapping meta data.
<3> Get the handler method.
<4> Add the registration.
[[mvc-ann-methods]]
=== Handler Methods