GatewayApp.java 2.83 KB
package com.viontech.fanxing.forward;

import feign.codec.Decoder;
import feign.optionals.OptionalDecoder;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.http.HttpMessageConverters;
import org.springframework.cloud.client.discovery.EnableDiscoveryClient;
import org.springframework.cloud.openfeign.EnableFeignClients;
import org.springframework.cloud.openfeign.support.ResponseEntityDecoder;
import org.springframework.cloud.openfeign.support.SpringDecoder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.ComponentScan;
import org.springframework.context.annotation.FilterType;
import org.springframework.http.MediaType;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.scheduling.annotation.EnableScheduling;

import java.util.ArrayList;
import java.util.List;

/**
 * .
 *
 * @author 谢明辉
 * @date 2021/6/11
 */
@SpringBootApplication
@ComponentScan(basePackages = "com.viontech.fanxing", excludeFilters = {
        @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, classes = com.viontech.fanxing.commons.config.WebConfig.class),
        @ComponentScan.Filter(type = FilterType.ASSIGNABLE_TYPE, classes = com.viontech.fanxing.commons.base.GlobalExceptionHandler.class)
})
@EnableDiscoveryClient
@EnableScheduling
@EnableFeignClients(basePackages = "com.viontech.fanxing")
@Slf4j
public class GatewayApp {

    public static void main(String[] args) {
        try {
            SpringApplication.run(GatewayApp.class, args);
        } catch (Exception e) {
            log.error("gateway app start error", e);
        }
    }

    @Bean
    public Decoder feignDecoder(MyMappingJackson2HttpMessageConverter converter) {
        ObjectFactory<HttpMessageConverters> factory = () -> new HttpMessageConverters(converter);
        return new OptionalDecoder(
                new ResponseEntityDecoder(new SpringDecoder(factory)));
    }

    @Bean
    public MyMappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter() {
        return new MyMappingJackson2HttpMessageConverter();
    }

    @Bean
    public HttpMessageConverters httpMessageConverters(MyMappingJackson2HttpMessageConverter converter) {
        return new HttpMessageConverters(converter);
    }

    public static class MyMappingJackson2HttpMessageConverter extends MappingJackson2HttpMessageConverter {
        public MyMappingJackson2HttpMessageConverter() {
            List<MediaType> mediaTypeList = new ArrayList<>(this.getSupportedMediaTypes());
            mediaTypeList.add(MediaType.TEXT_PLAIN);
            this.setSupportedMediaTypes(mediaTypeList);
        }

    }

}