From eddc5b8898851956e61b1ffaf0c6701f1b00c95c Mon Sep 17 00:00:00 2001 From: Arjen Poutsma Date: Thu, 4 Mar 2010 10:51:27 +0000 Subject: [PATCH] SPR-6935 - @ResponseBody with method that return void throws "Could not resolve view exception". --- .../AnnotationMethodHandlerAdapter.java | 7 +++++-- .../ServletAnnotationControllerTests.java | 21 +++++++++++++++++++ 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/mvc/annotation/AnnotationMethodHandlerAdapter.java b/org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/mvc/annotation/AnnotationMethodHandlerAdapter.java index 0c5a823cb3..c6b19db415 100644 --- a/org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/mvc/annotation/AnnotationMethodHandlerAdapter.java +++ b/org.springframework.web.servlet/src/main/java/org/springframework/web/servlet/mvc/annotation/AnnotationMethodHandlerAdapter.java @@ -447,7 +447,7 @@ public class AnnotationMethodHandlerAdapter extends WebContentGenerator * @return the ServletRequestDataBinder instance to use * @throws Exception in case of invalid state or arguments * @see ServletRequestDataBinder#bind(javax.servlet.ServletRequest) - * @see ServletRequestDataBinder#convertIfNecessary(Object, Class, MethodParameter) + * @see ServletRequestDataBinder#convertIfNecessary(Object, Class, org.springframework.core.MethodParameter) */ protected ServletRequestDataBinder createBinder( HttpServletRequest request, Object target, String objectName) throws Exception { @@ -812,7 +812,7 @@ public class AnnotationMethodHandlerAdapter extends WebContentGenerator } } - if (returnValue != null && AnnotationUtils.findAnnotation(handlerMethod, ResponseBody.class) != null) { + if (AnnotationUtils.findAnnotation(handlerMethod, ResponseBody.class) != null) { handleResponseBody(returnValue, webRequest); return null; } @@ -861,6 +861,9 @@ public class AnnotationMethodHandlerAdapter extends WebContentGenerator @SuppressWarnings("unchecked") private void handleResponseBody(Object returnValue, ServletWebRequest webRequest) throws ServletException, IOException { + if (returnValue == null) { + return; + } HttpInputMessage inputMessage = new ServletServerHttpRequest(webRequest.getRequest()); List acceptedMediaTypes = inputMessage.getHeaders().getAccept(); diff --git a/org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/mvc/annotation/ServletAnnotationControllerTests.java b/org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/mvc/annotation/ServletAnnotationControllerTests.java index f994b54e92..6dad5c6556 100644 --- a/org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/mvc/annotation/ServletAnnotationControllerTests.java +++ b/org.springframework.web.servlet/src/test/java/org/springframework/web/servlet/mvc/annotation/ServletAnnotationControllerTests.java @@ -1187,6 +1187,18 @@ public class ServletAnnotationControllerTests { assertEquals("Invalid response status code", "application/json", response.getHeader("Content-Type")); } + @Test + public void responseBodyVoid() throws ServletException, IOException { + initServlet(ResponseBodyVoidController.class); + + MockHttpServletRequest request = new MockHttpServletRequest("GET", "/something"); + request.addHeader("Accept", "text/*, */*"); + MockHttpServletResponse response = new MockHttpServletResponse(); + servlet.service(request, response); + assertEquals(200, response.getStatus()); + } + + @Test public void headers() throws ServletException, IOException { initServlet(HeadersController.class); @@ -2156,6 +2168,15 @@ public class ServletAnnotationControllerTests { } } + @Controller + public static class ResponseBodyVoidController { + + @RequestMapping("/something") + @ResponseBody + public void handle() throws IOException { + } + } + public static class NotReadableMessageConverter implements HttpMessageConverter { public boolean canRead(Class clazz, MediaType mediaType) {