sprigboot--组合 swagger

maven 依赖

1
2
3
4
5
6
7
8
9
10
11
<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>

  

添加swagger配置文件

1
2
3
4
5
6
7
8
9
10
11
package com.config;
 
import org.springframework.context.annotation.Configuration;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
 
//配置到springboot
@Configuration
//开启swagger---页面地址 /swagger-ui.html
@EnableSwagger2
public class SwaggerConfig {
}

  

 

 ----------------------------------------------------以上swagger就启动起来了--------------------------------------------------------------------

 

配置swagger

1、配置网页展示的信息

 

 

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
package com.config;
 
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
 
import java.util.ArrayList;
 
//配置到springboot
@Configuration
//开启swagger---页面地址 /swagger-ui.html
@EnableSwagger2
public class SwaggerConfig {
    //配置了Swagger 的Docket的bean实例
    @Bean
    public Docket docket(){
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo());
    }
 
    //配置Swagger信息=apiinfo
    private ApiInfo apiInfo(){
//        作者信息A
        Contact contact =new Contact("刘锴傧https://www.cnblogs.com/kaibindirver/p/15025758.html",
                "https://i.cnblogs.com/posts/edit-done;postId=14948036",
                "tomcat8888@163.com");
        return new ApiInfo(
                "凯宾斯基SwaggerAPi文档",
                "这个是我文档的描述",
                "v1.01",
                "https://i.cnblogs.com/posts/edit-done;postId=14948036",
                contact,
                "Apache 2.0",
                "http://www.apache.org/licenses/LICENSE-2.0",
                new ArrayList());
 
    }
}

 

2、配置扫描接口-和判断当前环境 是否开启 swagger网站访问

设置2个不同环境的配置文件见:  https://www.cnblogs.com/kaibindirver/p/15025758.html

复制代码
package com.config;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

import java.util.ArrayList;

//配置到springboot
@Configuration
//开启swagger---页面地址 /swagger-ui.html
@EnableSwagger2
public class SwaggerConfig {
    //配置了Swagger 的Docket的bean实例
    @Bean
    public Docket docket(Environment environment){

//        设置要显示的Swagger环境
        Profiles profiles= Profiles.of("dev","test");
//获取当前的环境 boolean flag = environment.acceptsProfiles(profiles); return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) //控制是否开启swagger 设置为false 浏览器就无法访问 .enable(flag) .select() // RequestHandlerSelectors配置要扫描接口的方式,下面是扫描包 .apis(RequestHandlerSelectors.basePackage("com.control")) // none()不扫描,打开swagger是空的 // .apis(RequestHandlerSelectors.none()) //只扫描/kai开头的接口 // .paths(PathSelectors.ant("/kai/**")) .build(); }
//配置Swagger信息=apiinfo private ApiInfo apiInfo(){ // 作者信息A Contact contact =new Contact("刘锴傧", "https://i.cnblogs.com/posts/edit-done;postId=14948036", "tomcat8888@163.com"); return new ApiInfo( "凯宾斯基SwaggerAPi文档", "这个是我文档的描述", "v1.01", "https://i.cnblogs.com/posts/edit-done;postId=14948036", contact, "Apache 2.0", "http://www.apache.org/licenses/LICENSE-2.0", new ArrayList()); } }
复制代码

 

设置分组 的方法 加多一个bean

复制代码
package com.config;
 
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.env.Environment;
import org.springframework.core.env.Profiles;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;
 
import java.util.ArrayList;
 
//配置到springboot
@Configuration
//开启swagger---页面地址 /swagger-ui.html
@EnableSwagger2
public class SwaggerConfig {
 
    //配置了Swagger 的Docket的bean实例
    @Bean
    public Docket docket(Environment environment){
 
//        设置要显示的Swagger环境
        Profiles profiles= Profiles.of("dev","test");
        boolean flag = environment.acceptsProfiles(profiles);
 
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
//                设置分组
                .groupName("分组-01")
                //控制是否开启swagger 设置为false 浏览器就无法访问
                .enable(flag)
                .select()
//        RequestHandlerSelectors配置要扫描接口的方式,下面是扫描包
                .apis(RequestHandlerSelectors.basePackage("com.control"))
                //        none()不扫描,打开swagger是空的
//                .apis(RequestHandlerSelectors.none())
                //只扫描/kai开头的接口
//                .paths(PathSelectors.ant("/kai/**"))
                .build();
    }
 
 
    //下面是再创建多一个相同的函数,实现接口分组
    @Bean
    public Docket docket2(Environment environment){
 
//        设置要显示的Swagger环境
        Profiles profiles= Profiles.of("dev","test");
        boolean flag = environment.acceptsProfiles(profiles);
 
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
//                设置分组
                .groupName("分组-02")
                .enable(flag)
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.control"))
                .build();
    }
 
    //配置Swagger信息=apiinfo
    private ApiInfo apiInfo(){
//        作者信息A
        Contact contact =new Contact("刘锴傧",
                "https://i.cnblogs.com/posts/edit-done;postId=14948036",
                "tomcat8888@163.com");
        return new ApiInfo(
                "凯宾斯基SwaggerAPi文档",
                "这个是我文档的描述",
                "v1.01",
                "https://i.cnblogs.com/posts/edit-done;postId=14948036",
                contact,
                "Apache 2.0",
                "http://www.apache.org/licenses/LICENSE-2.0",
                new ArrayList());
 
    }
}
复制代码

 

 

 

给接口的请求参数 和 接口命名

给返回的实体类或post接受映射的实体类命名

 

 

复制代码
package com.dao.entity;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;

@NoArgsConstructor
@AllArgsConstructor
@ToString
@Data //注意要有get、set方法
@ApiModel("用户实体类")
public class User {
    @ApiModelProperty("用户名")
    private String name;
    @ApiModelProperty("用户年龄")
    private String age;
}
复制代码

 

 

给接口命名

 

 

复制代码
import com.dao.entity.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.web.bind.annotation.*;


@RestController
@Api(tags = "用户相关接口", description = "提供用户相关的 Rest API")
public class Swagger {

    @ApiOperation("post请求提交用户信息")
    @PostMapping("/aaa")
    public User delete_User3 (@ApiParam("用户信息") @RequestBody User user) {
        return new User();
    }

    @ApiOperation("get请求获取用户信息")
    @GetMapping("/bbb")
    public User delete_User4 (
            @ApiParam(name="用户名",required = true) @RequestParam(value="name") String name,
            @ApiParam("性别") @RequestParam(value="sex") int sex) {
        return new User();
    }
}
复制代码

 

可以参考下 : https://blog.csdn.net/zhongzk69/article/details/94987568 (里面有统一配置400 500 的接口响应)

以上看得是狂神的: https://www.bilibili.com/video/BV1Y441197Lw?from=search&seid=17321077544629926329

posted @   凯宾斯基  阅读(47)  评论(0编辑  收藏  举报
编辑推荐:
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
阅读排行:
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 使用C#创建一个MCP客户端
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列1:轻松3步本地部署deepseek,普通电脑可用
· 按钮权限的设计及实现
历史上的今天:
2020-06-28 条件(三元)运算符
2020-06-28 js的数组
2020-06-28 js函数
点击右上角即可分享
微信分享提示