SpringMVC之HttpMessageConverter

來源:互聯網
上載者:User
這一篇文章主要介紹下HttpMessageConverter整個註冊過程包含自訂的HttpMessageConverter,然後對一些HttpMessageConverter進行具體介紹。 

HttpMessageConverter介面介紹:  
Java代碼   public interface HttpMessageConverter<T> {          /**       * Indicates whether the given class can be read by this converter.       * @param clazz the class to test for readability       * @param mediaType the media type to read, can be {@code null} if not specified.       * Typically the value of a {@code Content-Type} header.       * @return {@code true} if readable; {@code false} otherwise       */       boolean canRead(Class<?> clazz, MediaType mediaType);          /**       * Indicates whether the given class can be written by this converter.       * @param clazz the class to test for writability       * @param mediaType the media type to write, can be {@code null} if not specified.       * Typically the value of an {@code Accept} header.       * @return {@code true} if writable; {@code false} otherwise       */       boolean canWrite(Class<?> clazz, MediaType mediaType);          /**       * Return the list of {@link MediaType} objects supported by this converter.       * @return the list of supported media types       */       List<MediaType> getSupportedMediaTypes();          /**       * Read an object of the given type form the given input message, and returns it.       * @param clazz the type of object to return. This type must have previously been passed to the       * {@link #canRead canRead} method of this interface, which must have returned {@code true}.       * @param inputMessage the HTTP input message to read from       * @return the converted object       * @throws IOException in case of I/O errors       * @throws HttpMessageNotReadableException in case of conversion errors       */       T read(Class<? extends T> clazz, HttpInputMessage inputMessage)               throws IOException, HttpMessageNotReadableException;          /**       * Write an given object to the given output message.       * @param t the object to write to the output message. The type of this object must have previously been       * passed to the {@link #canWrite canWrite} method of this interface, which must have returned {@code true}.       * @param contentType the content type to use when writing. May be {@code null} to indicate that the       * default content type of the converter must be used. If not {@code null}, this media type must have       * previously been passed to the {@link #canWrite canWrite} method of this interface, which must have       * returned {@code true}.       * @param outputMessage the message to write to       * @throws IOException in case of I/O errors       * @throws HttpMessageNotWritableException in case of conversion errors       */       void write(T t, MediaType contentType, HttpOutputMessage outputMessage)               throws IOException, HttpMessageNotWritableException;      }  
從HttpInputMessage中讀取資料: T read(Class<? extends T> clazz, HttpInputMessage inputMessage),前提clazz能夠通過canRead(clazz,mediaType)測試。 
向HttpOutputMessage中寫入資料:void write(T t, MediaType contentType, HttpOutputMessage outputMessage),前提能夠通過canWrite方法。 

簡單舉例: 
如StringHttpMessageConverter,read方法就是根據編碼類別型將HttpInputMessage中的資料變為字串。write方法就是根據編碼類別型將字串資料寫入HttpOutputMessage中。 

HttpMessageConverter的使用情境: 
它主要是用來轉換request的內容到一定的格式,轉換輸出的內容的到response。 
看下自訂的使用方式:  
Java代碼   <mvc:annotation-driven>           <mvc:message-converters register-defaults="true">               <bean class="org.springframework.http.converter.StringHttpMessageConverter">                   <constructor-arg value="UTF-8"/>               </bean>           </mvc:message-converters>       </mvc:annotation-driven>  
首先還是在對mvc:annotation-driven解析的AnnotationDrivenBeanDefinitionParser中,有這麼一個方法:  
Java代碼   ManagedList<?> messageConverters = getMessageConverters(element, source, parserContext);  
擷取所有的HttpMessageConverter,最終設定到RequestMappingHandlerAdapter的private List<HttpMessageConverter<?>> messageConverters屬性上。看下具體的擷取過程:  
Java代碼   private ManagedList<?> getMessageConverters(E

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.