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

com.cloud.lego.web.spring.boot.autoconfigure.StandardResultAutoConfiguration Maven / Gradle / Ivy

package com.cloud.lego.web.spring.boot.autoconfigure;

import com.cloud.lego.web.spring.boot.autoconfigure.constants.OrderConstants;
import com.cloud.lego.web.spring.boot.core.interceptor.AppClientInterceptor;
import com.cloud.lego.web.spring.boot.core.interceptor.VersionInterceptor;
import com.cloud.lego.web.spring.boot.core.properties.StandardResultProperties;
import com.cloud.lego.web.spring.boot.core.standard.advice.StandardResultResponseBody;
import com.cloud.lego.web.spring.boot.core.standard.advice.WebGlobalExceptionHandler;
import org.springframework.boot.autoconfigure.AutoConfiguration;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.condition.ConditionalOnWebApplication;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import static com.cloud.lego.constants.spring.boot.web.WebConstants.STATIC_RESOURCE_URLS;

/**
 * Web自动装配,web应用才会创建
 *
 * @author Andy
 */
@AutoConfiguration(after = {WebRequestHolderAutoConfiguration.class})
@EnableConfigurationProperties(StandardResultProperties.class)
@ConditionalOnWebApplication
public class StandardResultAutoConfiguration implements WebMvcConfigurer {

    /**
     * 初始化 GlobalExceptionHandler全局异常
     *
     * @return 全局异常处理
     */
    @Bean
    @ConditionalOnMissingBean(WebGlobalExceptionHandler.class)
    public WebGlobalExceptionHandler webGlobalExceptionHandler(StandardResultProperties standardResultProperties) {
        return new WebGlobalExceptionHandler(standardResultProperties);
    }

    /**
     * 初始化标准返回结果支持
     *
     * @return 标准结果转换
     */
    @Bean
    @ConditionalOnMissingBean(StandardResultResponseBody.class)
    public StandardResultResponseBody standardResultResponseBody(StandardResultProperties standardResultProperties) {
        return new StandardResultResponseBody(standardResultProperties);
    }

    /**
     * 配置拦截器
     *
     * @param registry 静态资源过滤
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new VersionInterceptor()).order(OrderConstants.Interceptor.WEB_API_VERSION).addPathPatterns("/**")
                .excludePathPatterns(STATIC_RESOURCE_URLS);
        registry.addInterceptor(new AppClientInterceptor()).order(OrderConstants.Interceptor.WEB_REQUEST_HEADER).addPathPatterns("/**")
                .excludePathPatterns(STATIC_RESOURCE_URLS);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy