SPR-7018 - Support for HttpEntity<?> in @MVC

This commit is contained in:
Arjen Poutsma
2010-03-22 10:23:39 +00:00
parent 69cbadf7e9
commit b07d02f1bf
3 changed files with 121 additions and 14 deletions

View File

@@ -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);
}
}
}