自学内容网 自学内容网

spring boot 3.x 整合Swagger3

环境介绍

  • jdk版本: 17
  • spring boot 版本:3.2.0
  • Swagger3版本:2.2.0

整合Swagger

添加依赖

<dependency>
            <groupId>org.springdoc</groupId>
            <artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
            <version>2.2.0</version>
 </dependency>

添加配置类 

package com.tech.cloud.config;

import io.swagger.v3.oas.models.ExternalDocumentation;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Info;
import org.springdoc.core.models.GroupedOpenApi;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@Configuration
public class Swagger3Config {

    // 按模块定义 当前支付模块
    @Bean
    public GroupedOpenApi PayApi() {
        return GroupedOpenApi.builder().group("支付微服务模块")
                .pathsToMatch("/pay/**").build();
    }

    // 举例其他模块
    @Bean
    public GroupedOpenApi OtherApi() {
        return GroupedOpenApi.builder().group("其他微服务模块")
                .pathsToMatch("/other/**", "others").build();
    }


    // 生成文档
    @Bean
    public OpenAPI docsOpenApi() {
        return new OpenAPI()
                .info(new Info().title("cloud_tech")
                        .description("通用架构设计")
                        .version("v1.0"))
                .externalDocs(new ExternalDocumentation()
                        .description("整合目前微服务最新组件,基于jdk17、spring boot3.2.0")
                        .url("www.baidu.com"));
    }

}

在项目中使用

接口中使用

  • 类上 @Tag(name = "支付服务模块", description = "支付CRUD")
  • 方法上  @Operation(summary = "新增", description = "新增支付流水方法")

@RestController
@Slf4j
@Tag(name = "支付服务模块", description = "支付CRUD")
public class PaymentController {
    @Resource
    private PaymentService payService;

    @PostMapping(value = "/pay/add")
    @Operation(summary = "新增", description = "新增支付流水方法")
    public ResultData<String> addPay(@RequestBody PaymentPO pay) {
        System.out.println(pay.toString());
        int i = payService.add(pay);
        return ResultData.success("成功插入记录,返回值: " + i);
    }

    @DeleteMapping(value = "/pay/del/{id}")
    @Operation(summary = "删除", description = "删除支付流水方法")
    public ResultData<Integer> deletePay(@PathVariable("id") Integer id) {
        int i = payService.delete(id);
        return ResultData.success(i);
    }

    @PutMapping(value = "/pay/update")
    @Operation(summary = "修改", description = "修改支付流水方法")
    public ResultData<String> updatePay(@RequestBody PayDTO payDTO) {
        PaymentPO pay = new PaymentPO();
        BeanUtils.copyProperties(payDTO, pay);
        int i = payService.update(pay);
        return ResultData.success("成功修改记录,返回值: " + i);
    }

    @GetMapping(value = "/pay/get/{id}")
    @Operation(summary = "按照ID查流水", description = "查询支付流水方法")
    public ResultData<PaymentPO> getPayInfo(@PathVariable("id") Integer id) {
        return ResultData.success( payService.getById(id));
    }

    @GetMapping(value = "/get/all")
    @Operation(summary = "获取所有", description = "获取所有")
    public ResultData getAll() {
        List<PaymentPO> all = payService.getAll();
        return ResultData.success(all);
    }

}

实体类中使用

  • 类上和字段上@Schema(title = "主键")

@Data
@Table(name = "t_pay")
@Schema(title = "支付交易表实体类")
public class PaymentPO {
    @Id
    @GeneratedValue(generator = "JDBC")
    @Schema(title = "主键")
    private Integer id;

    /**
     * 支付流水号
     */
    @Column(name = "pay_no")
    @Schema(title = "支付流水号")
    private String payNo;

    /**
     * 订单流水号
     */
    @Column(name = "order_no")
    @Schema(title = "订单流水号")
    private String orderNo;

    /**
     * 用户账号ID
     */
    @Column(name = "user_id")
    @Schema(title = "用户账号ID")
    private Integer userId;

    /**
     * 交易金额
     */
    @Schema(title = "交易金额")
    private BigDecimal amount;

    /**
     * 删除标志,默认0不删除,1删除
     */
    private Byte deleted;

    /**
     * 创建时间
     */
    @Column(name = "create_time")
    @Schema(title = "创建时间")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private Date createTime;

    /**
     * 更新时间
     */
    @Column(name = "update_time")
    @Schema(title = "更新时间")
    @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss", timezone = "GMT+8")
    private Date updateTime;
}

访问Swagger

主机名:端口/swagger-ui/index.html,我这里访问http://localhost:9100/swagger-ui/index.html

 按模块展示

 

查看标注的实体类 


原文地址:https://blog.csdn.net/m0_65775063/article/details/143359216

免责声明:本站文章内容转载自网络资源,如本站内容侵犯了原著者的合法权益,可联系本站删除。更多内容请关注自学内容网(zxcms.com)!