spring-boot之接口文档Swagger配置使用

Swagger

前后端分离

Vue + SpringBoot

后端时代:前端只用管理静态页面; html==> 后端。模板引擎JSP =>后端是主力

前后端分离式时代:

●后端:后端控制层,服务层,数据访问层[后端团队]

●前端:前端控制层,视图层[前端团队]

。伪造后端数据,json。 已经存在了,不需要后端,前端工程依旧能够跑起来

●前端后如何交互? ===> API

●前后端相对独立,松耦合;

●前后端甚至可以部署在不同的服务器上;

I

官网https://swagger.io/

在项目使用Swagger需要springbox;

●swagger2

●ui

SpringBoot集成Swagger

1,新建一个springboot-web项目

2,导入maven

复制代码
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger-ui -->
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger-ui</artifactId>
    <version>3.0.0</version>
</dependency>

<!-- https://mvnrepository.com/artifact/io.springfox/springfox-swagger2 -->
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-swagger2</artifactId>
    <version>3.0.0</version>
</dependency>

spring boot老版本使用以上配置

或者

复制代码
<!-- https://mvnrepository.com/artifact/io.springfox/springfox-boot-starter -->
<dependency>
    <groupId>io.springfox</groupId>
    <artifactId>springfox-boot-starter</artifactId>
    <version>3.0.0</version>
</dependency>

3.编写一-个Hello工程

4.配置Swagger ==> Config

复制代码
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
import springfox.documentation.oas.annotations.EnableOpenApi;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableOpenApi
public class SwaggerConfig {
}

访问地址http://localhost:8080/swagger-ui/

使用swagger3出现Failed to start

bean'documentationPluginsBootstrapper 错误,还可以在application.properties里面加"spring.mvc.pathmatch.matching-strategy= ANT_PATH_MATCHER"

配置Swagger信息

默认调用的是ApiInfo里面的

this.apiInfo = ApiInfo.DEFAULT;

复制代码
public static final Contact DEFAULT_CONTACT = new Contact("", "", "");

static {
    DEFAULT = new ApiInfo("Api Documentation", "Api Documentation", "1.0", "urn:tos", DEFAULT_CONTACT, "Apache 2.0", "http://www.apache.org/licenses/LICENSE-2.0", new ArrayList());
}
java 复制代码
@Configuration
@EnableOpenApi
public class SwaggerConfig {


    //配置了Swagger的Docker的bean实例,替换默认值  this.apiInfo = ApiInfo.DEFAULT;
    
   /* static {
        DEFAULT = new ApiInfo("Api Documentation", "Api Documentation", "1.0", "urn:tos", DEFAULT_CONTACT, "Apache 2.0", "http://www.apache.org/licenses/LICENSE-2.0", new ArrayList());
    }*/
    @Bean
    public Docket docket(){

        return new Docket(DocumentationType.OAS_30).apiInfo(apiInfo());
    }

    //配置Swagger信息=apiInfo
    private ApiInfo apiInfo(){
        //作者信息
        Contact contact = new Contact("陈平安", "urn:tos", "2188671488.com");
        return new ApiInfo("陈平安的Swagger API文档",
                "有不开心,也有在努力生活",
                "1.0",
                "urn:tos",
                contact,
               /* DEFAULT_CONTACT,*/
                "Apache 2.0",
                "http://www.apache.org/licenses/LICENSE-2.0",
                new ArrayList());
    }
}

Swagger配置扫描接口

Docket.select()

复制代码
@Bean
public Docket docket(){

    return new Docket(DocumentationType.OAS_30).
            apiInfo(apiInfo()).
            select()
            //requestHandler ->
                    //RequestHandlerSelectors 配置要扫描接口的方式
                            //basePackage("")指定扫描的包
                            //any()扫描全部
                            //none()都不扫描
                            //withClassAnnotation 扫描类上的注解 withClassAnnotation.(RestController.class)
                            //withMethodAnnotation 扫描方法上的注解 withMethodAnnotation.(GetMapping.class)
           .
             apis(RequestHandlerSelectors.basePackage("com.controller")).
    //过滤什么路径
            paths(PathSelectors.ant("/com/**")).
            build();
}

配置是否启动swagger

复制代码
@Bean
public Docket docket(){

    return new Docket(DocumentationType.OAS_30).
            apiInfo(apiInfo()).
            enable(true).//enable是否启动swagger, 如果为False, 则swagger不能再浏览器中访问
                    select()
            //requestHandler ->
                    //RequestHandlerSelectors 配置要扫描接口的方式
                            //basePackage("")指定扫描的包
                            //any()扫描全部
                            //none()都不扫描
                            //withClassAnnotation 扫描类上的注解 withClassAnnotation.(RestController.class)
                            //withMethodAnnotation 扫描方法上的注解 withMethodAnnotation.(GetMapping.class)
           .
             apis(RequestHandlerSelectors.basePackage("com.controller")).
    //过滤什么路径
            //paths(PathSelectors.ant("/com/**")).
            build();
}
我只希望我的Swagger在生产环境中使用,在发布的时候不使用

●判断是不是生产环境flag = false

●注入enable (flag)

复制代码
@Bean                    //获取项目环境
public Docket docket(Environment environment){


    //设置要显示的Swagger环境
    Profiles profiles = Profiles.of("dev","test");

    //environment.acceptsProfiles 判断是否处在自己设定的环境当中
    boolean flag = environment.acceptsProfiles(profiles);
    return new Docket(DocumentationType.OAS_30).
            apiInfo(apiInfo()).
            //enable(true).//enable是否启动swagger, 如果为False, 则swagger不能再浏览器中访问
            enable(flag).
                    select()
           .apis(RequestHandlerSelectors.basePackage("com.controller")).
            build();
}
application.properties
复制代码
spring.profiles.active=dev

applicationdev.properties

复制代码
server.port=8081

配置API文档分组

复制代码
groupName("陈平安")
配置多个分组:多个Docket实例既可
复制代码
@Bean
public Docket docket1(){
    return  new Docket(DocumentationType.OAS_30).groupName("A");
}
@Bean
public Docket docket2(){
    return  new Docket(DocumentationType.OAS_30).groupName("B");
}
@Bean
public Docket docket3(){
    return  new Docket(DocumentationType.OAS_30).groupName("C");
}
实体类配置

@Api(tags = "")

类注释

@ApiModel("")

方法注释

@ApiOperatio

复制代码
//@Api(注释)
@ApiModel("用户类")
public class User {

    @ApiModelProperty("用户名")
    public  String username;
    @ApiModelProperty("密码")
    public String password;





}
controller
复制代码
@Api(tags = "HelloController控制类")
@RestController
public class HelloController {

    @GetMapping( value = "/hello")
    public  String hello(){
        return "hello";
    }

    //只要我们的接口中,返回值中存在实体类,他就会被扫描到swagger中
    @GetMapping( value = "/user")
    public User user(){
        return new User();
    }

 //Operation接口,不是放在类上的,是方法
   // @ApiOperation("hello控制类")
    @GetMapping(value = "/hello2")
    public  String hello2(@ApiParam("用户名") String  username){
        return "hello" + username;
    }



    @PostMapping( value = "/postt")
    public  User postt(@ApiParam("用户名") User user){
        return  user;
    }
相关推荐
橘子编程1 天前
编程语言全指南:从C到Rust
java·c语言·开发语言·c++·python·rust·c#
艾莉丝努力练剑1 天前
【Linux线程】Linux系统多线程(三):Linux线程 VS 进程,线程控制
java·linux·运维·服务器·c++·学习·ubuntu
小白天下第一1 天前
java+三角测量(两个工业级)+人体3d骨骼关键点获取(yolov8+HRNET_w48_2d)
java·yolo·3d·三角测量
William Dawson1 天前
Java 后端高频 20 题超详细解析 ①
java·开发语言
编程之升级打怪1 天前
Java NIO的简单封装
java·开发语言·nio
wuxinyan1231 天前
Java面试题46:一文深入了解JVM 核心知识体系
java·jvm·面试题
小江的记录本1 天前
【JEECG Boot】 《JEECG Boot 数据字典使用教程》(完整版)
java·前端·数据库·spring boot·后端·spring·mybatis
鲸渔1 天前
【C++ 变量与常量】变量的定义、初始化、const 与 constexpr
java·开发语言·c++
i220818 Faiz Ul1 天前
教育资源共享平台|基于springboot + vue教育资源共享平台系统(源码+数据库+文档)
java·数据库·vue.js·spring boot·论文·毕设·教育资源共享平台
玛卡巴卡ldf1 天前
【Springboot7】ApachePOI文件导入导出
java·spring boot·sql