有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java Swagger 2.8.0无法生成apidocs

当我调用swagger /v2/api-docs端点时,我无法在spring boot应用程序中获得有效的json

Chrome错误消息:

This page contains the following errors: error on line 1 at column 1330: xmlParseEntityRef: no name Below is a rendering of the page up to the first error.

有了开发工具,我看到了这种狂妄自大。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依赖项,让spring不将其作为类路径中的第一个加载? 或者有没有其他方法来解决这个问题

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

大摇大摆的配置:

@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) 个答案

  1. # 1 楼答案

    由于您有jackson-dataformat-xml依赖项,spring boot将默认内容类型为application/xml。 如果您想要application\json作为内容类型,那么将restTemplate配置为这样做。 请跟着问题Swagger 2 accept xml instead of json