#269 - Make sure URL parts are joined by exactly one slash.
Original pull request: #423.
This commit is contained in:
committed by
Oliver Gierke
parent
792b5fcd22
commit
720ff797de
@@ -109,7 +109,17 @@ public class AnnotationMappingDiscoverer implements MappingDiscoverer {
|
||||
return typeMapping;
|
||||
}
|
||||
|
||||
return typeMapping == null || "/".equals(typeMapping) ? mapping[0] : typeMapping + mapping[0];
|
||||
return typeMapping == null || "/".equals(typeMapping) ? mapping[0] : join(typeMapping, mapping[0]);
|
||||
}
|
||||
|
||||
private String join(String typeMapping, String mapping) {
|
||||
StringBuilder builder = new StringBuilder();
|
||||
|
||||
builder.append(typeMapping.endsWith("/") ? typeMapping.substring(0, typeMapping.length() - 1) : typeMapping);
|
||||
builder.append('/');
|
||||
builder.append(mapping.startsWith("/") ? mapping.substring(1) : mapping);
|
||||
|
||||
return builder.toString();
|
||||
}
|
||||
|
||||
private String[] getMappingFrom(Annotation annotation) {
|
||||
|
||||
@@ -21,7 +21,9 @@ import static org.junit.Assert.*;
|
||||
import java.lang.reflect.Method;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.http.HttpEntity;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
|
||||
/**
|
||||
* Unit tests for {@link AnnotationMappingDiscoverer}.
|
||||
@@ -97,6 +99,25 @@ public class AnnotationMappingDiscovererUnitTest {
|
||||
assertThat(discoverer.getMapping(ChildWithTypeMapping.class, method), is("/child/parent"));
|
||||
}
|
||||
|
||||
/**
|
||||
* @see #269
|
||||
*/
|
||||
@Test
|
||||
public void handlesSlashes() throws Exception {
|
||||
|
||||
Method method = ControllerWithoutSlashes.class.getMethod("noslash");
|
||||
assertThat(discoverer.getMapping(method), is("slashes/noslash"));
|
||||
|
||||
method = ControllerWithoutSlashes.class.getMethod("withslash");
|
||||
assertThat(discoverer.getMapping(method), is("slashes/withslash"));
|
||||
|
||||
method = ControllerWithTrailingSlashes.class.getMethod("noslash");
|
||||
assertThat(discoverer.getMapping(method), is("trailing/noslash"));
|
||||
|
||||
method = ControllerWithTrailingSlashes.class.getMethod("withslash");
|
||||
assertThat(discoverer.getMapping(method), is("trailing/withslash"));
|
||||
}
|
||||
|
||||
@RequestMapping("/type")
|
||||
interface MyController {
|
||||
|
||||
@@ -139,4 +160,24 @@ public class AnnotationMappingDiscovererUnitTest {
|
||||
|
||||
@RequestMapping("/child")
|
||||
interface ChildWithTypeMapping extends ParentWithMethod {}
|
||||
|
||||
@RequestMapping("slashes")
|
||||
interface ControllerWithoutSlashes {
|
||||
|
||||
@RequestMapping("noslash")
|
||||
void noslash();
|
||||
|
||||
@RequestMapping("/withslash")
|
||||
void withslash();
|
||||
}
|
||||
|
||||
@RequestMapping("trailing/")
|
||||
interface ControllerWithTrailingSlashes {
|
||||
|
||||
@RequestMapping("noslash")
|
||||
void noslash();
|
||||
|
||||
@RequestMapping("/withslash")
|
||||
void withslash();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user