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
端口号要改为你服务的端口号。
标签:return,title,Spring,ResponseResult,Boot2,Operation,Swagger3,public,Schema From: https://www.cnblogs.com/wyzstudy/p/18184130