在SpringMVC中,可以使用@RequestBody和@ResponseBody兩個註解,分別完成請求報文到對象和對像到響應報文的轉換,底層這種靈活的消息轉換機制。使用系統默認配置的HttpMessageConverter進行解析,然後把相應的數據綁定到要返回的對像上。
HttpInputMessage
這個類是SpringMVC內部對一次Http請求報文的抽象,在HttpMessageConverter的read()方法中,有一個HttpInputMessage的形參,它正是SpringMVC的消息轉換器所作用的受體“請求消息”的內部抽象,消息轉換器從“請求消息”中按照規則提取消息,轉換為方法形參中聲明的對象。
package org.springframework.http;import java.io.IOException;import java.io.InputStream;public interface HttpInputMessage extends HttpMessage { InputStream getBody() throws IOException;}HttpOutputMessage
在HttpMessageConverter的write()方法中,有一個HttpOutputMessage的形參,它正是SpringMVC的消息轉換器所作用的受體“響應消息”的內部抽象,消息轉換器將“響應消息”按照一定的規則寫到響應報文中。
package org.springframework.http;import java.io.IOException;import java.io.OutputStream;public interface HttpOutputMessage extends HttpMessage { OutputStream getBody() throws IOException;}HttpMessageConverter
/* * Copyright 2002-2010 the original author or authors. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */package org.springframework.http.converter;import java.io.IOException;import java.util.List;import org.springframework.http.HttpInputMessage;import org.springframework.http.HttpOutputMessage;import org.springframework.http.MediaType;public interface HttpMessageConverter<T> { boolean canRead(Class<?> clazz, MediaType mediaType); boolean canWrite(Class<?> clazz, MediaType mediaType); List<MediaType> getSupportedMediaTypes(); T read(Class<? extends T> clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException; void write(T t, MediaType contentType, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException;}HttpMessageConverter 接口提供了5個方法:
其中read 和write 方法的參數分別有有HttpInputMessage 和HttpOutputMessage 對象,這兩個對象分別代表著一次Http通訊中的請求和響應部分,可以通過getBody 方法獲得對應的輸入流和輸出流。
當前Spring中已經默認提供了相當多的轉換器,分別有:
| 名稱 | 作用 | 讀支持MediaType | 寫支持MediaType |
|---|---|---|---|
| ByteArrayHttpMessageConverter | 數據與字節數組的相互轉換 | / | application/octet-stream |
| StringHttpMessageConverter | 數據與String類型的相互轉換 | text/* | text/plain |
| FormHttpMessageConverter | 表單與MultiValueMap<string, string=””>的相互轉換 | application/x-www-form-urlencoded | application/x-www-form-urlencoded |
| SourceHttpMessageConverter | 數據與javax.xml.transform.Source的相互轉換 | text/xml和application/xml | text/xml和application/xml |
| MarshallingHttpMessageConverter | 使用SpringMarshaller/Unmarshaller轉換XML數據 | text/xml和application/xml | text/xml和application/xml |
| MappingJackson2HttpMessageConverter | 使用Jackson的ObjectMapper轉換Json數據 | application/json | application/json |
| MappingJackson2XmlHttpMessageConverter | 使用Jackson的XmlMapper轉換XML數據 | application/xml | application/xml |
| BufferedImageHttpMessageConverter | 數據與java.awt.image.BufferedImage的相互轉換 | Java I/O API支持的所有類型 | Java I/O API支持的所有類型 |
HttpMessageConverter匹配過程:
@RequestBody註解時: 根據Request對象header部分的Content-Type類型,逐一匹配合適的HttpMessageConverter來讀取數據。
private Object readWithMessageConverters(MethodParameter methodParam, HttpInputMessage inputMessage, Class paramType) throws Exception { MediaType contentType = inputMessage.getHeaders().getContentType(); if (contentType == null) { StringBuilder builder = new StringBuilder(ClassUtils.getShortName(methodParam.getParameterType())); String paramName = methodParam.getParameterName(); if (paramName != null) { builder.append(' '); builder.append(paramName); } throw new HttpMediaTypeNotSupportedException("Cannot extract parameter (" + builder.toString() + "): no Content-Type found"); } List<MediaType> allSupportedMediaTypes = new ArrayList<MediaType>(); if (this.messageConverters != null) { for (HttpMessageConverter<?> messageConverter : this.messageConverters) { allSupportedMediaTypes.addAll(messageConverter.getSupportedMediaTypes()); if (messageConverter.canRead(paramType, contentType)) { if (logger.isDebugEnabled()) { logger.debug("Reading [" + paramType.getName() + "] as /"" + contentType + "/" using [" + messageConverter + "]"); } return messageConverter.read(paramType, inputMessage); } } } throw new HttpMediaTypeNotSupportedException(contentType, allSupportedMediaTypes); } @ResponseBody註解時:根據Request對象header部分的Accept屬性(逗號分隔),逐一按accept中的類型,去遍歷找到能處理的HttpMessageConverter。
private void writeWithMessageConverters(Object returnValue, HttpInputMessage inputMessage, HttpOutputMessage outputMessage) throws IOException, HttpMediaTypeNotAcceptableException { List<MediaType> acceptedMediaTypes = inputMessage.getHeaders().getAccept(); if (acceptedMediaTypes.isEmpty()) { acceptedMediaTypes = Collections.singletonList(MediaType.ALL); } MediaType.sortByQualityValue(acceptedMediaTypes); Class<?> returnValueType = returnValue.getClass(); List<MediaType> allSupportedMediaTypes = new ArrayList<MediaType>(); if (getMessageConverters() != null) { for (MediaType acceptedMediaType : acceptedMediaTypes) { for (HttpMessageConverter messageConverter : getMessageConverters()) { if (messageConverter.canWrite(returnValueType, acceptedMediaType)) { messageConverter.write(returnValue, acceptedMediaType, outputMessage); if (logger.isDebugEnabled()) { MediaType contentType = outputMessage.getHeaders().getContentType(); if (contentType == null) { contentType = acceptedMediaType; } logger.debug("Written [" + returnValue + "] as /"" + contentType + "/" using [" + messageConverter + "]"); } this.responseArgumentUsed = true; return; } } } for (HttpMessageConverter messageConverter : messageConverters) { allSupportedMediaTypes.addAll(messageConverter.getSupportedMediaTypes()); } } throw new HttpMediaTypeNotAcceptableException(allSupportedMediaTypes); }自定義一個JSON轉換器
class CustomJsonHttpMessageConverter implements HttpMessageConverter { //Jackson的Json映射類private ObjectMapper mapper = new ObjectMapper(); //該轉換器的支持類型:application/json private List supportedMediaTypes = Arrays.asList(MediaType.APPLICATION_JSON); /** * 判斷轉換器是否可以將輸入內容轉換成Java類型* @param clazz 需要轉換的Java類型* @param mediaType 該請求的MediaType * @return */ @Override public boolean canRead(Class clazz, MediaType mediaType) { if (mediaType == null) { return true; } for (MediaType supportedMediaType : getSupportedMediaTypes()) { if (supportedMediaType.includes(mediaType)) { return true; } } return false; } /** * 判斷轉換器是否可以將Java類型轉換成指定輸出內容* @param clazz 需要轉換的Java類型* @param mediaType 該請求的MediaType * @return */ @Override public boolean canWrite(Class clazz, MediaType mediaType) { if (mediaType == null || MediaType.ALL.equals(mediaType)) { return true; } for (MediaType supportedMediaType : getSupportedMediaTypes()) { if (supportedMediaType.includes(mediaType)) { return true; } } return false; } /** * 獲得該轉換器支持的MediaType * @return */ @Override public List getSupportedMediaTypes() { return supportedMediaTypes; } /** * 讀取請求內容,將其中的Json轉換成Java對象* @param clazz 需要轉換的Java類型* @param inputMessage 請求對象* @return * @throws IOException * @throws HttpMessageNotReadableException */ @Override public Object read(Class clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException { return mapper.readValue(inputMessage.getBody(), clazz); } /** * 將Java對象轉換成Json返回內容* @param o 需要轉換的對象* @param contentType 返回類型* @param outputMessage 回執對象* @throws IOException * @throws HttpMessageNotWritableException */ @Override public void write(Object o, MediaType contentType, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException { mapper.writeValue(outputMessage.getBody(), o); }}自定義MappingJackson2HttpMessage
從MappingJackson2HttpMessageConverter 的父類AbstractHttpMessageConverter 中的write 方法可以看出,該方法通過writeInternal 方法向返回結果的輸出流中寫入數據,所以只需要重寫該方法即可:
@Beanpublic MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter() { return new MappingJackson2HttpMessageConverter() { //重寫writeInternal方法,在返回內容前首先進行加密@Override protected void writeInternal(Object object, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException { //使用Jackson的ObjectMapper將Java對象轉換成Json String ObjectMapper mapper = new ObjectMapper(); String json = mapper.writeValueAsString(object); LOGGER.error(json); //加密String result = json + "加密了!"; LOGGER.error(result); //輸出outputMessage.getBody().write(result.getBytes()); } };}在這之後還需要將這個自定義的轉換器配置到Spring中,這里通過重寫WebMvcConfigurer 中的configureMessageConverters 方法添加自定義轉換器:
//添加自定義轉換器@Overridepublic void configureMessageConverters(List<httpmessageconverter<?>> converters) { converters.add(mappingJackson2HttpMessageConverter()); super.configureMessageConverters(converters);}以上就是本文的全部內容,希望對大家的學習有所幫助,也希望大家多多支持武林網。