Swagger3 新增自定义接口文档

文章介绍了如何在SpringBoot项目中使用Security时,由于login和logout接口由filter处理,导致Swagger不显示。作者提供了一个自定义ApiListingScannerPlugin,用于在Swagger文档中添加登录接口的详细描述。
摘要由CSDN通过智能技术生成

 当在spring boot项目中使用security时,项目的login,logout登录登出接口是使用filter实现的,swagger文档不会展示,那么我们就要使用自定义接口文档.

@Component
@Order(SwaggerPluginSupport.SWAGGER_PLUGIN_ORDER)
public class SwaggerAddtion  implements ApiListingScannerPlugin {


	@Override
	public List<ApiDescription> apply(DocumentationContext context) {
		List<ApiDescription> apiDescriptions = new ArrayList<>();
        //此处一定要加一个判断 不然每个组下都有该接口
		if (context.getGroupName().equals(Constants.ADMIN_API_SWAGGER_GROUP_NAME)) {
			apiDescriptions.add(new ApiDescription(
					Constants.ADMIN_API_SWAGGER_GROUP_NAME,
					Constants.LOGIN_URL,
					"用户登录",
					"login",
					Collections.singletonList(userLogin()),
					false
			));
		}
		return apiDescriptions;
	}

	private Operation userLogin() {
		List<RequestParameter> list = new ArrayList<>();
		list.add(new RequestParameterBuilder()
				.name("password")
				.description("密码")
				.required(true)
				.in(ParameterType.QUERY)
				.build()
		);
		list.add(new RequestParameterBuilder()
				.name("username")
				.description("用户名")
				.required(true)
				.in(ParameterType.QUERY)
				.build()
		);

		Set<String> consumes = new HashSet<>();
		consumes.add(MediaType.APPLICATION_FORM_URLENCODED_VALUE);
		Set<String> produces = new HashSet<>();
		produces.add(MediaType.APPLICATION_JSON_VALUE);
		Set<String> tags = new HashSet<>();
		tags.add("用户登录");
		return new OperationBuilder(new CachingOperationNameGenerator())
				.method(HttpMethod.POST)
				.uniqueId("用户登录2")
				.summary("用户登录接口")
				.consumes(consumes)
				.produces(produces)
				.tags(tags)
				.requestParameters(list)
				.responses(Collections.singleton(
						new ResponseBuilder().code("200").description("success").build()))
				.build();
	}

	@Override
	public boolean supports(DocumentationType delimiter) {
		return DocumentationType.OAS_30.equals(delimiter);
	}
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Swagger是一种API文档生成工具,可以帮助开发人员自动生成API文档。Swagger3是Swagger的最新版本,它提供了更多的功能和更好的用户体验。下面是使用Swagger3生成接口文档的步骤: 1.在pom.xml文件中添加Swagger3的依赖: ```xml <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-boot-starter</artifactId> <version>3.0.0</version> </dependency> ``` 2.添加Swagger配置类: ```java @Configuration @EnableSwagger2WebMvc public class SwaggerConfig { @Bean public Docket api() { return new Docket(DocumentationType.SWAGGER_2) .select() .apis(RequestHandlerSelectors.basePackage("com.example.demo.controller")) .paths(PathSelectors.any()) .build() .apiInfo(apiInfo()); } private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("API文档") .description("这是一个API文档示例") .version("1.0.0") .build(); } } ``` 3.在Controller中添加Swagger接口注解: ```java @RestController @RequestMapping("/api") @Api(tags = "用户管理") public class UserController { @GetMapping("/users") @ApiOperation("获取用户列表") public List<User> getUsers() { // ... } @PostMapping("/users") @ApiOperation("创建用户") public void createUser(@RequestBody User user) { // ... } @GetMapping("/users/{id}") @ApiOperation("获取用户信息") public User getUserById(@PathVariable Long id) { // ... } @PutMapping("/users/{id}") @ApiOperation("更新用户信息") public void updateUser(@PathVariable Long id, @RequestBody User user) { // ... } @DeleteMapping("/users/{id}") @ApiOperation("删除用户") public void deleteUser(@PathVariable Long id) { // ... } } ``` 4.启动应用程序并访问http://localhost:8080/swagger-ui/index.html,即可查看和测试接口
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值