|
17 | 17 | package org.springframework.web.servlet.mvc.method.annotation;
|
18 | 18 |
|
19 | 19 | import java.io.IOException;
|
| 20 | +import java.io.InputStream; |
| 21 | +import java.io.PushbackInputStream; |
20 | 22 | import java.lang.annotation.Annotation;
|
21 | 23 | import java.lang.reflect.Type;
|
22 | 24 | import java.util.List;
|
23 | 25 |
|
| 26 | +import javax.servlet.http.HttpServletRequest; |
| 27 | + |
24 | 28 | import org.springframework.core.Conventions;
|
25 | 29 | import org.springframework.core.MethodParameter;
|
26 | 30 | import org.springframework.core.annotation.AnnotationUtils;
|
27 | 31 | import org.springframework.http.HttpInputMessage;
|
28 | 32 | import org.springframework.http.converter.HttpMessageConverter;
|
29 | 33 | import org.springframework.http.converter.HttpMessageNotReadableException;
|
| 34 | +import org.springframework.http.server.ServletServerHttpRequest; |
30 | 35 | import org.springframework.validation.BindingResult;
|
31 | 36 | import org.springframework.validation.Errors;
|
32 | 37 | import org.springframework.web.HttpMediaTypeNotAcceptableException;
|
@@ -134,18 +139,45 @@ private boolean isBindExceptionRequired(WebDataBinder binder, MethodParameter pa
|
134 | 139 | }
|
135 | 140 |
|
136 | 141 | @Override
|
137 |
| - protected <T> Object readWithMessageConverters(HttpInputMessage inputMessage, |
138 |
| - MethodParameter methodParam, Type paramType) throws IOException, HttpMediaTypeNotSupportedException { |
| 142 | + protected <T> Object readWithMessageConverters(NativeWebRequest webRequest, |
| 143 | + MethodParameter methodParam, Type paramType) throws IOException, HttpMediaTypeNotSupportedException { |
139 | 144 |
|
140 |
| - if (inputMessage.getBody() != null) { |
141 |
| - return super.readWithMessageConverters(inputMessage, methodParam, paramType); |
142 |
| - } |
| 145 | + final HttpServletRequest servletRequest = webRequest.getNativeRequest(HttpServletRequest.class); |
| 146 | + HttpInputMessage inputMessage = new ServletServerHttpRequest(servletRequest); |
143 | 147 |
|
144 | 148 | RequestBody annot = methodParam.getParameterAnnotation(RequestBody.class);
|
145 | 149 | if (!annot.required()) {
|
146 |
| - return null; |
| 150 | + InputStream inputStream = inputMessage.getBody(); |
| 151 | + if (inputStream == null) { |
| 152 | + return null; |
| 153 | + } |
| 154 | + else if (inputStream.markSupported()) { |
| 155 | + inputStream.mark(1); |
| 156 | + if (inputStream.read() == -1) { |
| 157 | + return null; |
| 158 | + } |
| 159 | + inputStream.reset(); |
| 160 | + } |
| 161 | + else { |
| 162 | + final PushbackInputStream pushbackInputStream = new PushbackInputStream(inputStream); |
| 163 | + int b = pushbackInputStream.read(); |
| 164 | + if (b == -1) { |
| 165 | + return null; |
| 166 | + } |
| 167 | + else { |
| 168 | + pushbackInputStream.unread(b); |
| 169 | + } |
| 170 | + inputMessage = new ServletServerHttpRequest(servletRequest) { |
| 171 | + @Override |
| 172 | + public InputStream getBody() throws IOException { |
| 173 | + // Form POST should not get here |
| 174 | + return pushbackInputStream; |
| 175 | + } |
| 176 | + }; |
| 177 | + } |
147 | 178 | }
|
148 |
| - throw new HttpMessageNotReadableException("Required request body content is missing: " + methodParam.toString()); |
| 179 | + |
| 180 | + return super.readWithMessageConverters(inputMessage, methodParam, paramType); |
149 | 181 | }
|
150 | 182 |
|
151 | 183 | public void handleReturnValue(Object returnValue, MethodParameter returnType,
|
|
0 commit comments