Skip to content

Commit 660458a

Browse files
committed
Fix init order issue in RequestMappingHandlerMapping
Issue: SPR-10173
1 parent 902a136 commit 660458a

File tree

2 files changed

+49
-3
lines changed

2 files changed

+49
-3
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -157,10 +157,10 @@ public List<String> getFileExtensions() {
157157

158158
@Override
159159
public void afterPropertiesSet() {
160-
super.afterPropertiesSet();
161160
if (this.useRegisteredSuffixPatternMatch) {
162161
this.fileExtensions.addAll(contentNegotiationManager.getAllFileExtensions());
163162
}
163+
super.afterPropertiesSet();
164164
}
165165

166166
/**

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

Lines changed: 48 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,20 +15,28 @@
1515
*/
1616
package org.springframework.web.servlet.mvc.method.annotation;
1717

18-
import static org.junit.Assert.*;
18+
import static org.junit.Assert.assertArrayEquals;
19+
import static org.junit.Assert.assertEquals;
20+
import static org.junit.Assert.assertFalse;
21+
import static org.junit.Assert.assertTrue;
1922

23+
import java.lang.reflect.Method;
2024
import java.util.Arrays;
2125
import java.util.Collections;
26+
import java.util.HashSet;
2227
import java.util.Map;
28+
import java.util.Set;
2329

2430
import org.junit.Before;
2531
import org.junit.Test;
2632
import org.springframework.http.MediaType;
33+
import org.springframework.stereotype.Controller;
2734
import org.springframework.util.StringValueResolver;
2835
import org.springframework.web.accept.ContentNegotiationManager;
2936
import org.springframework.web.accept.PathExtensionContentNegotiationStrategy;
37+
import org.springframework.web.bind.annotation.RequestMapping;
3038
import org.springframework.web.context.support.StaticWebApplicationContext;
31-
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
39+
import org.springframework.web.servlet.mvc.method.RequestMappingInfo;
3240

3341
/**
3442
* Tests for {@link RequestMappingHandlerMapping}.
@@ -63,6 +71,35 @@ public void useRegsiteredSuffixPatternMatch() {
6371
assertEquals(Arrays.asList("json"), this.handlerMapping.getFileExtensions());
6472
}
6573

74+
@Test
75+
public void useRegsiteredSuffixPatternMatchInitialization() {
76+
77+
Map<String, MediaType> fileExtensions = Collections.singletonMap("json", MediaType.APPLICATION_JSON);
78+
PathExtensionContentNegotiationStrategy strategy = new PathExtensionContentNegotiationStrategy(fileExtensions);
79+
ContentNegotiationManager manager = new ContentNegotiationManager(strategy);
80+
81+
final Set<String> extensions = new HashSet<String>();
82+
83+
RequestMappingHandlerMapping hm = new RequestMappingHandlerMapping() {
84+
@Override
85+
protected RequestMappingInfo getMappingForMethod(Method method, Class<?> handlerType) {
86+
extensions.addAll(getFileExtensions());
87+
return super.getMappingForMethod(method, handlerType);
88+
}
89+
};
90+
91+
StaticWebApplicationContext wac = new StaticWebApplicationContext();
92+
wac.registerSingleton("testController", TestController.class);
93+
wac.refresh();
94+
95+
hm.setContentNegotiationManager(manager);
96+
hm.setUseRegisteredSuffixPatternMatch(true);
97+
hm.setApplicationContext(wac);
98+
hm.afterPropertiesSet();
99+
100+
assertEquals(Collections.singleton("json"), extensions);
101+
}
102+
66103
@Test
67104
public void useSuffixPatternMatch() {
68105
assertTrue(this.handlerMapping.useSuffixPatternMatch());
@@ -93,4 +130,13 @@ public String resolveStringValue(String value) {
93130
assertArrayEquals(new String[] { "/foo", "/foo/bar" }, result);
94131
}
95132

133+
134+
@Controller
135+
static class TestController {
136+
137+
@RequestMapping
138+
public void handle() {
139+
}
140+
}
141+
96142
}

0 commit comments

Comments
 (0)