swagger入门级各种踩坑

在项目中使用 swagger,一上来就说原理的摆明了就是耍流氓,并鞭策自己向大佬看齐!

1. 使用

1)配置文件  --- SwaggerConfig

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.service.Parameter;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

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

@Configuration
@EnableSwagger2
public class SwaggerConfig {

    /**
     * 创建api应用
     * apiInfo() 增加API相关信息
     * 通过select() 函数返回一个ApiSelectorBuilder实例,用来控制哪写接口暴露给swagger
     * 本例采用指定扫描的包路径来定义指定要建立API的目录
     */
    @Bean
    public Docket createRestApi(){
        ParameterBuilder tokenPar = new ParameterBuilder();
        List pars = new ArrayList<>();
        tokenPar.name("token").description("令牌").
                modelRef(new ModelRef("string")).
                parameterType("query").required(false);
        pars.add(tokenPar.build());
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.company.project")) //自己的包路径
                .paths(PathSelectors.any())
                .build().globalOperationParameters(pars);
    }

    /**
     * 创建该API的基本信息(这些基本信息会展现在文档页面中)
     * 访问地址:http://
     * @return
     */
    @SuppressWarnings("deprecation")
    private ApiInfo apiInfo(){
        return new ApiInfoBuilder()
                .title("测试")
                .description("")
                .termsOfServiceUrl("")
                .contact("测试")
                .version("1.0")
                .build();
    }
}

 2)  继承 WebMvcConfigurerAdapter

@Configuration
public class WebMvcConfigurer extends WebMvcConfigurerAdapter{
 @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry){
          registry.addResourceHandler("swagger-ui.html") //对外暴露的访问路径
                  .addResourceLocations("classpath:/META-INF/resources/");  //该html放置位置
          registry.addResourceHandler("/webjars/**")
                  .addResourceLocations("classpath:/META-INF/resources/webjars/");
    }
}

3) 测试

  @ApiOperation(value = "登录")
  @ApiImplicitParams({
            @ApiImplicitParam(paramType="query",name = "username", value = "用户名",required = true, dataType = "String"),
            @ApiImplicitParam(paramType="query",name = "password", value = "密码",required = true, dataType = "String")
    })
    @AuthInterceptor(InterceptorLevel.NONE)
    @PutMapping("/login")
    public ServerResponse login( String username, String password) {
        System.out.println("用户名"+username);
        System.out.println("密码"+password);
        ServerResponse response = userService.login(username, password);
        if (response.isSuccess()) {
            request.getSession().setAttribute(ValueConsts.USER_STRING, response.getData());
        }
        return response;
    }

 

 

你可能感兴趣的:(java学习)