Tim Büthe
Tim Büthe

Reputation: 63734

Why is Swagger's api-docs response wrapped in additional JSON Object?

I added swagger to my Spring Boot application by including the dependency:

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

and adding a config 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(...);
    }
}

I can now access /v2/api-docs and get some JSON that seems to describe my API. However, the JSON is wrapped in another JSON object like this (abbreviated): {"value":"{\"swagger\":\"2.0\",\"info\":...}

The {"value": "..."} Object isn't needed and causes errors in swagger-ui. The PetStore example doesn't has this extra level. This this a bug? Or a config issue?

PS: I also created a github issue for that

Upvotes: 3

Views: 1609

Answers (1)

Tim B&#252;the
Tim B&#252;the

Reputation: 63734

The guys over at https://github.com/springfox/springfox/issues/1156 pointed me in the right direction. The problem is as follows:

I'm using Gson instead of Jackson to de-/serialise Json. When the object of type springfox.documentation.spring.web.json.Json gets serialised, included annotation are considered by Jackson but ignored by Gson.

Upvotes: 1

Related Questions