swagger教学嵌入Spring Boot项目中使用教程

pom文件引入

        <!--swagger3-->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-boot-starter</artifactId>
            <version>3.0.0</version>
        </dependency>
        
        
        
        或者
        
         <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>

创建一个swagger的配置类

@Configuration
@EnableOpenApi
@ConditionalOnProperty(prefix = "knife4j",name = "enable",havingValue = "true")//在application.yml文件中配置
public class Swagger2Config {

    @Bean
    public Docket restApi() {

 // DocumentationType.SWAGGER_2 固定的,代表swagger2
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())// 用于生成API信息
                .useDefaultResponseMessages(true)
                .forCodeGeneration(false)
                .select()// select()函数返回一个ApiSelectorBuilder实例,用来控制接口被swagger做成文档
                .apis(RequestHandlerSelectors.basePackage("com.xxx.xx"))// 用于指定扫描哪个包下的接口
                .paths(PathSelectors.any())// 选择所有的API,如果你想只为部分API生成文档,可以配置这里
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("XX项目API")// 可以用来自定义API的主标题
                .description("XX项目SwaggerAPI管理")// 可以用来描述整体的API
                .contact(new Contact("Fisher", "https://blog.csdn.net/qq_40977118", "xxx@163.com"))// 用于定义服务的域名
                .version("1.0")
                .build();
    }

定义接口

@Api(tags = "角色管理") // tags:你可以当作是这个组的名字。 @RestController public class RoleController { }

对于GET方式,swagger不推荐使用body方式来传递数据,也就是不希望在GET方式时使用json、form-data等方式来传递,这时候最好使用路径参数或者url参数。(😓虽然POSTMAN等是支持的),所以如果接口传递的数据是json或者form-data方式的,还是使用POST方式好。

场景一:请求参数是实体类。

此时我们需要使用@ApiModel来标注实体类,然后在接口中定义入参为实体类即可:

  • @ApiModel:用来标类

    • 常用配置项:

      • value:实体类简称

      • description:实体类说明

  • @ApiModelProperty:用来描述类的字段的意义。

    • 常用配置项:

      • value:字段说明

      • example:设置请求示例(Example Value)的默认值,如果不配置,当字段为string的时候,此时请求示例中默认值为"".

      • name:用新的字段名来替代旧的字段名。

      • allowableValues:限制值得范围,例如{1,2,3}代表只能取这三个值;[1,5]代表取1到5的值;(1,5)代表1到5的值,不包括1和5;还可以使用infinity或-infinity来无限值,比如[1, infinity]代表最小值为1,最大值无穷大。

      • required:标记字段是否必填,默认是false,

      • hidden:用来隐藏字段,默认是false,如果要隐藏需要使用true,因为字段默认都会显示,就算没有@ApiModelProperty

// 先使用@ApiModel来标注类
@ApiModel(value="用户登录表单对象",description="用户登录表单对象")
public class LoginForm {
    // 使用ApiModelProperty来标注字段属性。
    @ApiModelProperty(value = "用户名",required = true,example = "root")
    private String username;
    @ApiModelProperty(value = "密码",required = true,example = "123456")
    private String password;

    // 此处省略入参赋值时需要的getter,setter,swagger也需要这个
}

定义成入参:

@ApiOperation(value = "登录接口",notes = "登录接口的说明")
    @PostMapping("/login")
    public LoginForm login(@RequestBody LoginForm loginForm){
        return loginForm;
    }

不过我一般都是用于实体类
 

@TableName("goods_type")
@ApiModel(value = "商品类别实体")
public class GoodsType {

具体详情还是根据项目实际想布局多少而定
可以参考下https://www.cnblogs.com/progor/p/13297904.html

  • 7
    点赞
  • 22
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Spring Boot是一个开源的Java开发框架,而Swagger是一个用于构建、发布、文档化和管理API的工具。下面详细介绍如何在Spring Boot整合Swagger。 首先,你需要在pom.xml文件添加Swagger的依赖项。在<dependencies>标签添加以下代码: ```xml <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.10.5</version> </dependency> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.10.5</version> </dependency> ``` 然后,你需要在Spring Boot的配置类添加相关的注解和配置。创建一个SwaggerConfig.java文件,添加以下代码: ```java @Configuration @EnableSwagger2 public class SwaggerConfig { @Bean public Docket api() { return new Docket(DocumentationType.SWAGGER_2) .select() .apis(RequestHandlerSelectors.basePackage("your.package.name")) .paths(PathSelectors.any()) .build(); } @Bean public UiConfiguration uiConfig() { return new UiConfiguration(null, "list", "alpha", "schema", UiConfiguration.Constants.DEFAULT_SUBMIT_METHODS, false, true, 60000L); } } ``` 在上面的代码,你需要将"your.package.name"替换为你的应用程序的包名。这将扫描该包下的所有控制器,生成API文档。 接下来,你可以启动你的Spring Boot应用程序,并访问"http://localhost:8080/swagger-ui.html"来查看生成的API文档。你将看到所有的控制器和它们的方法以及相关的参数和注释。 如果你想修改API的文档信息,你可以使用Swagger的注解来添加说明和标注。例如,你可以在控制器的方法上添加@ApiOperation注解来描述该方法的作用。 综上所述,将Swagger整合到Spring Boot是很简单的。你只需要添加依赖项,配置SwaggerConfig类,然后访问Swagger UI来查看生成的API文档。同时,你可以使用Swagger注解来进一步完善API文档。希望这个教程可以帮助你理解如何在Spring Boot使用Swagger

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值