Swagger分组和接口注释
1. swagger分组(只需要创建几个Docket类型的方法即可)
package com.Google.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 org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.servlet.config.annotation.EnableWebMvc; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; import springfox.documentation.builders.PathSelectors; 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; import static springfox.documentation.spi.DocumentationType.SWAGGER_2; @Configuration //将这个类交给springBoot托管 @EnableSwagger2 //启用Swagger2 @EnableWebMvc public class SwaggerConfig implements WebMvcConfigurer { @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/**").addResourceLocations( "classpath:/static/"); registry.addResourceHandler("swagger-ui.html").addResourceLocations( "classpath:/META-INF/resources/"); registry.addResourceHandler("/webjars/**").addResourceLocations( "classpath:/META-INF/resources/webjars/"); WebMvcConfigurer.super.addResourceHandlers(registry); } //配置多组信息(一个Docket为一个组) @Bean public Docket docket1(){ return new Docket(SWAGGER_2).groupName("A"); } @Bean public Docket docket2(){ return new Docket(SWAGGER_2).groupName("B"); } @Bean public Docket docket3(){ return new Docket(SWAGGER_2).groupName("C"); } //配置Swagger信息 @Bean public Docket docket(Environment environment){ //设置要显示的swagger环境 Profiles profils = Profiles.of("dev"); //通过 environment.acceptsProfiles判断自己是否处于自己设定的环境 boolean flag = environment.acceptsProfiles(profils); return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()) .groupName("小落") .enable(flag)//关闭swagger 这里我们可以根据生产环境,选择开启或者关闭swagger .select() //RequestHandlerSelectors:配置要扫描的包 //(一般就扫描basePackage)basePackage:指定要扫描的包 //any():全扫描 //none():全部扫描 //withMethodAnnotation():扫描方法上的注解 参数是注解的反射 .apis(RequestHandlerSelectors.basePackage("com.Google.controller")) //过滤的路径(不扫描)这里的路径是指包的位置,而不是url //ant:过滤指定的路径 //any():过滤所有路径 //none():不过滤 //regex():正则表达 .paths(PathSelectors.ant("/Google/*")) .build(); } private ApiInfo apiInfo(){ Contact contact = new Contact("小落", "", "2034281742@qq.com"); //作者信息(Contact 联络) return new ApiInfo("小落的API", //文档的标头 "这个API用来测试", //文档的描述 "1.0", //文档的版本号 "urn:tos", contact, "Apache 2.0", "http://www.apache.org/licenses/LICENSE-2.0", new ArrayList() ); } }
注意需要@bean,交给springBoot接管
2.swagger注释
用于实体类
@ApiModel("用户实体类")
用于实体类参数
@ApiModelProperty("用户名")
package com.Google.pojo; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; import io.swagger.annotations.ApiOperation; import lombok.AllArgsConstructor; import lombok.Data; import lombok.NoArgsConstructor; @Data @AllArgsConstructor @NoArgsConstructor @ApiModel("用户实体类") public class user { @ApiModelProperty("用户名") String username; @ApiModelProperty("密码") String password; }
用于接口方法
@ApiOperation("userpost控制") //只放在接口上有用
用于接口参数
@ApiParam("用户名")
package com.Google.controller; import com.Google.pojo.user; import io.swagger.annotations.ApiOperation; import io.swagger.annotations.ApiParam; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class helloSwagger { @GetMapping("/hello") public String hello(){ return "你好,Swagger"; } //只要我们的接口中,返回值是实体类,它就会被Swagger扫描 @PostMapping("/postuser") @ApiOperation("userpost控制") //只放在接口上有用 public user postuser(@ApiParam("用户实体类参数") user user){ return user; } @GetMapping("/hello1") public String hello1(@ApiParam("用户名") String username){ return "你好,Swagger"+username; } }
小结;
- 在项目上线时Swagger要注意关闭,节省运行内存
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?