@RequestBody用於讀取Request請求的body數據,而後利用SpringMVC配置的HttpMessageConverter對數據進行轉換,最後把轉換後的數據綁定到被@RequestBody註解的參數上;java
根據request header中 Content-Type和被@RequestBody註解的參數不一樣,最多見的應用場景以下:json
該接口定義了五個方法,分別是讀取數據時的 canRead()、read() ,寫入數據時的canWrite()、 write()方法以及獲取支持類型的 getSupportedMediaTypes()後端
public interface HttpMessageConverter<T> { // Indicate whether the given class and media type can be read by this converter. boolean canRead(Class<?> clazz, MediaType mediaType); // Indicate whether the given class and media type can be written by this converter. boolean canWrite(Class<?> clazz, MediaType mediaType); // Return the list of MediaType objects supported by this converter. List<MediaType> getSupportedMediaTypes(); // Read an object of the given type from the given input message, and returns it. T read(Class<T> clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException; // Write an given object to the given output message. void write(T t, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException; }
使用 <mvc:annotation-driven />標籤配置時,默認配置了RequestMappingHandlerAdapter,併爲他配置了一下默認的HttpMessageConverter:
mvc
ByteArrayHttpMessageConverter: 負責讀取二進制格式的數據和寫出二進制格式的數據;
StringHttpMessageConverter: 負責讀取字符串格式的數據和寫出二進制格式的數據;
根據Request對象header部分的ContentType類型和被註解參數類型,逐一匹配合適的HttpMessageConverter來讀取數據;app
protected <T> Object readWithMessageConverters(HttpInputMessage inputMessage, MethodParameter parameter, Type targetType) throws IOException, HttpMediaTypeNotSupportedException, HttpMessageNotReadableException { ...... MediaType contentType; contentType = inputMessage.getHeaders().getContentType(); ....... for (HttpMessageConverter<?> converter : this.messageConverters) { Class<HttpMessageConverter<?>> converterType = (Class<HttpMessageConverter<?>>) converter.getClass(); if (converter instanceof GenericHttpMessageConverter) { GenericHttpMessageConverter<?> genericConverter = (GenericHttpMessageConverter<?>) converter; if (genericConverter.canRead(targetType, contextClass, contentType)) { if (logger.isDebugEnabled()) { logger.debug("Read [" + targetType + "] as \"" + contentType + "\" with [" + converter + "]"); } if (inputMessage.getBody() != null) { inputMessage = getAdvice().beforeBodyRead(inputMessage, parameter, targetType, converterType); body = genericConverter.read(targetType, contextClass, inputMessage); body = getAdvice().afterBodyRead(body, inputMessage, parameter, targetType, converterType); } else { body = getAdvice().handleEmptyBody(null, inputMessage, parameter, targetType, converterType); } break; } } else if (targetClass != null) { if (converter.canRead(targetClass, contentType)) { if (logger.isDebugEnabled()) { logger.debug("Read [" + targetType + "] as \"" + contentType + "\" with [" + converter + "]"); } if (inputMessage.getBody() != null) { inputMessage = getAdvice().beforeBodyRead(inputMessage, parameter, targetType, converterType); body = ((HttpMessageConverter<T>) converter).read(targetClass, inputMessage); body = getAdvice().afterBodyRead(body, inputMessage, parameter, targetType, converterType); } else { body = getAdvice().handleEmptyBody(null, inputMessage, parameter, targetType, converterType); } break; } } } ....... return body; }