SPR-6248 - Accept header should not be mandatory for properly handling response body.
This commit is contained in:
parent
a86baca9cb
commit
d54a975af4
|
|
@ -56,8 +56,8 @@ import org.springframework.core.ParameterNameDiscoverer;
|
||||||
import org.springframework.core.annotation.AnnotationUtils;
|
import org.springframework.core.annotation.AnnotationUtils;
|
||||||
import org.springframework.http.HttpInputMessage;
|
import org.springframework.http.HttpInputMessage;
|
||||||
import org.springframework.http.HttpOutputMessage;
|
import org.springframework.http.HttpOutputMessage;
|
||||||
import org.springframework.http.MediaType;
|
|
||||||
import org.springframework.http.HttpStatus;
|
import org.springframework.http.HttpStatus;
|
||||||
|
import org.springframework.http.MediaType;
|
||||||
import org.springframework.http.converter.ByteArrayHttpMessageConverter;
|
import org.springframework.http.converter.ByteArrayHttpMessageConverter;
|
||||||
import org.springframework.http.converter.FormHttpMessageConverter;
|
import org.springframework.http.converter.FormHttpMessageConverter;
|
||||||
import org.springframework.http.converter.HttpMessageConverter;
|
import org.springframework.http.converter.HttpMessageConverter;
|
||||||
|
|
@ -779,6 +779,9 @@ public class AnnotationMethodHandlerAdapter extends WebContentGenerator implemen
|
||||||
private void handleResponseBody(Object returnValue, ServletWebRequest webRequest) throws ServletException, IOException {
|
private void handleResponseBody(Object returnValue, ServletWebRequest webRequest) throws ServletException, IOException {
|
||||||
HttpInputMessage inputMessage = new ServletServerHttpRequest(webRequest.getRequest());
|
HttpInputMessage inputMessage = new ServletServerHttpRequest(webRequest.getRequest());
|
||||||
List<MediaType> acceptedMediaTypes = inputMessage.getHeaders().getAccept();
|
List<MediaType> acceptedMediaTypes = inputMessage.getHeaders().getAccept();
|
||||||
|
if (acceptedMediaTypes.isEmpty()) {
|
||||||
|
acceptedMediaTypes = Collections.singletonList(MediaType.ALL);
|
||||||
|
}
|
||||||
HttpOutputMessage outputMessage = new ServletServerHttpResponse(webRequest.getResponse());
|
HttpOutputMessage outputMessage = new ServletServerHttpResponse(webRequest.getResponse());
|
||||||
Class<?> returnValueType = returnValue.getClass();
|
Class<?> returnValueType = returnValue.getClass();
|
||||||
List<MediaType> allSupportedMediaTypes = new ArrayList<MediaType>();
|
List<MediaType> allSupportedMediaTypes = new ArrayList<MediaType>();
|
||||||
|
|
|
||||||
|
|
@ -5,7 +5,7 @@
|
||||||
* you may not use this file except in compliance with the License.
|
* you may not use this file except in compliance with the License.
|
||||||
* You may obtain a copy of the License at
|
* You may obtain a copy of the License at
|
||||||
*
|
*
|
||||||
* http://www.apache.org/licensesch/LICENSE-2.0
|
* http://www.apache.org/licenses/LICENSE-2.0
|
||||||
*
|
*
|
||||||
* Unless required by applicable law or agreed to in writing, software
|
* Unless required by applicable law or agreed to in writing, software
|
||||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||||
|
|
@ -970,6 +970,7 @@ public class ServletAnnotationControllerTests {
|
||||||
request.addHeader("Accept", "text/*");
|
request.addHeader("Accept", "text/*");
|
||||||
MockHttpServletResponse response = new MockHttpServletResponse();
|
MockHttpServletResponse response = new MockHttpServletResponse();
|
||||||
servlet.service(request, response);
|
servlet.service(request, response);
|
||||||
|
assertEquals(200, response.getStatus());
|
||||||
assertEquals(requestBody, response.getContentAsString());
|
assertEquals(requestBody, response.getContentAsString());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -1016,6 +1017,20 @@ public class ServletAnnotationControllerTests {
|
||||||
assertNotNull("No Accept response header set", response.getHeader("Accept"));
|
assertNotNull("No Accept response header set", response.getHeader("Accept"));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void responseBodyNoAcceptHeader() throws ServletException, IOException {
|
||||||
|
initServlet(RequestBodyController.class);
|
||||||
|
|
||||||
|
MockHttpServletRequest request = new MockHttpServletRequest("PUT", "/something");
|
||||||
|
String requestBody = "Hello World";
|
||||||
|
request.setContent(requestBody.getBytes("UTF-8"));
|
||||||
|
request.addHeader("Content-Type", "text/plain; charset=utf-8");
|
||||||
|
MockHttpServletResponse response = new MockHttpServletResponse();
|
||||||
|
servlet.service(request, response);
|
||||||
|
assertEquals(200, response.getStatus());
|
||||||
|
assertEquals(requestBody, response.getContentAsString());
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void badRequestRequestBody() throws ServletException, IOException {
|
public void badRequestRequestBody() throws ServletException, IOException {
|
||||||
@SuppressWarnings("serial") DispatcherServlet servlet = new DispatcherServlet() {
|
@SuppressWarnings("serial") DispatcherServlet servlet = new DispatcherServlet() {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue