Spring boot swagger ui不使用自定义XML ObjectMapper

Spring boot swagger ui不使用自定义XML ObjectMapper,spring-boot,swagger,swagger-ui,jackson-dataformat-xml,Spring Boot,Swagger,Swagger Ui,Jackson Dataformat Xml,我正在开发一个SpringBoot应用程序,它应该启用了SwiggerUI。 访问时出现错误弹出窗口: “无法推断基本url…” 此外,还显示: 第1列第1行出错:文档为空 此页面的源代码是json,但它是作为内容类型application/xhtml+xml请求的;字符集=UTF-8 原因似乎是我的自定义Jackson配置: @Configuration public class JacksonConfig { @Bean public MappingJackson2XmlHttpMessa

我正在开发一个SpringBoot应用程序,它应该启用了SwiggerUI。 访问时出现错误弹出窗口: “无法推断基本url…”

此外,还显示: 第1列第1行出错:文档为空 此页面的源代码是json,但它是作为内容类型application/xhtml+xml请求的;字符集=UTF-8

原因似乎是我的自定义Jackson配置:

@Configuration
public class JacksonConfig {

@Bean
public MappingJackson2XmlHttpMessageConverter mappingJackson2XmlHttpMessageConverter() {
    return new MappingJackson2XmlHttpMessageConverter(objectMapper());
}

@Bean
public ObjectMapper objectMapper() {
    JacksonXmlModule xmlModule = new JacksonXmlModule();
    xmlModule.setDefaultUseWrapper(false);
    XmlMapper objectMapper = new XmlMapper(xmlModule);
    objectMapper
            .registerModule(new ParameterNamesModule())
            .registerModule(new Jdk8Module())
            .registerModule(new JavaTimeModule());
    objectMapper.setPropertyNamingStrategy(PropertyNamingStrategy.UPPER_CAMEL_CASE);
    objectMapper
            .configure(MapperFeature.DEFAULT_VIEW_INCLUSION, false)
            .configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES, true);

    return objectMapper;
}
}
具有以下依赖项:

<dependency>
        <groupId>com.fasterxml.jackson.dataformat</groupId>
        <artifactId>jackson-dataformat-xml</artifactId>
    </dependency>

com.fasterxml.jackson.dataformat


所以我的问题是:如何指定jackson消息转换器的优先级以使swagger ui正常工作?

我在重读自己的问题时偶然发现了这个解决方案

只需将其添加到上述JacksonConfig类中(不知道排序是否重要,但它可以工作)


在swagger代码中,它检查是否存在ObjectMapper,如果不存在,则创建一个要使用的ObjectMapper。如果已经创建了一个使用ObjectMapper或XMLMapper的bean,那么Swagger将使用这个实例,并被破坏。解决方法是为ObjectMapper创建一个bean并使用@Primary注释,然后创建您想要使用的XMLMapper bean

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;

@Configuration
public class MessageResponseXMLMapper {


      @Bean
      @Primary
      public ObjectMapper customObjectMapper() {
            return new ObjectMapper();
      }


      @Bean(name="customXmlMapper")
      public XmlMapper customXmlMapper() {
            return new Jackson2ObjectMapperBuilder()
                    .indentOutput(true)
                    .createXmlMapper(true)
                    .propertyNamingStrategy(PropertyNamingStrategy.UPPER_CAMEL_CASE)
                    .build();     }   

}

希望这有助于解决此解决方案的挑战-它假定
ObjectMapper
的其他用法比Swagger代码更具可配置性。如果两个代码库都只是从bean注册表中选择“默认”代码,我们如何解决这个问题?
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
import com.fasterxml.jackson.dataformat.xml.XmlMapper;

@Configuration
public class MessageResponseXMLMapper {


      @Bean
      @Primary
      public ObjectMapper customObjectMapper() {
            return new ObjectMapper();
      }


      @Bean(name="customXmlMapper")
      public XmlMapper customXmlMapper() {
            return new Jackson2ObjectMapperBuilder()
                    .indentOutput(true)
                    .createXmlMapper(true)
                    .propertyNamingStrategy(PropertyNamingStrategy.UPPER_CAMEL_CASE)
                    .build();     }   

}