MvcUriComponentsBuilder is aware of path prefixes

Issue: SPR-16336
This commit is contained in:
Rossen Stoyanchev
2018-06-14 22:05:53 -04:00
parent e6fef9555d
commit 58cce615f5
3 changed files with 108 additions and 31 deletions

View File

@@ -56,6 +56,7 @@ import org.springframework.web.filter.ForwardedHeaderFilter;
import org.springframework.web.servlet.DispatcherServlet;
import org.springframework.web.servlet.ModelAndView;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.PathMatchConfigurer;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import org.springframework.web.util.UriComponents;
import org.springframework.web.util.UriComponentsBuilder;
@@ -378,12 +379,9 @@ public class MvcUriComponentsBuilderTests {
@Test
public void fromMappingNamePlain() {
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
context.setServletContext(new MockServletContext());
context.register(WebConfig.class);
context.refresh();
this.request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, context);
initWebApplicationContext(WebConfig.class);
this.request.setServerName("example.org");
this.request.setServerPort(9999);
this.request.setContextPath("/base");
@@ -395,12 +393,8 @@ public class MvcUriComponentsBuilderTests {
@Test
public void fromMappingNameWithCustomBaseUrl() {
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
context.setServletContext(new MockServletContext());
context.register(WebConfig.class);
context.refresh();
this.request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, context);
initWebApplicationContext(WebConfig.class);
UriComponentsBuilder baseUrl = UriComponentsBuilder.fromUriString("http://example.org:9999/base");
MvcUriComponentsBuilder mvcBuilder = relativeTo(baseUrl);
@@ -408,6 +402,41 @@ public class MvcUriComponentsBuilderTests {
assertEquals("http://example.org:9999/base/people/123/addresses/DE", url);
}
@Test
public void fromControllerWithPrefix() {
initWebApplicationContext(PathPrefixWebConfig.class);
this.request.setServerName("example.org");
this.request.setServerPort(9999);
this.request.setContextPath("/base");
assertEquals("http://example.org:9999/base/api/people/123/addresses",
fromController(PersonsAddressesController.class).buildAndExpand("123").toString());
}
@Test
public void fromMethodWithPrefix() {
initWebApplicationContext(PathPrefixWebConfig.class);
this.request.setServerName("example.org");
this.request.setServerPort(9999);
this.request.setContextPath("/base");
assertEquals("http://example.org:9999/base/api/people/123/addresses/DE",
fromMethodCall(on(PersonsAddressesController.class).getAddressesForCountry("DE"))
.buildAndExpand("123").toString());
}
private void initWebApplicationContext(Class<?> configClass) {
AnnotationConfigWebApplicationContext context = new AnnotationConfigWebApplicationContext();
context.setServletContext(new MockServletContext());
context.register(configClass);
context.refresh();
this.request.setAttribute(DispatcherServlet.WEB_APPLICATION_CONTEXT_ATTRIBUTE, context);
}
static class Person {
@@ -555,6 +584,21 @@ public class MvcUriComponentsBuilderTests {
}
@EnableWebMvc
static class PathPrefixWebConfig implements WebMvcConfigurer {
@Override
public void configurePathMatch(PathMatchConfigurer configurer) {
configurer.addPathPrefix("/api", PersonsAddressesController.class::equals);
}
@Bean
public PersonsAddressesController controller() {
return new PersonsAddressesController();
}
}
@Controller
@RequestMapping("/hotels/{hotel}")
static class BookingControllerWithModelAndView {