繁体   English   中英

@ApiResponse 响应体为空(Spring Boot)

[英]@ApiResponse with empty response body (Spring Boot)

我正在寻找一种方法来告诉 swagger 某个 API 响应代码没有响应主体。 例如,get 响应可以返回带有实际 object 的 200 代码作为响应,或者如果与传递的 ID 关联的 object 不存在则返回 404:

@ApiResponses(value = {
    @ApiResponse(responseCode = "200", description = "Object found"),
    @ApiResponse(responseCode = "404", description = "Invalid object ID", content = @Content)
})

这是我能想到的最接近的东西,但它并不完美,在 404 响应的描述下我仍然得到一个烦人的“媒体类型”。 谢谢!

如果您没有指定@ApiResponse注释的content属性,则控制器方法的返回类型将是您的响应内容。 为了防止这种情况,明确定义content

@ApiResponse(responseCode = "200", description = "OK",
             content = @Content(schema = @Schema(implementation = Void.class)))

或者您可以简单地返回ResponseEntity<Void>

您可以在 v2 中的方法之上使用以下内容

@ApiResponses(value = {
        @ApiResponse(code = 200, message = "Success", response = YourObject.class),
        @ApiResponse(code = 401, message = "Unauthorized"),
        @ApiResponse(code = 403, message="Forbidden"),
        @ApiResponse(code = 404, message = "Not Found"),
        @ApiResponse(code = 500, message = "Failure")
})

对于 V3,您可以尝试这样的方法,以防您的方法返回某个对象

@Operation(summary = "Add a new object", description = "", tags = { "yourObject" })
@ApiResponses(value = { 
        @ApiResponse(responseCode = "201", description = "Object created",content = @Content(schema = @Schema(implementation = YourObject.class))), 
        @ApiResponse(responseCode = "400", description = "Invalid input"), 
        @ApiResponse(responseCode = "409", description = "Object already exists") })    
        @PostMapping(value = "/your-url", consumes = {"application/json","application/xml" })
        public ResponseEntity<YourObject> addObject(
            ...
            return ...
        }

如果你的方法返回 void 试试这个

@Operation(summary = "Update an existing object", description = "", tags = { "yourObject" })
@ApiResponses(value = {
        @ApiResponse(responseCode = "200", description = "successful operation"),
        @ApiResponse(responseCode = "400", description = "Invalid ID supplied"),
        @ApiResponse(responseCode = "404", description = "Object not found"),
        @ApiResponse(responseCode = "405", description = "Validation exception") })  
        @PutMapping(value = "/your-url/{id}", consumes = { "application/json", "application/xml" })  
        public ResponseEntity<Void> addObject(
            ...
            return ...
        }

这可能是更好(也更短)的方式:

@ApiResponse(
   responseCode = "404", 
   description = "Not found", 
   content = @Content(schema = @Schema(hidden = true))) 

不确定它是否是一项功能,但空的@Content对我有用:

interface MyControllerOpenApiSpec {

    @ApiResponse(responseCode = "200") // shows MyDTO schema
    @ApiResponse(responseCode = "404", content = @Content) // no schema shown
    MyDTO getMyDTO();

}

没有任何内容方法; 也许,它被改变了。

public @interface ApiResponse {
    int code();

    String message();

    Class<?> response() default Void.class;

    String reference() default "";

    ResponseHeader[] responseHeaders() default {@ResponseHeader(
    name = "",
    response = Void.class
)};

    String responseContainer() default "";

    Example examples() default @Example({@ExampleProperty(
    value = "",
    mediaType = ""
)});
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM