Python 官方文档:入门教程 => 点击学习
目录一、创建SpringBoot项目,引入pom依赖二、配置类请求头携带token三、配置文件四、接口定义五、实现类六、实体类定义七、运行项目查看效果一、创建springboot项目
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-WEB</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<version>1.18.12</version>
</dependency>
<!-- 只需要引入这一个依赖就行了 -->
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-ui</artifactId>
<version>1.5.5</version>
</dependency>
import io.swagger.v3.oas.annotations.ExternalDocumentation;
import io.swagger.v3.oas.annotations.OpenAPIDefinition;
import io.swagger.v3.oas.annotations.enums.SecuritySchemeIn;
import io.swagger.v3.oas.annotations.enums.SecuritySchemeType;
import io.swagger.v3.oas.annotations.info.Contact;
import io.swagger.v3.oas.annotations.info.Info;
import io.swagger.v3.oas.annotations.security.SecurityRequirement;
import io.swagger.v3.oas.annotations.security.SecurityScheme;
@OpenAPIDefinition(
info = @Info(
title = "Swagger3",
version = "1.0",
description = "Swagger3使用演示",
contact = @Contact(name = "TOM")
),
security = @SecurityRequirement(name = "Jwt"),
externalDocs = @ExternalDocumentation(description = "参考文档",
url = "https://GitHub.com/swagger-api/swagger-core/wiki/Swagger-2.X---Annotations"
)
)
@SecurityScheme(type = SecuritySchemeType.Http, name = "JWT", scheme = "bearer", in = SecuritySchemeIn.HEADER)
public class Swagger3Config {
}
server:
port: 8080
springdoc:
api-docs:
#是否开启文档功能
enabled: true
#swagger后端请求地址
path: /api-docs
swagger-ui:
#自定义swagger前端请求路径,输入http:127.0.0.1:8080/test会自动重定向到swagger页面
path: /test
#包扫描路径
packages-to-scan: com.hello.controller,com.hello.dto
#这里定义了两个分组,可定义多个,也可以不定义
group-configs:
#分组名
- group: admin
#按路径匹配
pathsToMatch: /admin
public static <T> CommonResult<T> success(T data) {
return new CommonResult<T>(200, "操作成功", data);
}
public static <T> CommonResult<T> failed(String message) {
return new CommonResult<T>(400, message, null);
}
}
package com.hello.dto;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Data;
@Schema(name="User",description ="用户信息" )
@Data
@AllArgsConstructor
public class User {
@Schema(name = "name",description = "姓名")
private String name;
@Schema(name = "age",description = "年龄")
private int age;
}
浏览器输入127.0.0.1:8080/test会重定向到swagger页面
点击右上角的Authorize就会弹出以下界面,输入token,请求接口就会自动携带该token发送请求,这里随便输入一个abc为token,点击Authorize
打开一个接口去测试,查看效果,发现请求已经自动携带了token
到此这篇关于Springboot整合Swagger3全注解配置(springdoc-openapi-ui)的文章就介绍到这了,更多相关Springboot Swagger3全注解配置内容请搜索编程网以前的文章或继续浏览下面的相关文章希望大家以后多多支持编程网!
--结束END--
本文标题: Springboot整合Swagger3全注解配置(springdoc-openapi-ui)
本文链接: https://lsjlt.com/news/142387.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-03-01
2024-03-01
2024-03-01
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
2024-02-29
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0