All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.zodiac.autoconfigure.web.ReactiveHttpMessageAutoConfiguration Maven / Gradle / Ivy

There is a newer version: 1.6.8
Show newest version
package org.zodiac.autoconfigure.web;

import java.util.Arrays;

import org.springframework.boot.SpringBootConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnClass;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication.Type;
import org.springframework.core.Ordered;
import org.springframework.core.annotation.Order;
import org.springframework.core.codec.ByteArrayDecoder;
import org.springframework.core.codec.ByteArrayEncoder;
import org.springframework.core.codec.ResourceDecoder;
import org.springframework.core.codec.ResourceEncoder;
import org.springframework.core.codec.ResourceRegionEncoder;
import org.springframework.core.codec.StringDecoder;
import org.springframework.format.FormatterRegistry;
import org.springframework.format.datetime.DateFormatter;
import org.springframework.http.codec.ServerCodecConfigurer;
import org.springframework.http.codec.json.AbstractJackson2Decoder;
import org.springframework.http.codec.json.AbstractJackson2Encoder;
import org.springframework.util.MimeType;
import org.springframework.web.reactive.config.WebFluxConfigurer;
import org.springframework.web.reactive.result.method.annotation.ArgumentResolverConfigurer;
import org.zodiac.autoconfigure.jackson.PlatformJacksonOptionProperties;
import org.zodiac.commons.util.DateTimes;
import org.zodiac.core.web.http.codec.json.MappingApiJackson2JsonDecoder;
import org.zodiac.core.web.http.codec.json.MappingApiJackson2JsonEncoder;
import org.zodiac.core.web.reactive.ConventionalMethodArgumentResolver;

import com.fasterxml.jackson.databind.ObjectMapper;

@SpringBootConfiguration
@Order(value = Ordered.HIGHEST_PRECEDENCE)
@ConditionalOnWebApplication(type = Type.REACTIVE)
@ConditionalOnClass(value = {org.springframework.web.server.WebHandler.class, org.springframework.web.reactive.DispatcherHandler.class})
public class ReactiveHttpMessageAutoConfiguration implements WebFluxConfigurer {

    private static final MimeType[] EMPTY_MIME_TYPES = {};

    private final ObjectMapper objectMapper;
    private final PlatformJacksonOptionProperties platformJacksonOptionProperties;

    public ReactiveHttpMessageAutoConfiguration(ObjectMapper objectMapper, PlatformJacksonOptionProperties platformJacksonOptionProperties) {
        this.objectMapper = objectMapper;
        this.platformJacksonOptionProperties = platformJacksonOptionProperties;
    }

    /*使用 JACKSON 作为JSON MessageConverter 消息转换,内置断点续传,下载和字符串。*/
    @Override
    public void configureHttpMessageCodecs(ServerCodecConfigurer configurer) {
        ObjectMapper readObjectMapper = objectMapper;
        /*拷贝 readObjectMapper*/
        //ObjectMapper writeObjectMapper = readObjectMapper.copy();

        StringDecoder stringDecoder = StringDecoder.textPlainOnly(Arrays.asList(",", "\n"), false);

        //Decoder jackson2JsonCharsetDecoder = new Jackson2JsonCharsetDecoder(platformJacksonOptionProperties, stringDecoder, readObjectMapper, EMPTY_MIME_TYPES);
        //Encoder jackson2JsonCharsetEncoder = new Jackson2JsonCharsetEncoder(platformJacksonOptionProperties, writeObjectMapper, EMPTY_MIME_TYPES);

        /*由于AutoConfigure会自动覆盖jackson2JsonEncoder/Decoder,以下配置无法生效*/
        //configurer.defaultCodecs().jackson2JsonDecoder(jackson2JsonCharsetDecoder);
        //configurer.defaultCodecs().jackson2JsonEncoder(jackson2JsonCharsetEncoder);

        //configurer.customCodecs().register(jackson2JsonCharsetDecoder);
        //configurer.customCodecs().register(jackson2JsonCharsetEncoder);

        configurer.getReaders().removeIf(x -> x instanceof AbstractJackson2Encoder || x instanceof AbstractJackson2Decoder);

        stringDecoder.setDefaultCharset(platformJacksonOptionProperties.getCharset());
        configurer.customCodecs().register(stringDecoder);

        configurer.customCodecs().register(new ByteArrayEncoder());
        configurer.customCodecs().register(new ByteArrayDecoder());
        configurer.customCodecs().register(new ResourceEncoder());
        configurer.customCodecs().register(new ResourceDecoder());
        configurer.customCodecs().register(new  ResourceRegionEncoder());

        configurer.customCodecs().register(new MappingApiJackson2JsonDecoder(platformJacksonOptionProperties, readObjectMapper, EMPTY_MIME_TYPES));
        configurer.customCodecs().register(new MappingApiJackson2JsonEncoder(platformJacksonOptionProperties, readObjectMapper, EMPTY_MIME_TYPES));
    }

    /*日期格式化*/
    @Override
    public void addFormatters(FormatterRegistry registry) {
        registry.addFormatter(new DateFormatter(DateTimes.FORMAT_10));
        registry.addFormatter(new DateFormatter(DateTimes.FORMAT_22_b));
    }

    @Override
    public void configureArgumentResolvers(ArgumentResolverConfigurer configurer) {
        configurer.addCustomResolver(new ConventionalMethodArgumentResolver());
    }

}