Allow defining default content negotiation strategy
During the HTTP Content Negotiation phase, the ContentNegotiationManager uses configured ContentNegotiationStrategy(ies) to define the list of content types accepted by the client. When HTTP clients don't send Accept headers, nor use a configured file extension in the request, nor a request param, developers can define a default content type using the ContentNegotiationConfigurer.defaultContentType() method. This change adds a new overloaded defaultContentType method that takes a ContentNegotiationStrategy as an argument. This strategy will take the current request as an argument and return a default content type. Issue: SPR-12286
This commit is contained in:
@@ -1,5 +1,5 @@
|
||||
/*
|
||||
* Copyright 2002-2012 the original author or authors.
|
||||
* Copyright 2002-2014 the original author or authors.
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@@ -24,6 +24,7 @@ import org.junit.Test;
|
||||
import org.springframework.http.MediaType;
|
||||
import org.springframework.mock.web.test.MockHttpServletRequest;
|
||||
import org.springframework.web.accept.ContentNegotiationManager;
|
||||
import org.springframework.web.accept.FixedContentNegotiationStrategy;
|
||||
import org.springframework.web.context.request.NativeWebRequest;
|
||||
import org.springframework.web.context.request.ServletWebRequest;
|
||||
|
||||
@@ -110,4 +111,12 @@ public class ContentNegotiationConfigurerTests {
|
||||
|
||||
assertEquals(Arrays.asList(MediaType.APPLICATION_JSON), manager.resolveMediaTypes(this.webRequest));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void setDefaultContentTypeWithStrategy() throws Exception {
|
||||
this.configurer.defaultContentType(new FixedContentNegotiationStrategy(MediaType.APPLICATION_JSON));
|
||||
ContentNegotiationManager manager = this.configurer.getContentNegotiationManager();
|
||||
|
||||
assertEquals(Arrays.asList(MediaType.APPLICATION_JSON), manager.resolveMediaTypes(this.webRequest));
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user