Fix init order issue in RequestMappingHandlerMapping
Issue: SPR-10173
This commit is contained in:
parent
902a136b6e
commit
660458a649
|
|
@ -157,10 +157,10 @@ public class RequestMappingHandlerMapping extends RequestMappingInfoHandlerMappi
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void afterPropertiesSet() {
|
public void afterPropertiesSet() {
|
||||||
super.afterPropertiesSet();
|
|
||||||
if (this.useRegisteredSuffixPatternMatch) {
|
if (this.useRegisteredSuffixPatternMatch) {
|
||||||
this.fileExtensions.addAll(contentNegotiationManager.getAllFileExtensions());
|
this.fileExtensions.addAll(contentNegotiationManager.getAllFileExtensions());
|
||||||
}
|
}
|
||||||
|
super.afterPropertiesSet();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
|
|
@ -15,20 +15,28 @@
|
||||||
*/
|
*/
|
||||||
package org.springframework.web.servlet.mvc.method.annotation;
|
package org.springframework.web.servlet.mvc.method.annotation;
|
||||||
|
|
||||||
import static org.junit.Assert.*;
|
import static org.junit.Assert.assertArrayEquals;
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.junit.Assert.assertFalse;
|
||||||
|
import static org.junit.Assert.assertTrue;
|
||||||
|
|
||||||
|
import java.lang.reflect.Method;
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
import java.util.Collections;
|
import java.util.Collections;
|
||||||
|
import java.util.HashSet;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
import org.junit.Before;
|
import org.junit.Before;
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
import org.springframework.http.MediaType;
|
import org.springframework.http.MediaType;
|
||||||
|
import org.springframework.stereotype.Controller;
|
||||||
import org.springframework.util.StringValueResolver;
|
import org.springframework.util.StringValueResolver;
|
||||||
import org.springframework.web.accept.ContentNegotiationManager;
|
import org.springframework.web.accept.ContentNegotiationManager;
|
||||||
import org.springframework.web.accept.PathExtensionContentNegotiationStrategy;
|
import org.springframework.web.accept.PathExtensionContentNegotiationStrategy;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
import org.springframework.web.context.support.StaticWebApplicationContext;
|
import org.springframework.web.context.support.StaticWebApplicationContext;
|
||||||
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
|
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Tests for {@link RequestMappingHandlerMapping}.
|
* Tests for {@link RequestMappingHandlerMapping}.
|
||||||
|
|
@ -63,6 +71,35 @@ public class RequestMappingHandlerMappingTests {
|
||||||
assertEquals(Arrays.asList("json"), this.handlerMapping.getFileExtensions());
|
assertEquals(Arrays.asList("json"), this.handlerMapping.getFileExtensions());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void useRegsiteredSuffixPatternMatchInitialization() {
|
||||||
|
|
||||||
|
Map<String, MediaType> fileExtensions = Collections.singletonMap("json", MediaType.APPLICATION_JSON);
|
||||||
|
PathExtensionContentNegotiationStrategy strategy = new PathExtensionContentNegotiationStrategy(fileExtensions);
|
||||||
|
ContentNegotiationManager manager = new ContentNegotiationManager(strategy);
|
||||||
|
|
||||||
|
final Set<String> extensions = new HashSet<String>();
|
||||||
|
|
||||||
|
RequestMappingHandlerMapping hm = new RequestMappingHandlerMapping() {
|
||||||
|
@Override
|
||||||
|
protected RequestMappingInfo getMappingForMethod(Method method, Class<?> handlerType) {
|
||||||
|
extensions.addAll(getFileExtensions());
|
||||||
|
return super.getMappingForMethod(method, handlerType);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
StaticWebApplicationContext wac = new StaticWebApplicationContext();
|
||||||
|
wac.registerSingleton("testController", TestController.class);
|
||||||
|
wac.refresh();
|
||||||
|
|
||||||
|
hm.setContentNegotiationManager(manager);
|
||||||
|
hm.setUseRegisteredSuffixPatternMatch(true);
|
||||||
|
hm.setApplicationContext(wac);
|
||||||
|
hm.afterPropertiesSet();
|
||||||
|
|
||||||
|
assertEquals(Collections.singleton("json"), extensions);
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void useSuffixPatternMatch() {
|
public void useSuffixPatternMatch() {
|
||||||
assertTrue(this.handlerMapping.useSuffixPatternMatch());
|
assertTrue(this.handlerMapping.useSuffixPatternMatch());
|
||||||
|
|
@ -93,4 +130,13 @@ public class RequestMappingHandlerMappingTests {
|
||||||
assertArrayEquals(new String[] { "/foo", "/foo/bar" }, result);
|
assertArrayEquals(new String[] { "/foo", "/foo/bar" }, result);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Controller
|
||||||
|
static class TestController {
|
||||||
|
|
||||||
|
@RequestMapping
|
||||||
|
public void handle() {
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue