Revised reference example for linkable controller method signature

Issue: SPR-16710

(cherry picked from commit 7ee6130)
This commit is contained in:
Juergen Hoeller
2018-04-12 14:45:11 +02:00
parent fecbcd4e93
commit ac19c696fe
2 changed files with 275 additions and 216 deletions

View File

@@ -3314,7 +3314,8 @@ also have the literal part of the servlet mapping included:
[[mvc-links-to-controllers]]
=== Building URIs to Controllers and methods
Spring MVC also provides a mechanism for building links to controller methods. For example, given:
Spring MVC provides a mechanism to prepare links to controller methods. For example,
the following MVC controller easily allows for link creation:
[source,java,indent=0]
[subs="verbatim,quotes"]
@@ -3324,7 +3325,7 @@ Spring MVC also provides a mechanism for building links to controller methods. F
public class BookingController {
@GetMapping("/bookings/{booking}")
public String getBooking(@PathVariable Long booking) {
public ModelAndView getBooking(@PathVariable Long booking) {
// ...
}
}
@@ -3361,6 +3362,16 @@ akin to mock testing through proxies to avoid referring to the controller method
URI uri = uriComponents.encode().toUri();
----
[NOTE]
====
Controller method signatures are limited in their design when supposed to be usable for
link creation with `fromMethodCall`. Aside from needing a proper parameter signature,
there is a technical limitation on the return type: namely generating a runtime proxy
for link builder invocations, so the return type must not be `final`. In particular,
the common `String` return type for view names does not work here; use `ModelAndView`
or even plain `Object` (with a `String` return value) instead.
====
The above examples use static methods in `MvcUriComponentsBuilder`. Internally they rely
on `ServletUriComponentsBuilder` to prepare a base URL from the scheme, host, port,
context path and servlet path of the current request. This works well in most cases,