- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我想在我的方法中添加一个 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/
我正在使用具有以下依赖项的 SpringBoot org.springdoc springdoc-openapi-ui 1.5.12
我使用 swagger 创建 RESTful API,并有多个返回相同错误和响应的端点: @GET @Path("/some/endpoint") @ApiOperation(
我正在使用 Swagger 注释来记录非 spring 上下文中的 API。我发现 400、401 和 404 的响应文档是可重复使用的。由于记录每个响应代码大约需要 8 行,如下所示。 @O
我在 SpringBoot 项目中使用 OpenApi 3 来生成 Swagger html 页面。 POM.xml 中的依赖关系: org.springdoc
我正在寻找一种方法来告诉 swagger 某个 API 响应代码没有响应正文。例如,获取响应可以返回 200 代码以及实际对象作为响应,如果与传递的 ID 关联的对象不存在,则返回 404: @Api
我想在我的方法中添加一个 Swagger 示例,我已经尝试了一些方法,但它们没有用。 我有我的接口(interface),我在其中定义了方法: @Api(value = "test API") @Re
我使用 refit 来调用 API,响应包含在 ApiResonse 中。我可以模拟返回模拟 ApiResponse 的 refit 调用并断言。一切都按预期工作,只是我不确定如何测试返回异常的 ap
我正在使用 Google Cloud Node 库的 BigQuery API https://googlecloudplatform.github.io/google-cloud-node/#/do
我有一个返回 Map 的端点 public Response doStuff(){ Map map = getMap(); return Response.ok(map ).build();
如何将以下 Swagger 注解从 Java 转换为 Kotlin? @ApiResponses(value = { @ApiResponse(code = 200, message = "Give
如何使用 java 注释创建多个 404 响应(或更广泛地说,多个相同的 HTTP 代码响应)。 我试过了: @ApiResponse( responseCode = "404", d
我正在尝试从 springfox-swagger2 (OpenAPI 2) 迁移到 springdoc-openapi-ui (OpenAPI 3),以生成 swagger 文档。 这是一个示例路线:
我正在尝试使用一些公共(public) API 中的数据,但我陷入了有关此数据设计的一些问题。我正在使用 Spring。 我正在用经典的方式来做: private ResponseEntity get
我在代码中有Springfox注释如下: @ApiResponses(value = { @ApiResponse(code = 200, message = "Options
在 Swagger Java API 中,当我使用 code=400 的 responsecontainer="List"(或“Set”)时,我没有在 Swagger-GUI 上获取响应模型。我刚刚得
我对我的 android 应用程序做了一些更改,现在我得到了这个。我认为这可能是由更新我在我的应用程序中使用的自动生成的 ApiGateway SDK 引起的。但也许是别的原因?我什至不确定从哪里开始
我喜欢 Swagger,因为它使您的 API 对用户非常友好。我使用 Swagger 注释,例如 @ApiParam @ApiResponse | @ApiResponses @ApiOperatio
我有一个 Angular 8应用程序。这是服务的样子:- export class UserService{ constructor(private http: HttpClient) { }
如文档中所写,如果我们想要定义多个响应,我们可以将 anyOf 与 @Schema 一起使用。 @ApiResponse(responseCode = "201", description =
我正在使用 springfox 3.0.0 进行响应式支持,并且正在使用 @EnableSwagger2WebFlux在我招摇的配置上。 我的招摇配置如下: @Bean public Docket a
我是一名优秀的程序员,十分优秀!