|
20 | 20 | import java.io.OutputStream;
|
21 | 21 | import java.io.Reader;
|
22 | 22 | import java.io.Writer;
|
| 23 | +import java.io.IOException; |
23 | 24 | import java.lang.reflect.Method;
|
24 | 25 | import java.security.Principal;
|
25 | 26 | import java.util.ArrayList;
|
|
32 | 33 | import java.util.Locale;
|
33 | 34 | import java.util.Map;
|
34 | 35 | import java.util.Set;
|
| 36 | +import java.util.Iterator; |
35 | 37 | import java.util.concurrent.ConcurrentHashMap;
|
36 | 38 | import javax.servlet.ServletException;
|
37 | 39 | import javax.servlet.ServletRequest;
|
|
51 | 53 | import org.springframework.core.annotation.AnnotationUtils;
|
52 | 54 | import org.springframework.http.HttpInputMessage;
|
53 | 55 | import org.springframework.http.HttpOutputMessage;
|
| 56 | +import org.springframework.http.MediaType; |
54 | 57 | import org.springframework.http.converter.BufferedImageHttpMessageConverter;
|
55 | 58 | import org.springframework.http.converter.ByteArrayHttpMessageConverter;
|
56 | 59 | import org.springframework.http.converter.FormHttpMessageConverter;
|
|
71 | 74 | import org.springframework.validation.support.BindingAwareModelMap;
|
72 | 75 | import org.springframework.web.HttpRequestMethodNotSupportedException;
|
73 | 76 | import org.springframework.web.HttpSessionRequiredException;
|
| 77 | +import org.springframework.web.HttpMediaTypeNotAcceptableException; |
74 | 78 | import org.springframework.web.bind.MissingServletRequestParameterException;
|
75 | 79 | import org.springframework.web.bind.ServletRequestDataBinder;
|
76 | 80 | import org.springframework.web.bind.WebDataBinder;
|
@@ -726,15 +730,7 @@ public ModelAndView getModelAndView(Method handlerMethod,
|
726 | 730 | }
|
727 | 731 |
|
728 | 732 | if (returnValue != null && handlerMethod.isAnnotationPresent(ResponseBody.class)) {
|
729 |
| - Class returnValueType = returnValue.getClass(); |
730 |
| - HttpOutputMessage outputMessage = new ServletServerHttpResponse(webRequest.getResponse()); |
731 |
| - for (HttpMessageConverter messageConverter : messageConverters) { |
732 |
| - if (messageConverter.supports(returnValueType)) { |
733 |
| - messageConverter.write(returnValue, outputMessage); |
734 |
| - responseArgumentUsed = true; |
735 |
| - return null; |
736 |
| - } |
737 |
| - } |
| 733 | + handleRequestBody(returnValue, webRequest); |
738 | 734 | }
|
739 | 735 |
|
740 | 736 | if (returnValue instanceof ModelAndView) {
|
@@ -777,6 +773,31 @@ else if (!BeanUtils.isSimpleProperty(returnValue.getClass())) {
|
777 | 773 | throw new IllegalArgumentException("Invalid handler method return value: " + returnValue);
|
778 | 774 | }
|
779 | 775 | }
|
| 776 | + |
| 777 | + @SuppressWarnings("unchecked") |
| 778 | + private void handleRequestBody(Object returnValue, ServletWebRequest webRequest) throws ServletException, IOException { |
| 779 | + HttpInputMessage inputMessage = new ServletServerHttpRequest(webRequest.getRequest()); |
| 780 | + List<MediaType> acceptedMediaTypes = inputMessage.getHeaders().getAccept(); |
| 781 | + HttpOutputMessage outputMessage = new ServletServerHttpResponse(webRequest.getResponse()); |
| 782 | + Class<?> returnValueType = returnValue.getClass(); |
| 783 | + List<MediaType> allSupportedMediaTypes = new ArrayList<MediaType>(); |
| 784 | + for (HttpMessageConverter messageConverter : messageConverters) { |
| 785 | + allSupportedMediaTypes.addAll(messageConverter.getSupportedMediaTypes()); |
| 786 | + if (messageConverter.supports(returnValueType)) { |
| 787 | + for (Object o : messageConverter.getSupportedMediaTypes()) { |
| 788 | + MediaType supportedMediaType = (MediaType) o; |
| 789 | + for (MediaType acceptedMediaType : acceptedMediaTypes) { |
| 790 | + if (supportedMediaType.includes(acceptedMediaType)) { |
| 791 | + messageConverter.write(returnValue, outputMessage); |
| 792 | + responseArgumentUsed = true; |
| 793 | + return; |
| 794 | + } |
| 795 | + } |
| 796 | + } |
| 797 | + } |
| 798 | + } |
| 799 | + throw new HttpMediaTypeNotAcceptableException(allSupportedMediaTypes); |
| 800 | + } |
780 | 801 | }
|
781 | 802 |
|
782 | 803 | static class RequestMappingInfo {
|
|
0 commit comments