Skip to content

Commit

Permalink
Improve default content type selection
Browse files Browse the repository at this point in the history
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
  • Loading branch information
rstoyanchev committed May 10, 2013
1 parent 78fcd28 commit aaded7e
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@
*/
public class ContentNegotiationManager implements ContentNegotiationStrategy, MediaTypeFileExtensionResolver {

private static final List<MediaType> MEDIA_TYPE_ALL = Arrays.asList(MediaType.ALL);

private final List<ContentNegotiationStrategy> contentNegotiationStrategies =
new ArrayList<ContentNegotiationStrategy>();

Expand Down Expand Up @@ -119,9 +121,10 @@ public void addFileExtensionResolvers(MediaTypeFileExtensionResolver... resolver
public List<MediaType> resolveMediaTypes(NativeWebRequest webRequest) throws HttpMediaTypeNotAcceptableException {
for (ContentNegotiationStrategy strategy : this.contentNegotiationStrategies) {
List<MediaType> mediaTypes = strategy.resolveMediaTypes(webRequest);
if (!mediaTypes.isEmpty()) {
return mediaTypes;
if (mediaTypes.isEmpty() || mediaTypes.equals(MEDIA_TYPE_ALL)) {
continue;
}
return mediaTypes;
}
return Collections.emptyList();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,12 @@ public void setDefaultContentType() throws Exception {
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));
}

}

0 comments on commit aaded7e

Please sign in to comment.