英文:
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.Error
是 Payload<T>
的内部类。但无论如何,我还尝试过其他一些类,比如:String、Map 等,但 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("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>`. 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
还是直接在端点上使用注解都有效。
希望这就是您想要的内容。
英文:
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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论