Spring Boot中Swagger的基础应用

tech2022-12-18  112

1、在pom.xml中添加基础依赖 2、创建config/Swagger2Config文件 3、在文件内添加代码

package com.example.demo.config; //import 导入的包,最好是用代码内的自动导包导入 import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import springfox.documentation.builders.ApiInfoBuilder; import springfox.documentation.builders.PathSelectors; import springfox.documentation.builders.RequestHandlerSelectors; import springfox.documentation.service.ApiInfo; import springfox.documentation.spi.DocumentationType; import springfox.documentation.spring.web.plugins.Docket; import springfox.documentation.swagger2.annotations.EnableSwagger2; @Configuration @EnableSwagger2 public class Swagger2Config { @Bean public Docket createRestApi(){ return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .select() //为当前包下controller生成API文档 .apis(RequestHandlerSelectors.basePackage("com.example.demo.controller"))//(此处为你工程内controller文件内UserContreller这类文件内的的地址,最好是直接复制粘贴过来,看下图) //为有@Api注解的Controller生成API文档 // .apis(RequestHandlerSelectors.withClassAnnotation(Api.class)) //为有@ApiOperation注解的方法生成API文档 // .apis(RequestHandlerSelectors.withMethodAnnotation(ApiOperation.class)) .paths(PathSelectors.any()) .build(); } private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("SwaggerUI演示") .description("内蒙古电子大数据工坊") .contact("haoyu") .version("1.0") .build(); } }

然后运行工程斜体样式 在网页打开***http://localhost:8080/swagger-ui.html*** 点击show/Hide 出现成功

最新回复(0)