springBoot 消息转换器和自定义消息转换器

public interface HttpMessageConverter<T> {

   /**
    * 能否以指定的类读取
    */
   boolean canRead(Class<?> clazz, @Nullable MediaType mediaType);

   /**
    * 能否以指定的类写
    */
   boolean canWrite(Class<?> clazz, @Nullable MediaType mediaType);

   /**
    * 返回支持是消息转换器的媒体列表
    */
   List<MediaType> getSupportedMediaTypes();

   /**
    * Return the list of media types supported by this converter for the given
    * class. The list may differ from {@link #getSupportedMediaTypes()} if the
    * converter does not support the given Class or if it supports it only for
    * a subset of media types.
    * @param clazz the type of class to check
    * @return the list of media types supported for the given class
    * @since 5.3.4
    */
   default List<MediaType> getSupportedMediaTypes(Class<?> clazz) {
      return (canRead(clazz, null) || canWrite(clazz, null) ?
            getSupportedMediaTypes() : Collections.emptyList());
   }

   /**
    * 将输入的内容读成指定的类型并返回
    */
   T read(Class<? extends T> clazz, HttpInputMessage inputMessage)
         throws IOException, HttpMessageNotReadableException;

   /**
    * 将给定的内容写成指定的类型
    */
   void write(T t, @Nullable MediaType contentType, HttpOutputMessage outputMessage)
         throws IOException, HttpMessageNotWritableException;

}

由此可见,可根据游览器接受类型和服务器生产类型,返回不同的数据类型:

①定义游览器可以接受的数据类型:

例如:

Accept:text/html,application/xhtml+xml,application/xml

②服务器引入可生产的数据类型的jar或自定义消息转换器

例如:引入可生产xml消息类型的jar包   

扩展:自定义消息转换器

一、通过实现接口:WebMvcConfigurer

@Configuration
public class BootConfig implements WebMvcConfigurer {

    /**
     * 自定义参数转换
     */
    @Override
    public void addFormatters(FormatterRegistry registry) {
        registry.addConverter(new Converter<String, Integer>() {
            @Override
            public Integer convert(String source) {
                return null;
            }
        });
    }

    /**
     *
     */
    @Override
    public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
        converters.add(new MyMessageConver());
    }
}

二、通过注册Bean

@Bean
public WebMvcConfigurer webMvcConfigurer() {
    return new WebMvcConfigurer() {
        /**
         * 添加自定义格式化器或转换器
         *
         * @param registry
         */
        @Override
        public void addFormatters(FormatterRegistry registry) {
            //Converter<S, T>  S源类型,T目标类型
            registry.addConverter(new MyConverter());
        }

        /**
         * 扩展消息转换器
         */
        @Override
        public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
            converters.add(new MyMessageConver());
        }
    };
}

   

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值