【Webflux】实现全局返回Long转String

基于spring webflux框架,而非springmvc

@Configuration
public class LongToStringConfig {

    @Bean
    public ObjectMapper objectMapper() {
        ObjectMapper objectMapper = new ObjectMapper();
        SimpleModule simpleModule = new SimpleModule();
        simpleModule.addSerializer(Long.class, ToStringSerializer.instance)
                .addSerializer(Long.TYPE, ToStringSerializer.instance);
        objectMapper.registerModule(simpleModule);
        return objectMapper;
    }
}
package com.diligent.cloud.open.api.gateway.config;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.reactivestreams.Publisher;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.ResolvableType;
import org.springframework.core.codec.Encoder;
import org.springframework.core.io.buffer.DataBuffer;
import org.springframework.core.io.buffer.DataBufferFactory;
import org.springframework.http.codec.ServerCodecConfigurer;
import org.springframework.http.codec.json.Jackson2JsonEncoder;
import org.springframework.util.MimeType;
import org.springframework.web.reactive.config.WebFluxConfigurer;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;

import java.nio.charset.StandardCharsets;
import java.util.List;
import java.util.Map;

/**
 * @Description
 * @Author 李宇麒
 * @Version V1.0.0
 * @Date 2024/3/29
 */

@Configuration
public class WebFluxConfig implements WebFluxConfigurer {

    @Autowired
    private ObjectMapper objectMapper;

    @Override
    public void configureHttpMessageCodecs(ServerCodecConfigurer configurer) {
        configurer.customCodecs().register(new LongToStringEncoder(objectMapper));
    }

    public class LongToStringEncoder implements Encoder<Object> {

        private final Encoder<Object> delegate;
        private final ObjectMapper objectMapper;

        public LongToStringEncoder(ObjectMapper o) {
            objectMapper = o;
            Jackson2JsonEncoder encoder = new Jackson2JsonEncoder(o);
            this.delegate = encoder;
        }

        @Override
        public boolean canEncode(ResolvableType elementType, MimeType mimeType) {
            return this.delegate.canEncode(elementType, mimeType);
        }

        @Override
        public List<MimeType> getEncodableMimeTypes() {
            return delegate.getEncodableMimeTypes();
        }

        @Override
        public Flux<DataBuffer> encode(Publisher<?> inputStream, DataBufferFactory bufferFactory, ResolvableType elementType, MimeType mimeType, Map<String, Object> hints) {

            return Flux.from(inputStream)
                    .flatMap(value -> {
                        try {
                            //序列化
                            String res = objectMapper.writeValueAsString(value);

                            byte[] bytes = res.getBytes(StandardCharsets.UTF_8);
                            DataBuffer dataBuffer = bufferFactory.wrap(bytes);
                            return Mono.just(dataBuffer);
                        } catch (JsonProcessingException e) {
                            e.printStackTrace();
                            return this.delegate.encode(inputStream, bufferFactory, elementType, mimeType, hints);
                        }
                    });
        }

        @Override
        public DataBuffer encodeValue(Object value, DataBufferFactory bufferFactory, ResolvableType valueType, MimeType mimeType, Map<String, Object> hints) {
            // if (value instanceof Long) {
            // value = String.valueOf(value);
            // }
            return Encoder.super.encodeValue(value, bufferFactory, valueType, mimeType, hints);
        }

    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值