window.cnblogsConfig = { blogUser: 'MoYu', blogAvatar: 'https://gitee.com/MoYu-zc/picgo/raw/master/img/20210213094450.jpg', blogStartDate: '2020-02-09', webpageTitleOnblur: '(o゚v゚)ノ Hi,Back', webpageTitleOnblurTimeOut: 500, webpageTitleFocus: '(*´∇`*) 欢迎回来!', webpageTitleFocusTimeOut: 1000, webpageIcon: "https://gitee.com/MoYu-zc/picgo/raw/master/img/20210213094450.jpg", enable: true, // 是否开启日/夜间模式切换按钮 auto: { // 自动切换相关配置 enable: false, // 开启自动切换 dayHour: 7, // 日间模式开始时间,整数型,24小时制 nightHour: 20 // 夜间模式开始时间,整数型,24小时制 } switchDayNight: { enable: true, auto: { enable: true } }, progressBar: { id : 'top-progress-bar', // 请勿修改该值 color : '#77b6ff', height : '2px', duration: 0.2, }, loading: { rebound: { tension: 16, friction: 5, }, spinner: { id: 'spinner', radius: 90, sides: 3, depth: 4, colors: { background: '#f0f0f0', stroke: '#272633', base: null, child: '#272633', }, alwaysForward: true, // When false the spring will reverse normally. restAt: 0.5, // A number from 0.1 to 0.9 || null for full rotation renderBase: false, } }, homeTopAnimationRendered: true, homeTopAnimation: { radius: 15, density: 0.2, color: 'rgba(255,255,255, .2)', // 颜色设置,“random” 为随机颜色 clearOffset: 0.3, }, essayTopAnimationRendered: true, essayTopAnimation: { triW : 14, triH : 20, neighbours : ["side", "top", "bottom"], speedTrailAppear : .1, speedTrailDisappear : .1, speedTriOpen : 1, trailMaxLength : 30, trailIntervalCreation : 100, delayBeforeDisappear : 2, colorsRandom: false, // v1.2.4 是否开启随机颜色 colors: [ '#96EDA6', '#5BC6A9', '#38668C', '#374D84', '#BED5CB', '#62ADC6', '#8EE5DE', '#304E7B' ] }, homeTopImg: [ "https://cdn.jsdelivr.net/gh/BNDong/Cnblogs-Theme-SimpleMemory@master/img/webp/home_top_bg.webp", "https://cdn.jsdelivr.net/gh/BNDong/Cnblogs-Theme-SimpleMemory@master/img/webp/home_top_bg.webp" ], homeBannerTextType: "one", essayTopImg: [ "https://cdn.jsdelivr.net/gh/BNDong/Cnblogs-Theme-SimpleMemory@master/img/webp/nothome_top_bg.webp", "https://cdn.jsdelivr.net/gh/BNDong/Cnblogs-Theme-SimpleMemory@master/img/webp/nothome_top_bg.webp", "https://gitee.com/MoYu-zc/picgo/raw/master/img/20210208190902.jpg", "https://gitee.com/MoYu-zc/picgo/raw/master/img/20210208190954.jpg", ], codeMaxHeight: true, codeLineNumber: true, essayCode: { fontFamily: "'Ubuntu Mono',monospace", // 代码框字体 fontSize: "14px" // 代码框字体大小 }, }

SpringBoot-10 Swagger

SpringBoot-10 Swagger

Swagger基础搭建

1.导入依赖

<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>2.9.2</version>
</dependency>
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>2.9.2</version>
</dependency>

2.创建SwaggerConfig

@Configuration
@EnableSwagger2   //开启Swagger2
public class SwaggerConfig {
}

3.进行测试

这里要注意测试网址:http://localhost:8080/swagger-ui.html

1

Swagger配置设置

配置文件都在SwaggerConfig中:

//配置了Swagger的Docket的bean实例
@Bean
public Docket docket(){
    return new Docket(DocumentationType.SWAGGER_2)
        .apiInfo(apiInfo());
}
private ApiInfo apiInfo(){
    Contact DEFAULT_CONTACT = new Contact("zc", "https://www.moyuzc.cn/", "moyu_zc@163.com");
    return new ApiInfo(
        "Swagger2 Test",                                //标题
        "zc第一个测试",                                   //描述
        "1.0",                                          //版本号
        "https://www.moyuzc.cn/",                       //网址
        DEFAULT_CONTACT,                                //上方设置的信息,名称、网址、邮箱地址
        "Apache 2.0",                                   //版权
        "http://www.apache.org/licenses/LICENSE-2.0",   
        new ArrayList());                              
}

进行测试:

2

扫描接口及开关

创建两个配置接口,dev、test

application.properties

spring.profiles.active=dev

application-dev.properties

server.port=8081

application-test.properties

server.port=8082

设置要显示的Swagger环境

Profiles of = Profiles.of("dev","test");

environment.acceptsProfiles(of) 判断是否处于自己设定的环境

boolean flag = environment.acceptsProfiles(of); flag = environment.acceptsProfiles(of);

通过flag返回的boolean值判断能否进入

return new Docket(DocumentationType.SWAGGER_2)
    .apiInfo(apiInfo())
    .enable(flag)
    .select()
    .apis(RequestHandlerSelectors.basePackage("com.zc.controller"))
    .build();
3

4

分组

1.配置分组名称

groupName("zczc") 使用这个方法,可以更改分组名称

return new Docket(DocumentationType.SWAGGER_2)
    .apiInfo(apiInfo())
    .groupName("zczc")
    .enable(flag)
    .select()
    .apis(RequestHandlerSelectors.basePackage("com.zc.controller"))
    .build();
5

2.配置多个分组

另外增加A、B、C分组

@Bean
public Docket docket1(){
    return new Docket(DocumentationType.SWAGGER_2)
        .groupName("A");
}
@Bean
public Docket docket2(){
    return new Docket(DocumentationType.SWAGGER_2)
        .groupName("B");
}
@Bean
public Docket docket3(){
    return new Docket(DocumentationType.SWAGGER_2)
        .groupName("C");
}
6

接口注释

1.初步配置

创建实体类

public class User {
    public String username;
    public String password;
}

修改Controller类

@PostMapping("/user")
public User user(){
    return new User();
}

因为在之前,SwaggerConfig类中已经扫描了Controller包,所以在分组正确的情况下,可以直接测试

7

2.添加接口注释

实体类添加注释

@ApiModel("用户实体类")
public class User {
    @ApiModelProperty("用户名")
    public String username;
    @ApiModelProperty("密码")
    public String password;
}

8

Controller类加入注释

@ApiOperation("Hello2方法")
@GetMapping("/hello2")
public String hello2(@ApiParam("用户名") String username){
    return "hello"+username;
}
9

个人博客为:
MoYu's HomePage
MoYu's Gitee Blog

posted @ 2021-04-02 11:47  MoYu-zc  阅读(67)  评论(0编辑  收藏  举报