Polishing contribution

See gh-24143
This commit is contained in:
Rossen Stoyanchev 2020-01-08 16:46:53 +00:00
parent 53b39eb753
commit d509d5ae6f
2 changed files with 16 additions and 16 deletions

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2002-2019 the original author or authors. * Copyright 2002-2020 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -102,8 +102,6 @@ public class MvcUriComponentsBuilder {
*/ */
public static final String MVC_URI_COMPONENTS_CONTRIBUTOR_BEAN_NAME = "mvcUriComponentsContributor"; 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); private static final Log logger = LogFactory.getLog(MvcUriComponentsBuilder.class);
@ -547,7 +545,10 @@ public class MvcUriComponentsBuilder {
String typePath = getClassMapping(controllerType); String typePath = getClassMapping(controllerType);
String methodPath = getMethodMapping(method); String methodPath = getMethodMapping(method);
String path = pathMatcher.combine(typePath, methodPath); String path = pathMatcher.combine(typePath, methodPath);
builder.path(path.startsWith(PATH_SEPARATOR) ? path : PATH_SEPARATOR + path); if (StringUtils.hasLength(path) && !path.startsWith("/")) {
path = "/" + path;
}
builder.path(path);
return applyContributors(builder, method, args); return applyContributors(builder, method, args);
} }
@ -578,11 +579,11 @@ public class MvcUriComponentsBuilder {
Assert.notNull(controllerType, "'controllerType' must not be null"); Assert.notNull(controllerType, "'controllerType' must not be null");
RequestMapping mapping = AnnotatedElementUtils.findMergedAnnotation(controllerType, RequestMapping.class); RequestMapping mapping = AnnotatedElementUtils.findMergedAnnotation(controllerType, RequestMapping.class);
if (mapping == null) { if (mapping == null) {
return PATH_SEPARATOR; return "/";
} }
String[] paths = mapping.path(); String[] paths = mapping.path();
if (ObjectUtils.isEmpty(paths) || !StringUtils.hasLength(paths[0])) { if (ObjectUtils.isEmpty(paths) || !StringUtils.hasLength(paths[0])) {
return PATH_SEPARATOR; return "/";
} }
if (paths.length > 1 && logger.isTraceEnabled()) { if (paths.length > 1 && logger.isTraceEnabled()) {
logger.trace("Using first of multiple paths on " + controllerType.getName()); logger.trace("Using first of multiple paths on " + controllerType.getName());
@ -598,7 +599,7 @@ public class MvcUriComponentsBuilder {
} }
String[] paths = requestMapping.path(); String[] paths = requestMapping.path();
if (ObjectUtils.isEmpty(paths) || !StringUtils.hasLength(paths[0])) { if (ObjectUtils.isEmpty(paths) || !StringUtils.hasLength(paths[0])) {
return PATH_SEPARATOR; return "/";
} }
if (paths.length > 1 && logger.isTraceEnabled()) { if (paths.length > 1 && logger.isTraceEnabled()) {
logger.trace("Using first of multiple paths on " + method.toGenericString()); logger.trace("Using first of multiple paths on " + method.toGenericString());

View File

@ -1,5 +1,5 @@
/* /*
* Copyright 2012-2019 the original author or authors. * Copyright 2012-2020 the original author or authors.
* *
* Licensed under the Apache License, Version 2.0 (the "License"); * Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License. * you may not use this file except in compliance with the License.
@ -435,15 +435,15 @@ public class MvcUriComponentsBuilderTests {
} }
@Test @Test
public void fromMappingNameWithNonSlashPath() { public void fromMappingNameWithPathWithoutLeadingSlash() {
initWebApplicationContext(NonSlashPathWebConfig.class); initWebApplicationContext(PathWithoutLeadingSlashConfig.class);
this.request.setServerName("example.org"); this.request.setServerName("example.org");
this.request.setServerPort(9999); this.request.setServerPort(9999);
this.request.setContextPath("/base"); this.request.setContextPath("/base");
String mappingName = "NSPC#getAddressesForCountry"; String mappingName = "PWLSC#getAddressesForCountry";
String url = fromMappingName(mappingName).arg(0, "DE;FR").encode().buildAndExpand("_+_"); String url = fromMappingName(mappingName).arg(0, "DE;FR").encode().buildAndExpand("_+_");
assertThat(url).isEqualTo("/base/people/DE%3BFR"); assertThat(url).isEqualTo("/base/people/DE%3BFR");
} }
@ -512,9 +512,8 @@ public class MvcUriComponentsBuilderTests {
} }
} }
@RequestMapping({"people"}) @RequestMapping({"people"})
static class NonSlashPathController { static class PathWithoutLeadingSlashController {
@RequestMapping("/{country}") @RequestMapping("/{country}")
HttpEntity<Void> getAddressesForCountry(@PathVariable String country) { HttpEntity<Void> getAddressesForCountry(@PathVariable String country) {
@ -646,11 +645,11 @@ public class MvcUriComponentsBuilderTests {
@EnableWebMvc @EnableWebMvc
static class NonSlashPathWebConfig implements WebMvcConfigurer { static class PathWithoutLeadingSlashConfig implements WebMvcConfigurer {
@Bean @Bean
public NonSlashPathController controller() { public PathWithoutLeadingSlashController controller() {
return new NonSlashPathController(); return new PathWithoutLeadingSlashController();
} }
} }