【问题标题】:Why is Swagger's api-docs response wrapped in additional JSON Object?为什么 Swagger 的 api-docs 响应包含在额外的 JSON 对象中?
【发布时间】:2016-01-27 10:11:33
【问题描述】:

我通过包含依赖项向我的 Spring Boot 应用程序添加了 swagger:

<dependency>
  <groupId>io.springfox</groupId>
  <artifactId>springfox-swagger2</artifactId>
  <version>2.3.1</version>
</dependency>

并添加一个配置类:

@Configuration
@EnableSwagger2
public class SwaggerConfig {

    @Bean
    public Docket api(){
        return new Docket(DocumentationType.SWAGGER_2)
            .select()
            .apis(RequestHandlerSelectors.any())
            .paths(Predicates.or(PathSelectors.regex("...")))
            .build()
            .apiInfo(apiInfo());
    }

    private ApiInfo apiInfo() {
        return new ApiInfo(...);
    }
}

我现在可以访问 /v2/api-docs 并获得一些似乎描述我的 API 的 JSON。然而,JSON 被包裹在另一个 JSON 对象中,像这样(缩写): {"value":"{\"swagger\":\"2.0\",\"info\":...}

{"value": "..."} 对象不是必需的,它会导致 swagger-ui 中的错误。 PetStore 示例没有这个额外的级别。这是一个错误?还是配置问题?

PS:我还为此创建了一个github issue

【问题讨论】:

    标签: spring spring-boot swagger swagger-2.0


    【解决方案1】:

    https://github.com/springfox/springfox/issues/1156 的人为我指出了正确的方向。问题如下:

    我使用 Gson 而不是 Jackson 来反序列化 Json。当springfox.documentation.spring.web.json.Json 类型的对象被序列化时,Jackson 会考虑包含的注释,但 Gson 会忽略。

    【讨论】:

      猜你喜欢
      • 2016-08-07
      • 2015-10-09
      • 2016-09-12
      • 1970-01-01
      • 1970-01-01
      • 2017-05-14
      • 1970-01-01
      • 1970-01-01
      • 2017-02-12
      相关资源
      最近更新 更多