gpt4 book ai didi

java - 如何使用 Swagger 在 @ApiResponse 中添加示例?

转载 作者:行者123 更新时间:2023-12-05 04:26:44 24 4
gpt4 key购买 nike

我想在我的方法中添加一个 Swagger 示例,我已经尝试了一些方法,但它们没有用。

我有我的接口(interface),我在其中定义了方法:

@Api(value = "test API")
@RequestMapping("/api/v1/product")
public interface TestController {

@ApiOperation(
value = "Service that return a Product",
notes = "This service returns a Product by the ID",
nickname = "getProductById",
response = ProductResponse.class)
@ApiResponses(value = {
@ApiResponse(code = 200, message = "The request has succeeded.", response = ProductResponse.class),
@ApiResponse(code = 500, message = "Internal server error.", response = ProductResponse.class) })
@GetMapping(
value = "/productById",
produces = { "application/json" }
)
ResponseEntity<ProductResponse> getProductById(@RequestParam(value = "productId", required = true) String productId);

ProductResponse 类如下:

@Getter
@Setter
@AllArgsConstructor
public class ProductResponse {

private Product product;
private CustomException customException;

}

产品类别如下:

@Getter
@Setter
@AllArgsConstructor
public class Product {

@JsonProperty("id")
private String id;

@JsonProperty("productName")
private String productName;

@JsonProperty("productDescription")
private String productDescription;

@JsonProperty("unitPrice")
private Double unitPrice;

CustomException 类如下:

@Getter
public class CustomException {

private final String message;
private final String errorCode;
private final String errorType;
private final Exception exceptionDetail;

public CustomException(String message, String errorCode, String errorType, Exception exceptionDetail) {
this.message = message;
this.errorCode = errorCode;
this.errorType = errorType;
this.exceptionDetail = exceptionDetail;
}

当响应为200时,响应是这样的:

{
"product": {
"id": "12345",
"productName": "Product name",
"productDescription": "This is a description",
"unitPrice": 3.25
},
"customException": null
}

但是当响应为 500 时,响应是这样的:

{
"product": "null,",
"customException": {
"message": "/ by zero",
"errorCode": "500",
"errorType": "Internal server error",
"exceptionDetail": null,
"cause": null,
"stackTrace": [
{
"classLoaderName": "app",
"moduleName": null,
"moduleVersion": null,
"methodName": "getProductById",
"fileName": "TestControllerImpl.java",
"lineNumber": 33,
"className": "com.myproject.testmicroservice.controller.impl.TestControllerImpl",
"nativeMethod": false
}
]
}
}

如何在@ApiResponse 注解中添加自定义示例?

最佳答案

您可能缺少 @Operation 注释,您将 @ApiResponse 放在其中。

例子:

  import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.Operation;

@Operation(responses = {
@ApiResponse(responseCode = "200", content = @Content(examples = {
@ExampleObject(name = "getUserAttribute",
summary = "Retrieves a User's attributes.",
description = "Retrieves a User's attributes.",
value = "[{\"value\": [\"area1\", \"area2\", \"area3\"], \"key\":\"GENERAL_AREAS\"}, {\"value\":\"933933933\", \"key\":\"FONyE\"}]")
}, mediaType = MediaType.APPLICATION_JSON_VALUE))})
public ResponseEntity<List<UserPreferenceDto>> getUserPreferenceByCode(
@Pattern(regexp = "\\w+") @PathVariable String userCode, @Parameter(hidden = true) Pageable pageable) {

...
}

关于java - 如何使用 Swagger 在 @ApiResponse 中添加示例?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72965340/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com