Spring Boot2中Swagger3使用

无涯子wyz發表於2024-05-10

1.依賴引入

<!--   引入swagger          -->
<dependency>
    <groupId>org.springdoc</groupId>
    <artifactId>springdoc-openapi-ui</artifactId>
    <version>1.7.0</version>
</dependency>

2.常用註解介紹

swagger2 OpenAPI 3 註解位置
@Api @Tag(name = “介面類描述”) Controller 類上
@ApiOperation @Operation(summary =“介面方法描述”) Controller 方法上
@ApiImplicitParams @Parameters Controller 方法上
@ApiImplicitParam @Parameter(description=“引數描述”) Controller 方法上 @Parameters 裡
@ApiParam @Parameter(description=“引數描述”) Controller 方法的引數上
@ApiIgnore @Parameter(hidden = true) 或 @Operation(hidden = true) 或 @Hidden -
@ApiModel @Schema DTO、DO、VO屬性上
@ApiModelProperty @Schema DTO、DO、VO屬性上

3.配置檔案

@Configuration
public class SwaggerConfig {
    @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("cloud2024")
                        .description("通用設定rest")
                        .version("1.0"))
                .externalDocs(new ExternalDocumentation()
                        .description("www.cnblogs.com/wyzstudy")
                        .url("https://www.cnblogs.com/wyzstudy"));
    }
}

4.常見使用

@Data
@Schema(title = "支付DTO物件")
public class PayDTO implements Serializable {
    @Schema(title = "ID")
    private Integer id;

    @Schema(title = "支付ID")
    private String payNo;

    @Schema(title = "訂單ID")
    private String orderNo;

    @Schema(title = "使用者ID")
    private Integer userId;

    @Schema(title = "支付金額")
    private Double amount;
}
@RestController
@RequestMapping("/pay")
@Tag(name = "支付模組", description = "支付相關介面")
public class PayController {
    @Resource
    private PayService payService;

    @Operation(summary = "新增支付訂單資訊")
    @PostMapping
    public ResponseResult<String> save(@RequestBody PayDTO payDTO){
        payService.save(payDTO);
        return ResponseResult.success();
    }

    @Operation(summary = "刪除支付訂單資訊")
    @DeleteMapping("/{id}")
    public ResponseResult<String> delete(@PathVariable Integer id){
        payService.delete(id);
        return ResponseResult.success();
    }

    @Operation(summary = "更新支付訂單資訊")
    @PutMapping
    public ResponseResult<String> update(@RequestBody PayDTO payDTO){
        payService.updatePayById(payDTO);
        return ResponseResult.success();
    }

    @Operation(summary = "根據ID查詢支付訂單資訊")
    @GetMapping("/{id}")
    public ResponseResult<PayVO> queryPayById(@PathVariable Integer id){
        return ResponseResult.success(payService.getPayById(id));
    }

    @Operation(summary = "查詢所有訂單資訊")
    @GetMapping
    public ResponseResult<List<PayVO>> getPayList(){
        return  ResponseResult.success(payService.getPayList());
    }
}

5.前端檢視


訪問地址:http://localhost:8001/swagger-ui/index.html

埠號要改為你服務的埠號。

相關文章