Swagger UI不显示来自自定义注解接口的响应模型。

huangapple go评论83阅读模式
英文:

Swagger UI does not display response models from custom annotation interface

问题

我正在尝试为 Swagger 文档定义全局响应我有以下注解接口

```java
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 = "成功的状态响应"
        ),
        @ApiResponse(
                code = 401,
                message = "未授权",
                response = Payload.Error.class
        ),
        @ApiResponse(
                code = 403,
                message = "禁止访问",
                response = Payload.Error.class
        ),
        @ApiResponse(
                code = 404,
                message = "未找到",
                response = Payload.Error.class
        ),
        @ApiResponse(
                code = 500,
                message = "通用服务器错误",
                response = Payload.Error.class
        ),
})
@ResponseBody
public @interface PayloadResponse {
}

在我的控制器内部,我像这样使用这个接口:

    @ApiOperation("登录后获取用户详细信息")
    @PayloadResponse
    @GetMapping("/user")
    Payload<User> fetchUser(
            @ApiIgnore
            @RequestHeader(HttpHeaders.AUTHORIZATION) String authorization
    );

这里的 Payload.ErrorPayload<T> 的内部类。但无论如何,我还尝试过其他一些类,比如:String、Map 等,但 Swagger UI 仍然显示空的响应:

Swagger UI不显示来自自定义注解接口的响应模型。

从这一点出发,我如何在尽可能少的更改情况下使其正常工作?


Note: I've provided the translated code parts as requested. If you need further assistance or have more content to translate, please let me know.

<details>
<summary>英文:</summary>

I am trying define global responses for swagger docs. 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(&quot;Get user details after login&quot;)
@PayloadResponse
@GetMapping(&quot;/user&quot;)
Payload&lt;User&gt; fetchUser(
        @ApiIgnore
        @RequestHeader(HttpHeaders.AUTHORIZATION) String authorization
);

Here `Payload.Error` is an internal class of `Payload&lt;T&gt;`. But anyway, I tried with some other classes too, like: String, Map etc. but the swagger ui keeps displaying empty responses:

[![enter image description here][1]][1]

How can I manage to make it working from this point doing as little changes as possible?


  [1]: https://i.stack.imgur.com/ALs17.png

</details>


# 答案1
**得分**: 2

Springfox 3.0 默认使用 v3 模型,但您正在使用 `io.swagger.annotations.ApiResponses` 而不是 `io.swagger.v3.oas.annotations.responses.ApiResponses`,以及 `io.swagger.annotations.ApiResponse` 而不是 `io.swagger.v3.oas.annotations.responses.ApiResponse`。

这个问题已经在 https://github.com/springfox/springfox/issues/3503 进行了记录。

但是解决方法也非常简单。只需添加一个属性来覆盖 v3 模型,使用 v2 模型。

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

这样就可以很好地工作了。无论是使用 @PayloadResponse 还是直接在端点上使用注解都有效。

希望这就是您想要的内容。

Swagger UI不显示来自自定义注解接口的响应模型。

英文:

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.
This problem has been documented at
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.

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

And it works like a charm. Worked both with @PayloadResponse and as direct annotation at the endpoint.

I hope this is the one you wanted.
Swagger UI不显示来自自定义注解接口的响应模型。

huangapple
  • 本文由 发表于 2020年10月7日 10:24:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/64236324.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定