【问题标题】:Swagger UI not rendering the correct response(Should return a JSON response)[This is for the GET request Code 200]Swagger UI 未呈现正确的响应(应返回 JSON 响应)[这是用于 GET 请求代码 200]
【发布时间】:2019-07-16 19:07:18
【问题描述】:

Swagger UI 未在示例值中返回预期的 JSON 响应。它返回一个空列表。下面是我正在使用的代码 sn-ps,

Gradle 依赖

compile('io.springfox:springfox-swagger-ui:2.9.2')
compile('io.springfox:springfox-swagger2:2.9.2')

Swagger 配置

  @Bean
        public Docket api() {
            return new Docket(DocumentationType.SWAGGER_2)
                    .ignoredParameterTypes(HttpServletResponse.class)
                    .select()                .apis(RequestHandlerSelectors.basePackage("com.core.controller.v2"))           
                .paths(PathSelectors.any())
                .build()
                .enable(true)
                .apiInfo(apiInfo())
                .securityContexts(Lists.newArrayList(securityContext()))
                    .securitySchemes(Lists.newArrayList(apiKey()));

控制器

@ApiOperation(value="A GET request to get a list of all contents for a given user.",
            notes = "This API is used to get the contents for a given user with an NPI and Partner ID",
            response = CoreContentItem.class)
    @ApiResponses(value = {
            @ApiResponse(code = 200, message = "Success response", response = CoreContentItem.class,responseContainer = "List"),
            @ApiResponse(code = 401, message = "Unauthorized"),
            @ApiResponse(code = 400, message = "Bad Request",responseContainer = "String"),
            @ApiResponse(code = 403, message = "Forbidden"),
            @ApiResponse(code = 500, message = "Internal Server Error, please contact system administrator")})

大摇大摆的输出 Swagger UI for success Response

预期的 JSON 响应

这是一个预期的 JSON 响应示例

[
  {
    "item": {
      "id": "3f94ea1a687dda4af3e2",
      "category": null,
      "type": "EXTERNAL",
      "headline": "DO NOT DELETE - REST ASSURED All - HIGH - JV",
      "summary": "DO NOT DELETE - All - HIGH - JV",
      "details": null,
      "mediaURL": "",
      "createdOn": 1493658088000,
      "modifiedOn": 1495553312000,
      "priority": "HIGH",
      "startDate": 1493618400000,
      "endDate": 1588312800000,
      "feedbackEmail": null,
      "totalLikes": 0,
      "totalViews": 2,
      "customData": null,
      "userInteraction": {
        "userLiked": false,
        "userDisliked": false,
        "userViewed": false
      },
      "availableActions": [
        "View",
        "Done",
        "Submit"
      ],
      "externalURL": "https://www.1234.com/vegetables/armando%25e2%2580%2599s-chiles-rellenos/r/5014"
    }
  }
]

【问题讨论】:

  • 欢迎来到 SO!请在您的问题中包含您预期的回答。
  • 添加了 JSON 响应
  • 你试过不指定吗?此外,看起来您的示例是 CoreContentItems 的列表,而不仅仅是一个?如果您实际上要返回一个列表,则必须在您的 @ApiOperation 中添加 responseContainer="List" 参数。
  • 其实我也试过了。它仍然呈现一个空列表。
  • 这个 json 来自另一个微服务。这有什么区别吗??

标签: spring-boot swagger swagger-ui swagger-2.0 springfox


【解决方案1】:

我发现了问题。显然是模型映射异常导致 Swagger UI 返回一个空列表。现在我可以在示例值中看到模型。 enter image description here

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2021-04-29
    • 2020-02-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-01-29
    • 2012-03-09
    • 1970-01-01
    相关资源
    最近更新 更多