java全局配置

Posted zjxgdq

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了java全局配置相关的知识,希望对你有一定的参考价值。

<settings>
        <setting name="mapUnderscoreToCamelCase" value="true"/>
        <setting name="cacheEnabled" value="true"/>
    </settings>
    <typeAliases>
<!--        <typeAlias type="com.example.demotest.domain.Videos" alias="video"></typeAlias>-->
        <package name="com.example.demotest.domain"/>
    </typeAliases>

  

java web全局配置

import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import com.cnabc.base.springcloud.interceptor.UserAuthRestInterceptor;
import org.springframework.boot.actuate.endpoint.http.ActuatorMediaType;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;

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

@Configuration
@Primary
public class WebConfiguration extends WebMvcConfigurationSupport {

    @Bean
    UserAuthRestInterceptor getUserAuthRestInterceptor() {
        return new UserAuthRestInterceptor();
    }

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(getUserAuthRestInterceptor()).addPathPatterns("/**");
    }

    @Override
    protected RequestMappingHandlerMapping createRequestMappingHandlerMapping() {
        return new RequestMappingHandlerMapping() {
            @Override
            protected boolean isHandler(Class<?> beanType) {
                boolean isController = isController(beanType);
                System.out.println("class " + beanType.toString() + "   " + isController);
                return super.isHandler(beanType) && isController;
            }
        };
    }

    /**
     * 判断是否是货真价实的Controller
     */
    private boolean isController(Class<?> beanType) {
        Class<?>[] interfaces = beanType.getInterfaces();
        for (Class<?> anInterface : interfaces) {
            if (anInterface.getAnnotation(FeignClient.class) != null
                && beanType.getAnnotation(RestController.class) == null){
                return false;
            }
        }

        return beanType.getAnnotation(FeignClient.class) == null || !beanType.isInterface();

    }

    /**
     * 修改自定义消息转换器
     *
     * @param converters 消息转换器列表
     */
    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        //調用父類的配置
        super.configureMessageConverters(converters);
        //創建fastJson消息轉換器
        FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();

        //升級最新版本需加=============================================================
        List<MediaType> supportedMediaTypes = new ArrayList<>();
        supportedMediaTypes.add(MediaType.APPLICATION_JSON);
        supportedMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
        supportedMediaTypes.add(MediaType.APPLICATION_ATOM_XML);
        supportedMediaTypes.add(MediaType.APPLICATION_FORM_URLENCODED);
        supportedMediaTypes.add(MediaType.APPLICATION_OCTET_STREAM);
        supportedMediaTypes.add(MediaType.APPLICATION_PDF);
        supportedMediaTypes.add(MediaType.APPLICATION_RSS_XML);
        supportedMediaTypes.add(MediaType.APPLICATION_XHTML_XML);
        supportedMediaTypes.add(MediaType.APPLICATION_XML);
        supportedMediaTypes.add(MediaType.IMAGE_GIF);
        supportedMediaTypes.add(MediaType.IMAGE_JPEG);
        supportedMediaTypes.add(MediaType.IMAGE_PNG);
        supportedMediaTypes.add(MediaType.TEXT_EVENT_STREAM);
        supportedMediaTypes.add(MediaType.TEXT_HTML);
        supportedMediaTypes.add(MediaType.TEXT_MARKDOWN);
        supportedMediaTypes.add(MediaType.TEXT_PLAIN);
        supportedMediaTypes.add(MediaType.TEXT_XML);
        supportedMediaTypes.add(MediaType.valueOf(ActuatorMediaType.V1_JSON));
        supportedMediaTypes.add(MediaType.valueOf(ActuatorMediaType.V2_JSON));
        fastConverter.setSupportedMediaTypes(supportedMediaTypes);

        //創建配置類
        FastJsonConfig fastJsonConfig = new FastJsonConfig();
        //修改配置返回內容的過濾
        //WriteNullListAsEmpty  :List字段如果為null,輸出為[],而非null
        //WriteNullStringAsEmpty : 字符類型字段如果為null,輸出為"",而非null
        //DisableCircularReferenceDetect :消除對同一對象循環引用的問題,默認為false(如果不配置有可能會進入死循環)
        //WriteNullBooleanAsFalse:Boolean字段如果為null,輸出為false,而非null
        //WriteMapNullValue:是否輸出值為null的字段,默認為false
        fastJsonConfig.setSerializerFeatures(
                SerializerFeature.DisableCircularReferenceDetect,
                SerializerFeature.WriteNullListAsEmpty
        );
        fastConverter.setFastJsonConfig(fastJsonConfig);
        //將fastjson添加到視圖消息轉換器列表內
        converters.add(fastConverter);
    }


}

以上是关于java全局配置的主要内容,如果未能解决你的问题,请参考以下文章

java web全局配置

java全局配置

java 全局的应用程序,通常用来获取唯一的应用程序或者是进行全局的配置

JAVA_全局配置文件(配置网址,url等等)_第一种方式

Gradle配置全局变量

Java_myBatis_全局配置文件