【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);
        }

    }
}

相关推荐

  1. Webflux实现全局返回LongString

    2024-03-31 19:36:07       38 阅读
  2. SpringBoot全局配置LongString丢失精度的问题解决

    2024-03-31 19:36:07       60 阅读
  3. Spring (60)Spring WebFlux

    2024-03-31 19:36:07       28 阅读
  4. Spring WebFlux响应式实现WebFilter解决跨域问题

    2024-03-31 19:36:07       38 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2024-03-31 19:36:07       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2024-03-31 19:36:07       101 阅读
  3. 在Django里面运行非项目文件

    2024-03-31 19:36:07       82 阅读
  4. Python语言-面向对象

    2024-03-31 19:36:07       91 阅读

热门阅读

  1. 面试中会被问到的GIT问题解答(含答案)

    2024-03-31 19:36:07       33 阅读
  2. 在数据开发项目中使用Hive的场景和风险

    2024-03-31 19:36:07       35 阅读
  3. python基础练习题6

    2024-03-31 19:36:07       39 阅读
  4. 组件递归和动态component

    2024-03-31 19:36:07       40 阅读
  5. Product of Binary Decimals(搜索,暴力枚举,打表预处理)

    2024-03-31 19:36:07       29 阅读
  6. 2024系统架构师---解释器架构风格的概念与应用

    2024-03-31 19:36:07       37 阅读
  7. cs0449 c

    cs0449 c

    2024-03-31 19:36:07      35 阅读