Browse Source

MvcUriComponentsBuilder prepends slash

See gh-24143
pull/24067/merge
Astushi Yoshikawa 5 years ago committed by Rossen Stoyanchev
parent
commit
53b39eb753
  1. 10
      spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/MvcUriComponentsBuilder.java
  2. 33
      spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/MvcUriComponentsBuilderTests.java

10
spring-webmvc/src/main/java/org/springframework/web/servlet/mvc/method/annotation/MvcUriComponentsBuilder.java

@ -102,6 +102,8 @@ public class MvcUriComponentsBuilder { @@ -102,6 +102,8 @@ public class MvcUriComponentsBuilder {
*/
public static final String MVC_URI_COMPONENTS_CONTRIBUTOR_BEAN_NAME = "mvcUriComponentsContributor";
/** Path separator: "/". */
public static final String PATH_SEPARATOR = "/";
private static final Log logger = LogFactory.getLog(MvcUriComponentsBuilder.class);
@ -545,7 +547,7 @@ public class MvcUriComponentsBuilder { @@ -545,7 +547,7 @@ public class MvcUriComponentsBuilder {
String typePath = getClassMapping(controllerType);
String methodPath = getMethodMapping(method);
String path = pathMatcher.combine(typePath, methodPath);
builder.path(path);
builder.path(path.startsWith(PATH_SEPARATOR) ? path : PATH_SEPARATOR + path);
return applyContributors(builder, method, args);
}
@ -576,11 +578,11 @@ public class MvcUriComponentsBuilder { @@ -576,11 +578,11 @@ public class MvcUriComponentsBuilder {
Assert.notNull(controllerType, "'controllerType' must not be null");
RequestMapping mapping = AnnotatedElementUtils.findMergedAnnotation(controllerType, RequestMapping.class);
if (mapping == null) {
return "/";
return PATH_SEPARATOR;
}
String[] paths = mapping.path();
if (ObjectUtils.isEmpty(paths) || !StringUtils.hasLength(paths[0])) {
return "/";
return PATH_SEPARATOR;
}
if (paths.length > 1 && logger.isTraceEnabled()) {
logger.trace("Using first of multiple paths on " + controllerType.getName());
@ -596,7 +598,7 @@ public class MvcUriComponentsBuilder { @@ -596,7 +598,7 @@ public class MvcUriComponentsBuilder {
}
String[] paths = requestMapping.path();
if (ObjectUtils.isEmpty(paths) || !StringUtils.hasLength(paths[0])) {
return "/";
return PATH_SEPARATOR;
}
if (paths.length > 1 && logger.isTraceEnabled()) {
logger.trace("Using first of multiple paths on " + method.toGenericString());

33
spring-webmvc/src/test/java/org/springframework/web/servlet/mvc/method/annotation/MvcUriComponentsBuilderTests.java

@ -434,6 +434,20 @@ public class MvcUriComponentsBuilderTests { @@ -434,6 +434,20 @@ public class MvcUriComponentsBuilderTests {
assertThat(url).isEqualTo("/base/people/_%2B_/addresses/DE%3BFR");
}
@Test
public void fromMappingNameWithNonSlashPath() {
initWebApplicationContext(NonSlashPathWebConfig.class);
this.request.setServerName("example.org");
this.request.setServerPort(9999);
this.request.setContextPath("/base");
String mappingName = "NSPC#getAddressesForCountry";
String url = fromMappingName(mappingName).arg(0, "DE;FR").encode().buildAndExpand("_+_");
assertThat(url).isEqualTo("/base/people/DE%3BFR");
}
@Test
public void fromControllerWithPrefix() {
@ -499,6 +513,15 @@ public class MvcUriComponentsBuilderTests { @@ -499,6 +513,15 @@ public class MvcUriComponentsBuilderTests {
}
@RequestMapping({"people"})
static class NonSlashPathController {
@RequestMapping("/{country}")
HttpEntity<Void> getAddressesForCountry(@PathVariable String country) {
return null;
}
}
@RequestMapping({"/persons", "/people"})
private class InvalidController {
}
@ -622,6 +645,16 @@ public class MvcUriComponentsBuilderTests { @@ -622,6 +645,16 @@ public class MvcUriComponentsBuilderTests {
}
@EnableWebMvc
static class NonSlashPathWebConfig implements WebMvcConfigurer {
@Bean
public NonSlashPathController controller() {
return new NonSlashPathController();
}
}
@EnableWebMvc
static class PathPrefixWebConfig implements WebMvcConfigurer {

Loading…
Cancel
Save