• Swagger2在spring拦截器的配置及全局参数设置


    一:配置拦截器

    默认拦截器是对Swagger2请求时拦截的。

    @Configuration
    @EnableWebMvc
    public class MvcConfig implements WebMvcConfigurer {
    
    
    
        @Override
        public void addInterceptors(InterceptorRegistry registry) {
            //配置登录拦截器
            registry.addInterceptor(new AuthorizedCheckInterceptor()).
                    addPathPatterns("/**")
                        .excludePathPatterns("/users/login/**", "/swagger-resources/**", "/webjars/**", "/swagger-ui.html/**");
        }
    
        @Override
        public void addResourceHandlers(ResourceHandlerRegistry registry) {
            //将templates目录下的CSS、JS文件映射为静态资源,防止Spring把这些资源识别成thymeleaf模版
            //  registry.addResourceHandler("/templates/**.js").addResourceLocations("classpath:/templates/");
            //registry.addResourceHandler("/templates/**.css").addResourceLocations("classpath:/templates/");
            //其他静态资源
            //registry.addResourceHandler("/static/**").addResourceLocations("classpath:/static/");
            //swagger增加url映射
            registry.addResourceHandler("swagger-ui.html")
                    .addResourceLocations("classpath:/META-INF/resources/");
            registry.addResourceHandler("/webjars/**")
                    .addResourceLocations("classpath:/META-INF/resources/webjars/");
        }
    
    }

    二 配置全局参数

    如配置所有请求header中需要添加token,配置全局响应码

    package org.niugang.coding.conf;
    
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.web.bind.annotation.RequestMethod;
    import springfox.documentation.builders.*;
    import springfox.documentation.schema.ModelRef;
    import springfox.documentation.service.ApiInfo;
    import springfox.documentation.service.Contact;
    import springfox.documentation.service.Parameter;
    import springfox.documentation.service.ResponseMessage;
    import springfox.documentation.spi.DocumentationType;
    import springfox.documentation.spring.web.plugins.Docket;
    import springfox.documentation.swagger2.annotations.EnableSwagger2;
    
    import java.util.ArrayList;
    import java.util.List;
    
    /**
     * 接口文档
     *
     * @author Created by niugang on 2018/12/28/15:10
     */
    @Configuration
    @EnableSwagger2
    public class SwaggerConfig {
    
        /**
         * 全局参数(如header中的token)
         *
         * @return List<Parameter>
         */
        private List<Parameter> parameter() {
            List<Parameter> params = new ArrayList<>();
            params.add(new ParameterBuilder().name("token")
                    .description("请求令牌")
                    .modelRef(new ModelRef("string"))
                    .parameterType("header")
                    .required(false).build());
            return params;
        }
    
        @Bean
        public Docket sysApi() {
            //设置全局响应状态码
            List<ResponseMessage> responseMessageList = new ArrayList<>();
            responseMessageList.add(new ResponseMessageBuilder().code(404).message("找不到资源").responseModel(new ModelRef("ApiError")).build());
            responseMessageList.add(new ResponseMessageBuilder().code(400).message("参数错误").responseModel(new ModelRef("ApiError")).build());
            responseMessageList.add(new ResponseMessageBuilder().code(401).message("没有认证").responseModel(new ModelRef("ApiError")).build());
            responseMessageList.add(new ResponseMessageBuilder().code(500).message("服务器内部错误").responseModel(new ModelRef("ApiError")).build());
            responseMessageList.add(new ResponseMessageBuilder().code(403).message("没有没有访问权限").responseModel(new ModelRef("ApiError")).build());
            responseMessageList.add(new ResponseMessageBuilder().code(200).message("请求成功").responseModel(new ModelRef("ApiError")).build());
    
            return new Docket(DocumentationType.SWAGGER_2)
                    .globalResponseMessage(RequestMethod.GET, responseMessageList)
                    .globalResponseMessage(RequestMethod.POST, responseMessageList)
                    .globalResponseMessage(RequestMethod.PUT, responseMessageList)
                    .globalResponseMessage(RequestMethod.DELETE, responseMessageList)
                    .apiInfo(apiInfo())
                    .select()
                     //默认显示所有controller
                    .apis(RequestHandlerSelectors.basePackage("org.niugang.coding"))
                    .paths(PathSelectors.any())
                    .build().globalOperationParameters(parameter());
        }
    
        private ApiInfo apiInfo() {
            return new ApiInfoBuilder()
                    .title("new-coding-standards-user")
                    .description("springboot新代码规范脚手架工程")
                    .termsOfServiceUrl("")
                    .contact(new Contact("niugang", "", "863263957@qq.com"))
                    .version("1.0")
                    .build();
        }
    
    }
    

                                                                            微信公众号

                                                   

                                                                                 JAVA程序猿成长之路

                              分享资源,记录程序猿成长点滴。专注于Java,Spring,SpringBoot,SpringCloud,分布式,微服务。 

  • 相关阅读:
    云计算架构
    Java多线程中static变量的使用
    Spring单例与线程安全小结
    sparkSQL实战详解
    sparkSQL整体实现框架
    spark架构
    如何快速定位出一个IP地址的归属地?——二分查找变体
    如何在 1000 万个整数中快速查找某个整数?——二分查找
    语言库中常用的排序算法qsort()底层结构
    链表常考笔试面试题(常备)
  • 原文地址:https://www.cnblogs.com/niugang0920/p/12186446.html
Copyright © 2020-2023  润新知