springboot整合swagger3.0生成在线文档

springboot整合swagger3.0生成在线文档

1 . 依赖

<!-- swagger 相关依赖 -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-boot-starter</artifactId>
            <version>3.0.0</version>
        </dependency>

2.0 添加swagger配置

import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.*;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
@Configuration
@EnableOpenApi
public class SwaggerConfig {
    @Bean
    public Docket api() {
        return new Docket(DocumentationType.OAS_30)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.hisign"))
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("基于Swagger3.0.0的接口文档")
                .description("api信息列表")
                .version("0.01")
                .contact(new Contact("", "", ""))
                .build();
    }
}

接口使用:
实体
springboot整合swagger3.0生成在线文档_第1张图片
接口
springboot整合swagger3.0生成在线文档_第2张图片
springboot整合swagger3.0生成在线文档_第3张图片

生成之后

**访问路径 , 固定: ip + 端口 + /swagger-ui/ **

http://localhost:5000/swagger-ui/

springboot整合swagger3.0生成在线文档_第4张图片
springboot整合swagger3.0生成在线文档_第5张图片
springboot整合swagger3.0生成在线文档_第6张图片
注意:
如果有用到拦截器,swagger生成文档时的请求会被拦截,所以会出现访问不到,所以需要放行

例如我本地是拦截校验token,需要添加放行

token校验

package com.hisign.interceptor;

import com.hisign.config.ApplicationContextUtils;
import com.hisign.enums.MessageEnum;
import com.hisign.rest.exception.HisignException;
import org.apache.poi.ss.formula.functions.T;
import org.checkerframework.checker.units.qual.A;
import org.checkerframework.checker.units.qual.min;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import sun.plugin2.message.Message;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.concurrent.TimeUnit;


/**
 * 拦截器,对token进行校验,并进行时间刷新
 */
public class TokenInterceptor implements HandlerInterceptor {

    // 日志信息
    private final Logger logger = LoggerFactory.getLogger(getClass());

    private static String ACCOUNT = "account:";

    //登录成功之后,储存在redis中的token信息为 -->  account:token
    @Override
    public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
        String token = request.getHeader("token");
        //token为空的情况
        if (token == null) {
            //token为空的情况,只有用户未正常退出,前端会清空token,重新登录生成   登录信息失效,请重新登陆!
            throw new HisignException(MessageEnum.LOGIN_INVALID);
        }

        StringRedisTemplate stringRedisTemplate = (StringRedisTemplate) ApplicationContextUtils.getBean("stringRedisTemplate");
        //redis中储存的用户token
        String yhToken = ACCOUNT + token;
        //携带了token,但是token已经失效,保存的用户信息已经清除
        String ryxx = stringRedisTemplate.opsForValue().get(yhToken);
        if (ryxx == null) {
            //登录信息失效,请重新登陆!
            throw new HisignException(MessageEnum.LOGIN_INVALID);
        }

        //过期时间 分钟
        Long expire = stringRedisTemplate.getExpire(yhToken, TimeUnit.MINUTES);
        logger.info( "当前token为 : "  + token +" , 还有 " + expire + " 分钟过期!");

        //进行token刷新过期时间 默认 5 分钟
        Boolean boo = stringRedisTemplate.expire(yhToken, 24, TimeUnit.HOURS);
        if (boo) {
            Long expire1 = stringRedisTemplate.getExpire(yhToken, TimeUnit.MINUTES);
            logger.info("token 刷新成功, 过期时间为 "+expire1+" 分钟!");
        } else {
            logger.info("token 刷新失败!");
        }
        //返回flase 不放行,true才放行
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {

    }

    @Override
    public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {

    }
}

拦截请求

package com.hisign.interceptor;

import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;


@Configuration
public class InterceptorConfig implements WebMvcConfigurer {
    /**
     * 设置拦截路径限制
     *
     * @param registry 设置对象
     */
    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        registry.addInterceptor(new TokenInterceptor())
                .excludePathPatterns("/csxx/exportCsxxExcel","/rjsc/**", "/yhscsc/**", "/login", "/updatePassword", "/swagger-resources/**", "/webjars/**", "/v3/**", "/swagger-ui/**", "/swagger/**", "/api-docs/**")
                .addPathPatterns("/**");
    }

}

springboot整合swagger3.0生成在线文档_第7张图片

你可能感兴趣的:(spring,boot,spring,java)