为什么 Swagger 的 api-docs 响应包含在额外的 JSON 对象中?
Why is Swagger's api-docs response wrapped in additional JSON Object?
我通过包含依赖项向我的 Spring 启动应用程序添加了 swagger:
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.3.1</version>
</dependency>
并添加配置 class:
@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
并获得一些 JSON 似乎描述了我的 API。但是,JSON 包裹在另一个 JSON 对象中,如下所示(缩写):
{"value":"{\"swagger\":\"2.0\",\"info\":...}
不需要 {"value": "..."}
对象并导致 swagger-ui 出错。 PetStore 示例没有这个额外的级别。这是一个错误?还是配置问题?
PS:我还为此创建了一个github issue
https://github.com/springfox/springfox/issues/1156 那边的人给我指出了正确的方向。问题如下:
我使用 Gson 而不是 Jackson 来 de-/serialise Json。当 springfox.documentation.spring.web.json.Json
类型的对象被序列化时,Jackson 会考虑包含的注释,但会被 Gson 忽略。
我通过包含依赖项向我的 Spring 启动应用程序添加了 swagger:
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.3.1</version>
</dependency>
并添加配置 class:
@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
并获得一些 JSON 似乎描述了我的 API。但是,JSON 包裹在另一个 JSON 对象中,如下所示(缩写):
{"value":"{\"swagger\":\"2.0\",\"info\":...}
不需要 {"value": "..."}
对象并导致 swagger-ui 出错。 PetStore 示例没有这个额外的级别。这是一个错误?还是配置问题?
PS:我还为此创建了一个github issue
https://github.com/springfox/springfox/issues/1156 那边的人给我指出了正确的方向。问题如下:
我使用 Gson 而不是 Jackson 来 de-/serialise Json。当 springfox.documentation.spring.web.json.Json
类型的对象被序列化时,Jackson 会考虑包含的注释,但会被 Gson 忽略。