From 53100357000e46c388919baf6e1e2c0e9954454f Mon Sep 17 00:00:00 2001 From: Arjen Poutsma Date: Wed, 14 Oct 2009 10:46:29 +0000 Subject: [PATCH] SPR-6214 - Using @ResponseBody throws HttpMediaTypeNotAcceptableException when it supports writing an acceptable MediaType --- .../annotation/AnnotationMethodHandlerAdapter.java | 2 +- .../ServletAnnotationControllerTests.java | 14 ++++++++++++++ 2 files changed, 15 insertions(+), 1 deletion(-) 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 4a8353b41c..f4fcedb344 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 @@ -789,7 +789,7 @@ public class AnnotationMethodHandlerAdapter extends WebContentGenerator implemen for (Object o : messageConverter.getSupportedMediaTypes()) { MediaType supportedMediaType = (MediaType) o; for (MediaType acceptedMediaType : acceptedMediaTypes) { - if (supportedMediaType.includes(acceptedMediaType)) { + if (acceptedMediaType.includes(supportedMediaType)) { messageConverter.write(returnValue, outputMessage); this.responseArgumentUsed = true; return; 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 75b9e67bad..6fbf82713b 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 @@ -987,6 +987,20 @@ public class ServletAnnotationControllerTests { assertEquals(406, response.getStatus()); } + @Test + public void responseBodyWildCardMediaType() 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"); + request.addHeader("Accept", "*/*"); + MockHttpServletResponse response = new MockHttpServletResponse(); + servlet.service(request, response); + assertEquals(requestBody, response.getContentAsString()); + } + @Test public void unsupportedRequestBody() throws ServletException, IOException { initServlet(RequestBodyController.class);