1,引入Swagger2相关依赖
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.9.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.9.2</version>
</dependency>
2,启动Swagger2
@SpringBootApplication
@EnableSwagger2
public class SwaggerTestApplication {
public static void main(String[] args) {
SpringApplication.run(SwaggerTestApplication.class, args);
}
}
3,启动后的访问地址
http://localhost:8080/swagger-ui.html
4,Swagger的基本配置
添加@Configuration注解
加入spring容器@Bean
package com.serlyf.config;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import com.google.common.base.Predicates;
import com.serlyf.anno.MyAnnotation4Swagger;
import springfox.documentation.builders.ApiInfoBuilder;
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;
@Configuration
public class SwaggerConfiguration {
@Bean
public Docket docket(){
Docket docket =new Docket(DocumentationType.SWAGGER_2);
ApiInfo apiInfo=new ApiInfoBuilder()
.contact(new Contact("serlfy", "com.serlfy", "aaa.qq.com"))
.title("swagger框架学习文档")
.description("描述信息")
.version("1.0")
.build();
docket.apiInfo(apiInfo);
docket=docket
.select()
.apis(Predicates.not(
RequestHandlerSelectors.withMethodAnnotation(
MyAnnotation4Swagger.class)
))
.apis(RequestHandlerSelectors.basePackage("com.serlfy.controller"))
.paths(Predicates.or(PathSelectors.regex("/swagger/.*"),PathSelectors.regex("/swagger2/.*"),PathSelectors.regex("/.*")))
.build();
return docket;
}
}
5,自定义注解及使用
package com.serlyf.anno;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target(value={ElementType.METHOD,ElementType.TYPE})
@Retention(RetentionPolicy.RUNTIME)
public @interface MyAnnotation4Swagger {
String value() default "";
}
6,Swagger生成API文档的范围
.paths(Predicates.or(PathSelectors.regex("/swagger/.*"),PathSelectors.regex("/swagger2/.*"),PathSelectors.regex("/.*")))
7,常用注解
1,类注解:@Api(tags={"MyController","Swagger学习"})给当前控制器增加标签
2,方法注解:@ApiOperation(value="get请求,获取数据...")
3,参数注解:@ApiParam(name="用户名",value="传入参数",required=true)String name
4,@ApiIgnore
5,方法上的参数描述:@ApiImplicitParam(name="name", value="name的描述",required=false,paramType="字符串")
6,方法上的参数描述数组:@ApiImplicitParams(value={@ApiImplicitParam(name="name", value="name的描述",required=false,paramType="字符串"),})
8,实体类注解
@ApiModel(value="自定义实体",description="存储用户数据")
public class MyEntity {
@ApiModelProperty(value="主键",name="主键(ID)",required=false,example="1111",hidden=false)
private String id;
@ApiModelProperty(value="姓名",name="姓名(name)",required=false,example="张三",hidden=false)
private String name;
@ApiModelProperty(value="密码",name="密码(password)",required=false,example="123456",hidden=false)
private String password;
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· AI Agent开发,如何调用三方的API Function,是通过提示词来发起调用的吗