基于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);
}
}
}