Spring源码学习--HttpMessageConverter<T>接口

本文介绍了Spring 3.0引入的HttpMessageConverter接口,该接口用于将请求信息转换为对象,并将对象绑定到请求方法参数或响应信息。详细讲解了接口源码、HttpInputMessage和HttpOutputMessage接口及其在DispatcherServlet和RequestMappingHandlerAdapter中的应用。同时,讨论了HttpMessageConverter的多种实现类及其在Spring MVC中的实际运用。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

文章参考:

1 http://blog.youkuaiyun.com/silencecarrot/article/details/52493709

2 http://blog.youkuaiyun.com/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提供了多个实现类,这些实现类组成了一个功能强大、用途广泛的信息转换家族,主要包括一下实现类:

这里写图片描述

其中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方式配置。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值