Spring源码学习--HttpMessageConverter接口

文章参考:

1 http://blog.csdn.net/silencecarrot/article/details/52493709

2 http://blog.csdn.net/qq924862077/article/details/55222947

前言:

HttpMessageConverter接口是Spring3.0之后新增的一个接口,它负责将请求信息转换为一个对象(类型为T),并将对象(类型为T)绑定到请求方法的参数中或输出为响应信息。

一 丶HttpMessageConverter接口源码

在SpringMVC中,DispatcherServlet默认装配了RequestMappingHandlerAdapter作为HandlerAdapter组件的实现类,即HttpMessageConverter由RequestMappingHandlerAdapter使用,将请求信息转换为对象,或将对象转换为响应信息。其中RequestMappingHandlerAdapter是如何使用HttpMessageConverter的实现类的后续会有新文章说明。

下面是HttpMessageConverter接口中定义了方法:

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;

/** * Strategy interface that specifies a converter that can convert from and to HTTP requests and responses. * * @author Arjen Poutsma * @author Juergen Hoeller * @since 3.0 */
public interface HttpMessageConverter<T> {

    /** * 该方法指定转换器可以读取的对象类型,即转换器可将请求信息转换为clazz类型的对象, * 同时指定支持的MIME类型(text/html、application/json等)。 */
    boolean canRead(Class<?> clazz, MediaType mediaType);

    /** * 该方法指定转换器可以讲clazz类型的对象写到响应流当中,响应流支持的媒体类型在mediaType中定义 * */
    boolean canWrite(Class<?> clazz, MediaType mediaType);

    /** * 该方法返回当前转换器支持的媒体类型 */
    List<MediaType> getSupportedMediaTypes();

    /** * 该方法将请求信息转换为T类型的对象 */
    T read(Class<? extends T> clazz, HttpInputMessage inputMessage)
            throws IOException, HttpMessageNotReadableException;

    /** * 该方法将T类型的对象写到响应流当中,同事指定响应的媒体类型为contentType * */
    void write(T t, MediaType contentType, HttpOutputMessage outputMessage)
            throws IOException, HttpMessageNotWritableException;

}

其中,上传出现的HttpInputMessage 和HttpOutputMessage 分别是springMVC在读写操作方面提供的读写操作接口来完成具体的读写操作。下面给出HttpInputMessage 和 HttpOutputMessage接口的定义:

//HttpInputMessage提供的接口就是将body中的数据转为输入流

public interface HttpInputMessage extends HttpMessage {  

    /** * 获取body中的数据作为输入流 */  
    InputStream getBody() throws IOException;  

}  
//HttpOutputMessage提供的接口就是将body中的数据转为输出流

public interface HttpOutputMessage extends HttpMessage {  

    /** * 将数据转为输出流 */   
    OutputStream getBody() throws IOException;  

}  

具体的两者实现可以参照源码继续了解。

二丶HttpMessageConverter接口实现类和实际运用

Spring为HttpMessageConverter提供了多个实现类,这些实现类组成了一个功能强大、用途广泛的信息转换家族,主要包括一下实现类:

《Spring源码学习--HttpMessageConverter接口》

其中RequestMappingHandlerAdapter已经默认装配了一下的HttpMessageConverter实现类:

(1)丶StringHttpMessageConverter
(2)丶ByteArrayHttpMessageConverter
(3)丶SourceHttpMessageConverter
(4)丶XmlAwareFormHttpMessageConverter(已经被废弃,不建议使用)

如果需要装配其他类型的HttpMessageConverter实现类,则可以按照如下的java方式装配:

package com.hikvision.center.config.springmvc;

import java.util.ArrayList;
import java.util.List;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.converter.BufferedImageHttpMessageConverter;
import org.springframework.http.converter.ByteArrayHttpMessageConverter;
import org.springframework.http.converter.FormHttpMessageConverter;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.ResourceHttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.web.servlet.HandlerAdapter;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter;

import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;

/** * 数据转换器 */
@Configuration
public class HikMessageConverter {


    @Bean
    public HandlerAdapter handlerAdapter(){
        List<HttpMessageConverter<?>> messageConverters = new ArrayList<HttpMessageConverter<?>>();
        FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new FastJsonHttpMessageConverter();
        List<MediaType> supportedMediaTypes = new ArrayList<MediaType>();
        supportedMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
        supportedMediaTypes.add(MediaType.APPLICATION_FORM_URLENCODED);
        supportedMediaTypes.add(MediaType.TEXT_HTML);
        fastJsonHttpMessageConverter.setSupportedMediaTypes(supportedMediaTypes );
        fastJsonHttpMessageConverter.setFeatures(SerializerFeature.WriteMapNullValue,SerializerFeature.WriteNullStringAsEmpty,SerializerFeature.DisableCircularReferenceDetect);
        messageConverters.add(fastJsonHttpMessageConverter);

        FormHttpMessageConverter formHttpMessageConverter = new FormHttpMessageConverter();
        List<MediaType> formsupportedMediaTypes  = new ArrayList<MediaType>();
        formsupportedMediaTypes.add(MediaType.TEXT_PLAIN);
        formHttpMessageConverter.setSupportedMediaTypes(formsupportedMediaTypes);
        messageConverters.add(formHttpMessageConverter);

        BufferedImageHttpMessageConverter bufferedImageHttpMessageConverter = new BufferedImageHttpMessageConverter();
        messageConverters.add(bufferedImageHttpMessageConverter);

        ByteArrayHttpMessageConverter byteArrayHttpMessageConverter = new ByteArrayHttpMessageConverter();
        messageConverters.add(byteArrayHttpMessageConverter);

        StringHttpMessageConverter stringHttpMessageConverter = new StringHttpMessageConverter();
        List<MediaType> stringSupportedMediaTypes  = new ArrayList<MediaType>();
        stringSupportedMediaTypes.add(MediaType.TEXT_PLAIN);
        stringSupportedMediaTypes.add(MediaType.parseMediaType("UTF-8"));
        stringHttpMessageConverter.setSupportedMediaTypes(stringSupportedMediaTypes);
        messageConverters.add(stringHttpMessageConverter);

        ResourceHttpMessageConverter resourceHttpMessageConverter = new ResourceHttpMessageConverter();
        List<MediaType> resourceSupportMediaTypes = new ArrayList<MediaType>();
        resourceSupportMediaTypes.add(MediaType.TEXT_HTML);
        messageConverters.add(resourceHttpMessageConverter);

        RequestMappingHandlerAdapter requestMappingHandlerAdapter = new RequestMappingHandlerAdapter();
        requestMappingHandlerAdapter.setMessageConverters(messageConverters);
        return requestMappingHandlerAdapter;
    }
}

如果不喜欢java方式,也可以自行百度参照xml方式配置。

    原文作者:Spring MVC
    原文地址: https://blog.csdn.net/u013412772/article/details/78646740
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞