简体   繁体   English

Swagger UI 不显示来自自定义注释界面的响应模型

[英]Swagger UI does not display response models from custom annotation interface

I am trying define global responses for swagger docs.我正在尝试为 swagger 文档定义全局响应。 I have the following annotation interface:我有以下注释接口:

import io.swagger.annotations.ApiResponse;
import io.swagger.annotations.ApiResponses;
import org.springframework.web.bind.annotation.ResponseBody;

import java.lang.annotation.*;

@Documented
@Target(ElementType.METHOD)
@Retention(RetentionPolicy.RUNTIME)
@Inherited
@ApiResponses(value = {
        @ApiResponse(
                code = 200,
                message = "Successful status response"
        ),
        @ApiResponse(
                code = 401,
                message = "Unauthorized",
                response = Payload.Error.class
        ),
        @ApiResponse(
                code = 403,
                message = "Forbidden",
                response = Payload.Error.class
        ),
        @ApiResponse(
                code = 404,
                message = "Not Found",
                response = Payload.Error.class
        ),
        @ApiResponse(
                code = 500,
                message = "General Server Error",
                response = Payload.Error.class
        ),
})
@ResponseBody
public @interface PayloadResponse {
}

And inside the my controllers, I use this interface like this:在我的控制器中,我像这样使用这个界面:

    @ApiOperation("Get user details after login")
    @PayloadResponse
    @GetMapping("/user")
    Payload<User> fetchUser(
            @ApiIgnore
            @RequestHeader(HttpHeaders.AUTHORIZATION) String authorization
    );

Here Payload.Error is an internal class of Payload<T> .这里Payload.ErrorPayload<T>的内部类。 But anyway, I tried with some other classes too, like: String, Map etc. but the swagger ui keeps displaying empty responses:但无论如何,我也尝试过其他一些类,例如:String、Map 等,但 swagger ui 一直显示空响应:

在此处输入图片说明

How can I manage to make it working from this point doing as little changes as possible?我怎样才能让它从这一点开始工作,尽可能少做改变?

Springfox 3.0 uses v3 models by default but you are using io.swagger.annotations.ApiResponses instead of io.swagger.v3.oas.annotations.responses.ApiResponses and io.swagger.annotations.ApiResponse instead of io.swagger.v3.oas.annotations.responses.ApiResponse . Springfox 3.0 默认使用 v3 模型,但您使用的是io.swagger.annotations.ApiResponses而不是io.swagger.v3.oas.annotations.responses.ApiResponsesio.swagger.annotations.ApiResponse而不是io.swagger.v3.oas.annotations.responses.ApiResponse
This problem has been documented at https://github.com/springfox/springfox/issues/3503这个问题已经记录在https://github.com/springfox/springfox/issues/3503

But the workaround is also very easy.但解决方法也很简单。 Just add a property to override v3 models with v2 models.只需添加一个属性即可使用 v2 模型覆盖 v3 模型。

springfox.documentation.swagger.use-model-v3=false

And it works like a charm.它就像一个魅力。 Worked both with @PayloadResponse and as direct annotation at the endpoint.@PayloadResponse和端点的直接注释一起使用。

I hope this is the one you wanted.我希望这是你想要的。 Swagger ui 的屏幕截图

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

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