Swagger 2.8.0无法生成API文档

时间:2018-07-09 06:40:54

标签: java spring swagger-2.0 jackson-dataformat-xml

当我调用swagger /v2/api-docs端点时,无法在Spring Boot应用程序中获取有效的json。

Chrome错误消息:

  

此页面包含以下错误:位于第1行的错误   列1330:xmlParseEntityRef:无名称下图是   页面直到第一个错误。

使用开发人员工具,我看到swagger.json包裹在xml标记中, 内容类型也设置为application / xhtml + xml。 响应如下所示:

<Json>{"swagger":"2.0",..............</Json>

我正在使用Spring Boot 2.0.0.RELEASESpring 5.0.4.RELEASE和 用于XML映射jackson-dataformat-xml依赖版本2.9.4

有没有一种方法可以将application/json作为内容类型发送或配置杰克逊依赖项,从而使它不会作为类路径中的第一个加载? 还是有其他解决方法?

对于Jackson,我们只使用了导入,没有单独的配置。

Swagger配置:

@SpringBootApplication (exclude = {SecurityAutoConfiguration.class})
public class Main {

public static void main(String[] args) {
    SpringApplication.run(Main.class, args);
}

@ConditionalOnWebApplication
@EnableSwagger2
public static class SwaggerConfig {

    @Bean
    public Docket springfoxDocket() {

        StringVendorExtension vendorExtension = new StringVendorExtension("x-wso2-security", Wso2Config.serialize());
        List<VendorExtension> vendorExtensions = new ArrayList<>();
        vendorExtensions.add(vendorExtension);

        return new Docket(DocumentationType.SWAGGER_2)
                .select()
                .apis(RequestHandlerSelectors.basePackage("com"))
                .build()
                .protocols(protocolSet())
                .apiInfo(apiInfo())
                .extensions(vendorExtensions)
                .directModelSubstitute(LocalDateTime.class, Date.class)
                .useDefaultResponseMessages(false).enableUrlTemplating(false)
                ;
    }

    private ApiInfo apiInfo() {
        return new ApiInfo(
                "Event",
                "Creates, deletes, reads events",
                "2.0",
                "",
                null,
                null, null, Collections.emptyList());
    }

    private Set<String> protocolSet() {
        Set<String> protocolsSet = new HashSet<>();
        protocolsSet.add("http");
        protocolsSet.add("https");
        return protocolsSet;
    }

}
}

1 个答案:

答案 0 :(得分:0)

由于您具有jackson-dataformat-xml依赖性,因此Spring Boot会将内容类型默认为application/xml。 如果您想将application\json用作内容类型,请配置restTemplate。 请关注问题Swagger 2 accept xml instead of json