当前位置: 代码网 > it编程>编程语言>Java > 聊聊springboot中如何自定义消息转换器

聊聊springboot中如何自定义消息转换器

2025年08月16日 Java 我要评论
spring boot 中的消息转换器(httpmessageconverter)是处理 http 请求和响应数据格式转换的核心组件,负责将 http 请求体中的数据转换为 java 对象,或将 ja

spring boot 中的消息转换器(httpmessageconverter)是处理 http 请求和响应数据格式转换的核心组件,负责将 http 请求体中的数据转换为 java 对象,或将 java 对象转换为 http 响应体的数据。

核心接口

public interface httpmessageconverter<t> {
    boolean canread(class<?> clazz, @nullable mediatype mediatype);
    boolean canwrite(class<?> clazz, @nullable mediatype mediatype);
    list<mediatype> getsupportedmediatypes();
    default list<mediatype> getsupportedmediatypes(class<?> clazz) {
        return !this.canread(clazz, (mediatype)null) && !this.canwrite(clazz, (mediatype)null) ? collections.emptylist() : this.getsupportedmediatypes();
    }
    t read(class<? extends t> clazz, httpinputmessage inputmessage) throws ioexception, httpmessagenotreadableexception;
    void write(t t, @nullable mediatype contenttype, httpoutputmessage outputmessage) throws ioexception, httpmessagenotwritableexception;
}

getsupportedmediatypes 定义支持的媒体类型(如:application/json)

canread/canwrite:判断是否支持读写操作

read/write:执行具体的数据转换逻辑

springboot默认提供的转换器

spring boot 自动配置了以下常用消息转换器:

  1. stringhttpmessageconverter - 处理文本数据
  2. mappingjackson2httpmessageconverter - 处理 json 数据(使用 jackson)
  3. formhttpmessageconverter - 处理表单数据
  4. bytearrayhttpmessageconverter - 处理字节数组
  5. resourcehttpmessageconverter - 处理资源文件
  6. jaxb2rootelementhttpmessageconverter - 处理 xml(使用 jaxb)
  7. allencompassingformhttpmessageconverter - 增强的表单处理器

springboot在启动的时候就会自动注册上述默认的转换器,有请求进来的时候会根据请求的accept和响应的content-type,选择匹配的转换器,若多个转换器都支持,则按注册顺序优先。

如何自定义消息转换器

比如自定义个fastjson转换器

import com.alibaba.fastjson2.json;
import com.alibaba.fastjson2.support.config.fastjsonconfig;
import org.springframework.http.httpinputmessage;
import org.springframework.http.httpoutputmessage;
import org.springframework.http.mediatype;
import org.springframework.http.converter.abstracthttpmessageconverter;
import org.springframework.http.converter.httpmessagenotreadableexception;
import org.springframework.http.converter.httpmessagenotwritableexception;
import org.springframework.lang.nonnull;
import java.io.ioexception;
import java.io.inputstream;
import java.io.outputstream;
import java.lang.reflect.type;
import java.nio.charset.standardcharsets;
import java.util.arraylist;
import java.util.list;
public class fastjsonhttpmessageconverter extends abstracthttpmessageconverter<object> {
    private final fastjsonconfig fastjsonconfig = new fastjsonconfig();
    public fastjsonhttpmessageconverter() {
        // 支持多种媒体类型
        list<mediatype> supportedmediatypes = new arraylist<>();
        supportedmediatypes.add(mediatype.application_json);
        supportedmediatypes.add(new mediatype("application", "*+json"));
        setsupportedmediatypes(supportedmediatypes);
        // 配置fastjson
        fastjsonconfig.setdateformat("yyyy-mm-dd hh:mm:ss");
        fastjsonconfig.setcharset(standardcharsets.utf_8);
    }
    @override
    protected boolean supports(@nonnull class<?> clazz) {
        return true; // 支持所有类型
    }
    @override
    protected object readinternal(
            @nonnull class<?> clazz,
            @nonnull httpinputmessage inputmessage
    ) throws ioexception, httpmessagenotreadableexception {
        try (inputstream in = inputmessage.getbody()) {
            return json.parseobject(
                    in,
                    fastjsonconfig.getcharset(),
                    clazz,
                    fastjsonconfig.getfeatures()
            );
        } catch (exception e) {
            throw new httpmessagenotreadableexception(
                    "json parse error: " + e.getmessage(),
                    inputmessage
            );
        }
    }
    @override
    protected void writeinternal(
            @nonnull object object,
            @nonnull httpoutputmessage outputmessage
    ) throws ioexception, httpmessagenotwritableexception {
        try (outputstream out = outputmessage.getbody()) {
            json.writeto(
                    out,
                    object,
                    fastjsonconfig.getcharset(),
                    fastjsonconfig.getfeatures(),
                    fastjsonconfig.getfilters(),
                    fastjsonconfig.getdateformat(),
                    json.default_generate_feature,
                    fastjsonconfig.getwriterfeatures()
            );
        } catch (exception e) {
            throw new httpmessagenotwritableexception(
                    "json write error: " + e.getmessage(),
                    e
            );
        }
    }
    public fastjsonconfig getfastjsonconfig() {
        return fastjsonconfig;
    }
    public void setfastjsonconfig(fastjsonconfig fastjsonconfig) {
        this.fastjsonconfig.setdateformat(fastjsonconfig.getdateformat());
        this.fastjsonconfig.setcharset(fastjsonconfig.getcharset());
        this.fastjsonconfig.setfeatures(fastjsonconfig.getfeatures());
        this.fastjsonconfig.setreaderfeatures(fastjsonconfig.getreaderfeatures());
        this.fastjsonconfig.setwriterfeatures(fastjsonconfig.getwriterfeatures());
        this.fastjsonconfig.setfilters(fastjsonconfig.getfilters());
    }
}

注册自定义的消息转换器 

import org.springframework.context.annotation.configuration;
import org.springframework.http.converter.httpmessageconverter;
import org.springframework.web.servlet.config.annotation.webmvcconfigurer;
import java.util.list;
@configuration
public class fastjsonwebconfig implements webmvcconfigurer {
    @override
    public void configuremessageconverters(list<httpmessageconverter<?>> converters) {
        // 创建fastjson消息转换器
        fastjsonhttpmessageconverter fastjsonconverter = new fastjsonhttpmessageconverter();
        // 可以在这里进一步配置fastjson
        // fastjsonconverter.getfastjsonconfig().setdateformat("yyyy-mm-dd");
        // 将fastjson转换器添加到转换器列表的最前面
        converters.add(0, fastjsonconverter);
    }
}

注意这里优先级的问题,要把fastjson的放前面,这样才会优先走咱自定义的fastjson的转换器,而不是默认的gson的

到此这篇关于聊聊springboot中如何自定义消息转换器的文章就介绍到这了,更多相关springboot 消息转换器内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com