Open hu70258tao opened 1 year ago
可以自定义,我去试试
这是个简单实现,仅提供思路
@Configuration
public static class WebConfig implements WebFluxConfigurer {
@Override
public void configureHttpMessageCodecs(ServerCodecConfigurer configurer) {
configurer.registerDefaults(false);
//反序列化
configurer.customCodecs().register(new CustomDecoder());
//序列化
configurer.customCodecs().register(new CustomEncoder());
}
}
//反序列化
public static class CustomDecoder extends AbstractDecoder<Object> {
public CustomDecoder() {
super(MediaType.APPLICATION_JSON, MediaType.TEXT_HTML);
}
@Override
public Flux<Object> decode(Publisher<DataBuffer> inputStream, ResolvableType elementType, MimeType mimeType, Map<String, Object> hints) {
return Flux.from(inputStream)
.map(dataBuffer -> dataBuffer.toString(StandardCharsets.UTF_8))
.map(json -> JSON.parseObject(json, elementType.getType()));
}
}
//序列化
public static class CustomEncoder extends AbstractEncoder<Object> {
public CustomEncoder() {
super(MediaType.APPLICATION_JSON, MediaType.TEXT_HTML);
}
@Override
public Flux<DataBuffer> encode(Publisher<?> inputStream, DataBufferFactory bufferFactory, ResolvableType elementType, MimeType mimeType, Map<String, Object> hints) {
return Flux.from(inputStream)
.map(obj -> {
return JSON.toJSONBytes(obj, JSONWriter.Feature.WriteNullStringAsEmpty);
})
.map(bufferFactory::wrap);
}
}
@kaizen84 提交个PR集成进来?
@kaizen84 提交个PR集成进来?
精力和能力有限,暂时提供不了帮助😯后面有机会我可以试试
我看这两个类已经集成了 你可以试试
com.alibaba.fastjson2.support.spring.http.codec.Fastjson2Decoder com.alibaba.fastjson2.support.spring.http.codec.Fastjson2Encoder
in springboot 3.1.2
com.alibaba.fastjson2.support.spring.http.codec.Fastjson2Decoder com.alibaba.fastjson2.support.spring.http.codec.Fastjson2Encoder 集成的那两个类,貌似依赖objectmapper, 我把objectmapper相关的jackson依赖都移除了,就不行了,我简单改造了下
我新测试了一个
public class FastJson2Writer implements HttpMessageWriter<Object> {
@Override
@NonNull
public List<MediaType> getWritableMediaTypes() {
List<MediaType> mediaTypeList = new ArrayList<>();
mediaTypeList.add(MediaType.APPLICATION_JSON);
return mediaTypeList;
}
@Override
public boolean canWrite(@NonNull ResolvableType elementType, MediaType mediaType) {
return (mediaType == null || MediaType.APPLICATION_JSON.includes(mediaType));
}
@Override
@NonNull
public Mono<Void> write(@NonNull Publisher<?> inputStream, @NonNull ResolvableType elementType, MediaType mediaType, @NonNull ReactiveHttpOutputMessage message, @NonNull Map<String, Object> hints) {
return Mono.from(inputStream).flatMap(is -> {
message.getHeaders().setContentType(MediaType.APPLICATION_JSON);
DataBuffer dataBuffer = message.bufferFactory().wrap(JSON.toJSONBytes(is, JSONWriter.Feature.WriteNullStringAsEmpty));
Flux<DataBuffer> dataStream = Flux.just(dataBuffer);
return message.writeWith(dataStream);
});
}
}
配置类
@Configuration(proxyBeanMethods = false)
public class FastJson2Config {
@Bean
public CodecCustomizer fastjson2CodecCustomizer() {
return (configurer) -> {
// configurer.registerDefaults(false);
// configurer.customCodecs().register(new FastJson2Reader());
configurer.customCodecs().register(new FastJson2Writer());
};
}
}
package cn.lsguangsucm.webapi.config.json2;
import com.alibaba.fastjson2.JSON;
import org.springframework.core.ResolvableType;
import org.springframework.http.MediaType;
import org.springframework.http.ReactiveHttpInputMessage;
import org.springframework.http.codec.HttpMessageReader;
import org.springframework.lang.NonNull;
import reactor.core.publisher.Flux;
import reactor.core.publisher.Mono;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
public class FastJson2Reader implements HttpMessageReader<Object> {
@Override
@NonNull
public List<MediaType> getReadableMediaTypes() {
List<MediaType> mediaTypeList = new ArrayList<>();
mediaTypeList.add(MediaType.APPLICATION_JSON);
return mediaTypeList;
}
@Override
public boolean canRead(@NonNull ResolvableType elementType, MediaType mediaType) {
return (mediaType == null || MediaType.APPLICATION_JSON.includes(mediaType));
}
@Override
@NonNull
public Flux<Object> read(@NonNull ResolvableType elementType,
@NonNull ReactiveHttpInputMessage message,
@NonNull Map<String, Object> hints) {
return message.getBody()
.map(dataBuffer -> dataBuffer.toString(StandardCharsets.UTF_8))
.map(json -> JSON.parseObject(json, elementType.getType()));
}
@Override
@NonNull
public Mono<Object> readMono(@NonNull ResolvableType elementType,
@NonNull ReactiveHttpInputMessage message,
@NonNull Map<String, Object> hints) {
return Mono.from(message.getBody())
.map(dataBuffer -> dataBuffer.toString(StandardCharsets.UTF_8))
.map(json -> JSON.parseObject(json, elementType.getType()));
}
}
不知道怎么替换默认的jackson,也找不到其他的指导文档。作者能不能在Readme文件里,补充下如何在springboot-webflux里的替换教程。