From 4d005b69872eaf412561d237c21eeff67ddd2882 Mon Sep 17 00:00:00 2001 From: Rossen Stoyanchev Date: Fri, 10 May 2013 11:10:15 -0400 Subject: [PATCH] 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 --- .../web/accept/ContentNegotiationManager.java | 7 +++++-- .../accept/ContentNegotiationManagerFactoryBeanTests.java | 6 ++++++ 2 files changed, 11 insertions(+), 2 deletions(-) diff --git a/spring-web/src/main/java/org/springframework/web/accept/ContentNegotiationManager.java b/spring-web/src/main/java/org/springframework/web/accept/ContentNegotiationManager.java index 0024eef27db..2886fe487fd 100644 --- a/spring-web/src/main/java/org/springframework/web/accept/ContentNegotiationManager.java +++ b/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 { + private static final List MEDIA_TYPE_ALL = Arrays.asList(MediaType.ALL); + private final List contentNegotiationStrategies = new ArrayList(); @@ -119,9 +121,10 @@ public class ContentNegotiationManager implements ContentNegotiationStrategy, Me public List resolveMediaTypes(NativeWebRequest webRequest) throws HttpMediaTypeNotAcceptableException { for (ContentNegotiationStrategy strategy : this.contentNegotiationStrategies) { List mediaTypes = strategy.resolveMediaTypes(webRequest); - if (!mediaTypes.isEmpty()) { - return mediaTypes; + if (mediaTypes.isEmpty() || mediaTypes.equals(MEDIA_TYPE_ALL)) { + continue; } + return mediaTypes; } return Collections.emptyList(); } diff --git a/spring-web/src/test/java/org/springframework/web/accept/ContentNegotiationManagerFactoryBeanTests.java b/spring-web/src/test/java/org/springframework/web/accept/ContentNegotiationManagerFactoryBeanTests.java index d086093402c..f7924b840ed 100644 --- a/spring-web/src/test/java/org/springframework/web/accept/ContentNegotiationManagerFactoryBeanTests.java +++ b/spring-web/src/test/java/org/springframework/web/accept/ContentNegotiationManagerFactoryBeanTests.java @@ -122,6 +122,12 @@ public class ContentNegotiationManagerFactoryBeanTests { ContentNegotiationManager manager = this.factoryBean.getObject(); 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)); } }