(一)隐藏某个controller的文档


直接在这个Controller类上面用@ApiIgnore注解即可,


(二)实体类注解


@ApiModel 作用于类上面
@ApiModelProperty(“用户名”) 作用于实体类的属性上的

(三)生产环境关闭Swagger文档


Swagger用于开发期间前端和后端API上的交流使用,在生产环境中我们应该关掉Swagger,如果生产环境不关掉swagger将是一件非常危险的事情。


方式一:在Swagger2Config上使用@Profile注解标识,@Profile({“dev”,”test”})表示在dev和test环境才能访问swagger-ui.html,prod环境下访问不了

方式二:在Swagger2Config上使用@ ConditionalOnProperty注解,@ConditionalOnProperty(name = “swagger.enable”, havingValue = “true”)表示配置文件中如果swagger.enable =true表示开启。所以只需要在开发环境的配置文件配置为true,生产环境配置为false即可。(方式2没试过,方式1有效果)

参考:
https://www.toutiao.com/i6799839869825712644/
https://www.yuque.com/docs/share/dec0f2c9-8b2b-4c27-8874-27a30bfee88d?#



 Controller层注解


下面两个注解是作用于方法上的, 回来研究下是否是有效果的

@ApiImplicitParams({
@ApiImplicitParam(name = “username”, value = “用户名”, dataType = ApiDataType.STRING, paramType = ApiParamType.QUERY),
@ApiImplicitParam(name = “password”, value = “密码”, dataType = ApiDataType.STRING, paramType = ApiParamType.QUERY),
})

(一)匹配多个Controller生成文档



https://www.yuque.com/docs/share/00cd8c54-2892-4167-bc25-a7a584509378?#

 Token信息

(一)每个方法设置一个token输入框


输入token的话,这样就可以在请求头里面通过 token 字段获取token的值了.


import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.Parameter;
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;

@EnableWebMvc
@EnableSwagger2
@Configuration
public class SwaggerWebMvcConfig implements WebMvcConfigurer {

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler(“swagger-ui.html”)
.addResourceLocations(“classpath:/META-INF/resources/“);

registry.addResourceHandler(“/webjars/“)
.addResourceLocations(“classpath:/META-INF/resources/webjars/“);
}

@Bean
public Docket buildDocket() {

return new Docket(DocumentationType.SWAGGER_2).groupName(“sj”)

.select()
.apis(RequestHandlerSelectors.withClassAnnotation(RestController.
class))
.paths(PathSelectors.any())
.build()
.globalOperationParameters(
this.setTokenConfiguration());

}
// 这里是设置token的主要方法
private List setTokenConfiguration() {
//请求头添加token信息
ParameterBuilder ticketPar =
new ParameterBuilder();
List pars =
new ArrayList();
ticketPar.name(“token”).description(“user ticket”)//Token 以及Authorization 为自定义的参数,session保存的名字是哪个就可以写成那个
.modelRef(
new ModelRef(“string”)).parameterType(“header”)
.required(
false).build(); //header中的ticket参数非必填,传空也可以
pars.add(ticketPar.build()); //根据每个方法名也知道当前方法在设置什么参数
return** pars;
}
}


效果:
Swagger[笔记] - 图1

(二)设置全局token


原文:
https://blog.csdn.net/zhangdaiscott/article/details/98491716

这样swagger 页面右边就会有个Authorize 按钮了.

package com.fancy.config.swagger;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import springfox.documentation.builders.ParameterBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.schema.ModelRef;
import springfox.documentation.service.ApiKey;
import springfox.documentation.service.AuthorizationScope;
import springfox.documentation.service.Parameter;
import springfox.documentation.service.SecurityReference;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spi.service.contexts.SecurityContext;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

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

@EnableWebMvc
@EnableSwagger2
@Configuration
public class SwaggerWebMvcConfig implements WebMvcConfigurer {

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
registry.addResourceHandler(“swagger-ui.html”)
.addResourceLocations(“classpath:/META-INF/resources/“);

registry.addResourceHandler(“/webjars/“)
.addResourceLocations(“classpath:/META-INF/resources/webjars/“);
}

@Bean
public Docket buildDocket() {

return new Docket(DocumentationType.SWAGGER_2).groupName(“sj”)

.select()
.apis(RequestHandlerSelectors.withClassAnnotation(RestController.
class))
.paths(PathSelectors.any())
.build()
.securitySchemes(securitySchemes())
.securityContexts(securityContexts());
}

private List securitySchemes() {
List apiKeyList =
new ArrayList();
apiKeyList.add(
new ApiKey(“token”, “token”, “header”));
return apiKeyList;
}


List defaultAuth() {
AuthorizationScope authorizationScope =
new AuthorizationScope(“global”, “accessEverything”);
AuthorizationScope[] authorizationScopes =
new AuthorizationScope[1];
authorizationScopes[0] = authorizationScope;
List securityReferences =
new ArrayList<>();
securityReferences.add(
new SecurityReference(“token”, authorizationScopes));
return securityReferences;
}

private List securityContexts() {
List securityContexts =
new ArrayList<>();
securityContexts.add(
SecurityContext.builder()
.securityReferences(defaultAuth())
.forPaths(PathSelectors.regex(“^(?!auth).*$”))
.build());
return** securityContexts;
}

}