Browse Source

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
pull/283/merge
Rossen Stoyanchev 13 years ago
parent
commit
4d005b6987
  1. 7
      spring-web/src/main/java/org/springframework/web/accept/ContentNegotiationManager.java
  2. 6
      spring-web/src/test/java/org/springframework/web/accept/ContentNegotiationManagerFactoryBeanTests.java

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

@ -50,6 +50,8 @@ import org.springframework.web.context.request.NativeWebRequest;
*/ */
public class ContentNegotiationManager implements ContentNegotiationStrategy, MediaTypeFileExtensionResolver { public class ContentNegotiationManager implements ContentNegotiationStrategy, MediaTypeFileExtensionResolver {
private static final List<MediaType> MEDIA_TYPE_ALL = Arrays.asList(MediaType.ALL);
private final List<ContentNegotiationStrategy> contentNegotiationStrategies = private final List<ContentNegotiationStrategy> contentNegotiationStrategies =
new ArrayList<ContentNegotiationStrategy>(); new ArrayList<ContentNegotiationStrategy>();
@ -119,9 +121,10 @@ public class ContentNegotiationManager implements ContentNegotiationStrategy, Me
public List<MediaType> resolveMediaTypes(NativeWebRequest webRequest) throws HttpMediaTypeNotAcceptableException { public List<MediaType> resolveMediaTypes(NativeWebRequest webRequest) throws HttpMediaTypeNotAcceptableException {
for (ContentNegotiationStrategy strategy : this.contentNegotiationStrategies) { for (ContentNegotiationStrategy strategy : this.contentNegotiationStrategies) {
List<MediaType> mediaTypes = strategy.resolveMediaTypes(webRequest); List<MediaType> mediaTypes = strategy.resolveMediaTypes(webRequest);
if (!mediaTypes.isEmpty()) { if (mediaTypes.isEmpty() || mediaTypes.equals(MEDIA_TYPE_ALL)) {
return mediaTypes; continue;
} }
return mediaTypes;
} }
return Collections.emptyList(); return Collections.emptyList();
} }

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

@ -122,6 +122,12 @@ public class ContentNegotiationManagerFactoryBeanTests {
ContentNegotiationManager manager = this.factoryBean.getObject(); ContentNegotiationManager manager = this.factoryBean.getObject();
assertEquals(Arrays.asList(MediaType.APPLICATION_JSON), manager.resolveMediaTypes(this.webRequest)); assertEquals(Arrays.asList(MediaType.APPLICATION_JSON), manager.resolveMediaTypes(this.webRequest));
// SPR-10513
this.servletRequest.addHeader("Accept", MediaType.ALL_VALUE);
assertEquals(Arrays.asList(MediaType.APPLICATION_JSON), manager.resolveMediaTypes(this.webRequest));
} }
} }

Loading…
Cancel
Save