SPR-7018 - Support for HttpEntity<?> in @MVC
This commit is contained in:
@@ -55,6 +55,8 @@ import org.springframework.core.LocalVariableTableParameterNameDiscoverer;
|
||||
import org.springframework.core.Ordered;
|
||||
import org.springframework.core.ParameterNameDiscoverer;
|
||||
import org.springframework.core.annotation.AnnotationUtils;
|
||||
import org.springframework.http.HttpEntity;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpInputMessage;
|
||||
import org.springframework.http.HttpOutputMessage;
|
||||
import org.springframework.http.HttpStatus;
|
||||
@@ -797,6 +799,10 @@ public class AnnotationMethodHandlerAdapter extends WebContentGenerator
|
||||
handleResponseBody(returnValue, webRequest);
|
||||
return null;
|
||||
}
|
||||
if (returnValue instanceof HttpEntity) {
|
||||
handleHttpEntityResponse((HttpEntity<?>) returnValue, webRequest);
|
||||
return null;
|
||||
}
|
||||
|
||||
if (returnValue instanceof ModelAndView) {
|
||||
ModelAndView mav = (ModelAndView) returnValue;
|
||||
@@ -839,7 +845,6 @@ public class AnnotationMethodHandlerAdapter extends WebContentGenerator
|
||||
}
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private void handleResponseBody(Object returnValue, ServletWebRequest webRequest)
|
||||
throws ServletException, IOException {
|
||||
if (returnValue == null) {
|
||||
@@ -847,12 +852,35 @@ public class AnnotationMethodHandlerAdapter extends WebContentGenerator
|
||||
}
|
||||
|
||||
HttpInputMessage inputMessage = new ServletServerHttpRequest(webRequest.getRequest());
|
||||
HttpOutputMessage outputMessage = new ServletServerHttpResponse(webRequest.getResponse());
|
||||
|
||||
writeWithMessageConverters(returnValue, inputMessage, outputMessage);
|
||||
}
|
||||
|
||||
private void handleHttpEntityResponse(HttpEntity<?> responseEntity, ServletWebRequest webRequest)
|
||||
throws ServletException, IOException {
|
||||
if (responseEntity == null) {
|
||||
return;
|
||||
}
|
||||
HttpInputMessage inputMessage = new ServletServerHttpRequest(webRequest.getRequest());
|
||||
HttpOutputMessage outputMessage = new ServletServerHttpResponse(webRequest.getResponse());
|
||||
|
||||
HttpHeaders entityHeaders = responseEntity.getHeaders();
|
||||
if (!entityHeaders.isEmpty()) {
|
||||
outputMessage.getHeaders().putAll(entityHeaders);
|
||||
}
|
||||
writeWithMessageConverters(responseEntity.getBody(), inputMessage, outputMessage);
|
||||
}
|
||||
|
||||
@SuppressWarnings("unchecked")
|
||||
private void writeWithMessageConverters(Object returnValue,
|
||||
HttpInputMessage inputMessage, HttpOutputMessage outputMessage)
|
||||
throws IOException, HttpMediaTypeNotAcceptableException {
|
||||
List<MediaType> acceptedMediaTypes = inputMessage.getHeaders().getAccept();
|
||||
if (acceptedMediaTypes.isEmpty()) {
|
||||
acceptedMediaTypes = Collections.singletonList(MediaType.ALL);
|
||||
}
|
||||
MediaType.sortBySpecificity(acceptedMediaTypes);
|
||||
HttpOutputMessage outputMessage = new ServletServerHttpResponse(webRequest.getResponse());
|
||||
Class<?> returnValueType = returnValue.getClass();
|
||||
List<MediaType> allSupportedMediaTypes = new ArrayList<MediaType>();
|
||||
if (getMessageConverters() != null) {
|
||||
@@ -879,6 +907,7 @@ public class AnnotationMethodHandlerAdapter extends WebContentGenerator
|
||||
}
|
||||
throw new HttpMediaTypeNotAcceptableException(allSupportedMediaTypes);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
@@ -18,6 +18,7 @@ package org.springframework.web.servlet.mvc.annotation;
|
||||
|
||||
import java.io.IOException;
|
||||
import java.io.Serializable;
|
||||
import java.io.UnsupportedEncodingException;
|
||||
import java.io.Writer;
|
||||
import java.lang.annotation.ElementType;
|
||||
import java.lang.annotation.Retention;
|
||||
@@ -64,6 +65,7 @@ import org.springframework.beans.factory.support.RootBeanDefinition;
|
||||
import org.springframework.beans.propertyeditors.CustomDateEditor;
|
||||
import org.springframework.context.annotation.AnnotationConfigUtils;
|
||||
import org.springframework.core.MethodParameter;
|
||||
import org.springframework.http.HttpEntity;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpInputMessage;
|
||||
import org.springframework.http.HttpOutputMessage;
|
||||
@@ -1155,6 +1157,24 @@ public class ServletAnnotationControllerTests {
|
||||
assertEquals("Invalid response status code", HttpServletResponse.SC_BAD_REQUEST, response.getStatus());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void httpEntity() throws ServletException, IOException {
|
||||
initServlet(HttpEntityController.class);
|
||||
|
||||
MockHttpServletRequest request = new MockHttpServletRequest("PUT", "/handle");
|
||||
String requestBody = "Hello World";
|
||||
request.setContent(requestBody.getBytes("UTF-8"));
|
||||
request.addHeader("Content-Type", "text/plain; charset=utf-8");
|
||||
request.addHeader("Accept", "text/*, */*");
|
||||
request.addHeader("MyRequestHeader", "MyValue");
|
||||
MockHttpServletResponse response = new MockHttpServletResponse();
|
||||
servlet.service(request, response);
|
||||
assertEquals(200, response.getStatus());
|
||||
assertEquals(requestBody, response.getContentAsString());
|
||||
assertEquals("MyValue", response.getHeader("MyResponseHeader"));
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
* See SPR-6877
|
||||
*/
|
||||
@@ -2502,5 +2522,21 @@ public class ServletAnnotationControllerTests {
|
||||
}
|
||||
}
|
||||
|
||||
@Controller
|
||||
public static class HttpEntityController {
|
||||
|
||||
@RequestMapping("/handle")
|
||||
public HttpEntity<String> handle(HttpEntity<byte[]> requestEntity) throws UnsupportedEncodingException {
|
||||
assertNotNull(requestEntity);
|
||||
assertEquals("MyValue", requestEntity.getHeaders().getFirst("MyRequestHeader"));
|
||||
String requestBody = new String(requestEntity.getBody(), "UTF-8");
|
||||
assertEquals("Hello World", requestBody);
|
||||
|
||||
HttpHeaders responseHeaders = new HttpHeaders();
|
||||
responseHeaders.set("MyResponseHeader", "MyValue");
|
||||
return new HttpEntity<String>(requestBody, responseHeaders);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user