Skip to content

Commit 4d005b6

Browse files
committed
Improve default content type selection
Previously ContentNegotiationManager continued with the next ContentNegotiationStrategy only if the current one returned an empty list. Now it also does that if the current ContentNegotiationStrategy returns "*/*". Since the absence of an Accept header and "*/*" have the same meaning, this allows a default content type to be used in either case. Issue: SPR-10513
1 parent 532a7a3 commit 4d005b6

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

spring-web/src/main/java/org/springframework/web/accept/ContentNegotiationManager.java

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@
5050
*/
5151
public class ContentNegotiationManager implements ContentNegotiationStrategy, MediaTypeFileExtensionResolver {
5252

53+
private static final List<MediaType> MEDIA_TYPE_ALL = Arrays.asList(MediaType.ALL);
54+
5355
private final List<ContentNegotiationStrategy> contentNegotiationStrategies =
5456
new ArrayList<ContentNegotiationStrategy>();
5557

@@ -119,9 +121,10 @@ public void addFileExtensionResolvers(MediaTypeFileExtensionResolver... resolver
119121
public List<MediaType> resolveMediaTypes(NativeWebRequest webRequest) throws HttpMediaTypeNotAcceptableException {
120122
for (ContentNegotiationStrategy strategy : this.contentNegotiationStrategies) {
121123
List<MediaType> mediaTypes = strategy.resolveMediaTypes(webRequest);
122-
if (!mediaTypes.isEmpty()) {
123-
return mediaTypes;
124+
if (mediaTypes.isEmpty() || mediaTypes.equals(MEDIA_TYPE_ALL)) {
125+
continue;
124126
}
127+
return mediaTypes;
125128
}
126129
return Collections.emptyList();
127130
}

spring-web/src/test/java/org/springframework/web/accept/ContentNegotiationManagerFactoryBeanTests.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,12 @@ public void setDefaultContentType() throws Exception {
122122
ContentNegotiationManager manager = this.factoryBean.getObject();
123123

124124
assertEquals(Arrays.asList(MediaType.APPLICATION_JSON), manager.resolveMediaTypes(this.webRequest));
125+
126+
// SPR-10513
127+
128+
this.servletRequest.addHeader("Accept", MediaType.ALL_VALUE);
129+
130+
assertEquals(Arrays.asList(MediaType.APPLICATION_JSON), manager.resolveMediaTypes(this.webRequest));
125131
}
126132

127133
}

0 commit comments

Comments
 (0)