java—在带有自定义消息转换器的SpringMVC项目中使用springdoc

jmo0nnb3  于 2021-07-03  发布在  Java
关注(0)|答案(1)|浏览(402)

将springdoc添加到现有的springmvc restapi项目会导致定制消息转换器出现问题。项目配置自定义消息转换器,以将日期输出设置为特定格式,并删除包含 null . 这是使用 @Configuration 扩展的带注解类 WebMvcConfigurationSupport 它覆盖了 configureMessageConverters 并添加自定义转换器和默认转换器( addDefaultHttpMessageConverters ).
为了将springdoc(版本1.5.1)添加到项目中,我按照这里的说明进行了操作,只是注册了一个小改动 org.springdoc.webmvc.ui.SwaggerConfig.class 而不是列出的 org.springdoc.ui.SwaggerConfig.class 因为后者并不存在。这需要添加 @EnableWebMvc 注解,这反过来又需要实现 WebMvcConfigurer 而不是延伸 WebMvcConfigurationSupport . 通过使用 WebMvcConfigurer 我不能添加默认的转换器,通过只使用自定义转换器,springdocjson被转义,因此无效。如果 @EnableWebMvc 未添加注解。swagger-ui.html页不可用。
我想用 @EnableWebMvc ,实施 WebMvcConfigurer 使用自定义转换器,但通过以某种方式或通过其他方式添加默认转换器来防止springdocjson转义。或者我想使用“旧”配置,所以 WebMvcConfigurationSupport 而不是添加 @EnableWebMvc 注解,但不知何故使swagger-ui.html页面可访问。
如何实现这两种选择之一?
编辑1
已使用注解对配置类进行了注解 @EnableWebMvc 和工具 WebMvcConfigurer ,这样就可以访问swagger-ui.html页面。使用以下方法定制转换器:

@Override
public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
  List<MediaType> supportedMediaTypes = new ArrayList<>();
  supportedMediaTypes.add(MediaType.APPLICATION_JSON);

  for (HttpMessageConverter<?> converter : converters) {
    if (converter instanceof MappingJackson2HttpMessageConverter) {
      MappingJackson2HttpMessageConverter jsonMessageConverter = (MappingJackson2HttpMessageConverter) converter;
      ObjectMapper objectMapper = jsonMessageConverter.getObjectMapper();
      objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
      objectMapper.setTimeZone(TimeZone.getTimeZone(timeZone));
      jsonMessageConverter.setSupportedMediaTypes(supportedMediaTypes);
      break;
    }
  }
}

此解决方案基于此处接受的答案。
这样,所有来自端点的json响应都不会被忽略 null 字段并使用配置的时区,springdocjson也不会转义。
我试过配置一个 MappingJackson2HttpMessageConverter bean并将其添加到转换器列表中,但是springdocjson会被转义,从而使其无效。

@Override
public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
  converters.add(customJsonConverter());
  // It's possible to add a converter at a specific index, that to doesn't
  // prevent the springdoc JSON from being escaped.
  // converters.add(1, customJsonConverter());
}

编辑2
随着第一次编辑中提到的更改,一切似乎都正常工作。使用tomcat 7。但是,应用程序将使用tomcat9进行部署,这将导致一个(有些不伦不类的)错误。

[2020-12-15 04:41:42,964] Artifact <artifact-name>:war: Artifact is being deployed, please wait...
15-Dec-2020 16:41:51.791 INFO [RMI TCP Connection(2)-127.0.0.1] org.apache.jasper.servlet.TldScanner.scanJars At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
15-Dec-2020 16:41:52.187 SEVERE [RMI TCP Connection(2)-127.0.0.1] org.apache.tomcat.util.modeler.BaseModelMBean.invoke Exception invoking method [manageApp]
    java.lang.IllegalStateException: Error starting child
        at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:720)
        at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:690)
        at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:705)
        at org.apache.catalina.startup.HostConfig.manageApp(HostConfig.java:1727)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at org.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:288)
        at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:819)
        at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:801)
        at org.apache.catalina.mbeans.MBeanFactory.createStandardContext(MBeanFactory.java:456)
        at org.apache.catalina.mbeans.MBeanFactory.createStandardContext(MBeanFactory.java:405)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at org.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:288)
        at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:819)
        at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:801)
        at com.sun.jmx.remote.security.MBeanServerAccessController.invoke(MBeanServerAccessController.java:468)
        at javax.management.remote.rmi.RMIConnectionImpl.doOperation(RMIConnectionImpl.java:1468)
        at javax.management.remote.rmi.RMIConnectionImpl.access$300(RMIConnectionImpl.java:76)
        at javax.management.remote.rmi.RMIConnectionImpl$PrivilegedOperation.run(RMIConnectionImpl.java:1309)
        at java.security.AccessController.doPrivileged(Native Method)
        at javax.management.remote.rmi.RMIConnectionImpl.doPrivilegedOperation(RMIConnectionImpl.java:1408)
        at javax.management.remote.rmi.RMIConnectionImpl.invoke(RMIConnectionImpl.java:829)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:357)
        at sun.rmi.transport.Transport$1.run(Transport.java:200)
        at sun.rmi.transport.Transport$1.run(Transport.java:197)
        at java.security.AccessController.doPrivileged(Native Method)
        at sun.rmi.transport.Transport.serviceCall(Transport.java:196)
        at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:573)
        at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:834)
        at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.lambda$run$0(TCPTransport.java:688)
        at java.security.AccessController.doPrivileged(Native Method)
        at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:687)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
        at java.lang.Thread.run(Thread.java:748)
    Caused by: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/<application-context>]]
        at org.apache.catalina.util.LifecycleBase.handleSubClassException(LifecycleBase.java:440)
        at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:198)
        at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:717)
        ... 43 more
    Caused by: java.lang.ExceptionInInitializerError
        at org.springframework.boot.logging.LoggingSystemFactory.lambda$fromSpringFactories$0(LoggingSystemFactory.java:44)
        at org.springframework.boot.logging.DelegatingLoggingSystemFactory.getLoggingSystem(DelegatingLoggingSystemFactory.java:41)
        at org.springframework.boot.logging.LoggingSystem.get(LoggingSystem.java:159)
        at org.springframework.boot.logging.log4j2.SpringBootConfigurationFactory.getConfiguration(SpringBootConfigurationFactory.java:60)
        at org.apache.logging.log4j.core.config.ConfigurationFactory$Factory.getConfiguration(ConfigurationFactory.java:453)
        at org.apache.logging.log4j.core.config.ConfigurationFactory$Factory.getConfiguration(ConfigurationFactory.java:385)
        at org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:238)
        at org.apache.logging.log4j.core.config.Configurator.initialize(Configurator.java:158)
        at org.apache.logging.log4j.web.Log4jWebInitializerImpl.initializeNonJndi(Log4jWebInitializerImpl.java:174)
        at org.apache.logging.log4j.web.Log4jWebInitializerImpl.start(Log4jWebInitializerImpl.java:112)
        at org.apache.logging.log4j.web.Log4jServletContainerInitializer.onStartup(Log4jServletContainerInitializer.java:57)
        at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5128)
        at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
        ... 44 more
    Caused by: java.lang.NullPointerException
        at org.springframework.core.io.support.SpringFactoriesLoader.loadSpringFactories(SpringFactoriesLoader.java:126)
        at org.springframework.core.io.support.SpringFactoriesLoader.loadFactoryNames(SpringFactoriesLoader.java:122)
        at org.springframework.core.io.support.SpringFactoriesLoader.loadFactories(SpringFactoriesLoader.java:98)
        at org.springframework.boot.logging.LoggingSystemFactory.lambda$fromSpringFactories$0(LoggingSystemFactory.java:44)
        at org.springframework.boot.logging.DelegatingLoggingSystemFactory.getLoggingSystem(DelegatingLoggingSystemFactory.java:41)
        at org.springframework.boot.logging.LoggingSystem.get(LoggingSystem.java:159)
        at org.springframework.boot.logging.log4j2.SpringBootConfigurationFactory.getConfiguration(SpringBootConfigurationFactory.java:60)
        at org.apache.logging.log4j.core.config.ConfigurationFactory$Factory.getConfiguration(ConfigurationFactory.java:453)
        at org.apache.logging.log4j.core.config.ConfigurationFactory$Factory.getConfiguration(ConfigurationFactory.java:385)
        at org.apache.logging.log4j.core.config.ConfigurationFactory.getConfiguration(ConfigurationFactory.java:260)
        at org.apache.logging.log4j.core.LoggerContext.reconfigure(LoggerContext.java:615)
        at org.apache.logging.log4j.core.LoggerContext.reconfigure(LoggerContext.java:636)
        at org.apache.logging.log4j.core.LoggerContext.start(LoggerContext.java:231)
        at org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:153)
        at org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:45)
        at org.apache.logging.log4j.LogManager.getContext(LogManager.java:194)
        at org.apache.logging.log4j.spi.AbstractLoggerAdapter.getContext(AbstractLoggerAdapter.java:121)
        at org.apache.logging.log4j.jcl.LogAdapter.getContext(LogAdapter.java:39)
        at org.apache.logging.log4j.spi.AbstractLoggerAdapter.getLogger(AbstractLoggerAdapter.java:46)
        at org.apache.logging.log4j.jcl.LogFactoryImpl.getInstance(LogFactoryImpl.java:40)
        at org.apache.logging.log4j.jcl.LogFactoryImpl.getInstance(LogFactoryImpl.java:55)
        at org.apache.commons.logging.LogFactory.getLog(LogFactory.java:685)
        at org.springframework.core.io.support.SpringFactoriesLoader.<clinit>(SpringFactoriesLoader.java:71)
        ... 57 more
15-Dec-2020 16:41:52.189 SEVERE [RMI TCP Connection(2)-127.0.0.1] org.apache.tomcat.util.modeler.BaseModelMBean.invoke Exception invoking method [createStandardContext]
    javax.management.RuntimeOperationsException: Exception invoking method [manageApp]
        at org.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:297)
        at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:819)
        at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:801)
        at org.apache.catalina.mbeans.MBeanFactory.createStandardContext(MBeanFactory.java:456)
        at org.apache.catalina.mbeans.MBeanFactory.createStandardContext(MBeanFactory.java:405)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at org.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:288)
        at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:819)
        at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:801)
        at com.sun.jmx.remote.security.MBeanServerAccessController.invoke(MBeanServerAccessController.java:468)
        at javax.management.remote.rmi.RMIConnectionImpl.doOperation(RMIConnectionImpl.java:1468)
        at javax.management.remote.rmi.RMIConnectionImpl.access$300(RMIConnectionImpl.java:76)
        at javax.management.remote.rmi.RMIConnectionImpl$PrivilegedOperation.run(RMIConnectionImpl.java:1309)
        at java.security.AccessController.doPrivileged(Native Method)
        at javax.management.remote.rmi.RMIConnectionImpl.doPrivilegedOperation(RMIConnectionImpl.java:1408)
        at javax.management.remote.rmi.RMIConnectionImpl.invoke(RMIConnectionImpl.java:829)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at sun.rmi.server.UnicastServerRef.dispatch(UnicastServerRef.java:357)
        at sun.rmi.transport.Transport$1.run(Transport.java:200)
        at sun.rmi.transport.Transport$1.run(Transport.java:197)
        at java.security.AccessController.doPrivileged(Native Method)
        at sun.rmi.transport.Transport.serviceCall(Transport.java:196)
        at sun.rmi.transport.tcp.TCPTransport.handleMessages(TCPTransport.java:573)
        at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run0(TCPTransport.java:834)
        at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.lambda$run$0(TCPTransport.java:688)
        at java.security.AccessController.doPrivileged(Native Method)
        at sun.rmi.transport.tcp.TCPTransport$ConnectionHandler.run(TCPTransport.java:687)
        at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
        at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
        at java.lang.Thread.run(Thread.java:748)
    Caused by: java.lang.IllegalStateException: Error starting child
        at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:720)
        at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:690)
        at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:705)
        at org.apache.catalina.startup.HostConfig.manageApp(HostConfig.java:1727)
        at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
        at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
        at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
        at java.lang.reflect.Method.invoke(Method.java:498)
        at org.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:288)
        ... 35 more
    Caused by: org.apache.catalina.LifecycleException: Failed to start component [StandardEngine[Catalina].StandardHost[localhost].StandardContext[/<application-context>]]
        at org.apache.catalina.util.LifecycleBase.handleSubClassException(LifecycleBase.java:440)
        at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:198)
        at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:717)
        ... 43 more
    Caused by: java.lang.ExceptionInInitializerError
        at org.springframework.boot.logging.LoggingSystemFactory.lambda$fromSpringFactories$0(LoggingSystemFactory.java:44)
        at org.springframework.boot.logging.DelegatingLoggingSystemFactory.getLoggingSystem(DelegatingLoggingSystemFactory.java:41)
        at org.springframework.boot.logging.LoggingSystem.get(LoggingSystem.java:159)
        at org.springframework.boot.logging.log4j2.SpringBootConfigurationFactory.getConfiguration(SpringBootConfigurationFactory.java:60)
        at org.apache.logging.log4j.core.config.ConfigurationFactory$Factory.getConfiguration(ConfigurationFactory.java:453)
        at org.apache.logging.log4j.core.config.ConfigurationFactory$Factory.getConfiguration(ConfigurationFactory.java:385)
        at org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:238)
        at org.apache.logging.log4j.core.config.Configurator.initialize(Configurator.java:158)
        at org.apache.logging.log4j.web.Log4jWebInitializerImpl.initializeNonJndi(Log4jWebInitializerImpl.java:174)
        at org.apache.logging.log4j.web.Log4jWebInitializerImpl.start(Log4jWebInitializerImpl.java:112)
        at org.apache.logging.log4j.web.Log4jServletContainerInitializer.onStartup(Log4jServletContainerInitializer.java:57)
        at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5128)
        at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:183)
        ... 44 more
    Caused by: java.lang.NullPointerException
        at org.springframework.core.io.support.SpringFactoriesLoader.loadSpringFactories(SpringFactoriesLoader.java:126)
        at org.springframework.core.io.support.SpringFactoriesLoader.loadFactoryNames(SpringFactoriesLoader.java:122)
        at org.springframework.core.io.support.SpringFactoriesLoader.loadFactories(SpringFactoriesLoader.java:98)
        at org.springframework.boot.logging.LoggingSystemFactory.lambda$fromSpringFactories$0(LoggingSystemFactory.java:44)
        at org.springframework.boot.logging.DelegatingLoggingSystemFactory.getLoggingSystem(DelegatingLoggingSystemFactory.java:41)
        at org.springframework.boot.logging.LoggingSystem.get(LoggingSystem.java:159)
        at org.springframework.boot.logging.log4j2.SpringBootConfigurationFactory.getConfiguration(SpringBootConfigurationFactory.java:60)
        at org.apache.logging.log4j.core.config.ConfigurationFactory$Factory.getConfiguration(ConfigurationFactory.java:453)
        at org.apache.logging.log4j.core.config.ConfigurationFactory$Factory.getConfiguration(ConfigurationFactory.java:385)
        at org.apache.logging.log4j.core.config.ConfigurationFactory.getConfiguration(ConfigurationFactory.java:260)
        at org.apache.logging.log4j.core.LoggerContext.reconfigure(LoggerContext.java:615)
        at org.apache.logging.log4j.core.LoggerContext.reconfigure(LoggerContext.java:636)
        at org.apache.logging.log4j.core.LoggerContext.start(LoggerContext.java:231)
        at org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:153)
        at org.apache.logging.log4j.core.impl.Log4jContextFactory.getContext(Log4jContextFactory.java:45)
        at org.apache.logging.log4j.LogManager.getContext(LogManager.java:194)
        at org.apache.logging.log4j.spi.AbstractLoggerAdapter.getContext(AbstractLoggerAdapter.java:121)
        at org.apache.logging.log4j.jcl.LogAdapter.getContext(LogAdapter.java:39)
        at org.apache.logging.log4j.spi.AbstractLoggerAdapter.getLogger(AbstractLoggerAdapter.java:46)
        at org.apache.logging.log4j.jcl.LogFactoryImpl.getInstance(LogFactoryImpl.java:40)
        at org.apache.logging.log4j.jcl.LogFactoryImpl.getInstance(LogFactoryImpl.java:55)
        at org.apache.commons.logging.LogFactory.getLog(LogFactory.java:685)
        at org.springframework.core.io.support.SpringFactoriesLoader.<clinit>(SpringFactoriesLoader.java:71)
        ... 57 more
[2020-12-15 04:41:52,197] Artifact <artifact-name>:war: Error during artifact deployment. See server log for details.
15-Dec-2020 16:41:52.702 INFO [Catalina-utility-2] org.apache.catalina.startup.HostConfig.deployDirectory Deploying web application directory [/Users/dopheidp/Programs/apache-tomcat-9.0.37/webapps/manager]
15-Dec-2020 16:41:52.775 INFO [Catalina-utility-2] org.apache.jasper.servlet.TldScanner.scanJars At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
15-Dec-2020 16:41:52.794 INFO [Catalina-utility-2] org.apache.catalina.startup.HostConfig.deployDirectory Deployment of web application directory [/Users/dopheidp/Programs/apache-tomcat-9.0.37/webapps/manager] has finished in [92] ms

工件名称和应用程序上下文已分别替换为和,以确保安全。
tomcat9下的异常似乎是由添加到项目中的springdoc和/或spring引导依赖项引起的。将springdoc从 1.5.11.4.8 和Spring Boot 2.4.02.3.5.RELEASE 在tomcat9(和tomcat7)下一切都正常。

ybzsozfc

ybzsozfc1#

springdoc已经使用sprindoc网站上的说明添加到springmvc项目中。这意味着添加 @EnableWebMvc 注解和更改扩展的配置类 WebMvcConfigurationSupport 实施 WebMvcConfigurer . 所以

public class WebMvcConfig extends WebMvcConfigurationSupport { ... }

变成

@EnableWebMvc
public class WebMvcConfig implements WebMvcConfigurationSupport { ... }

这些更改导致用于全局配置json响应的消息转换器中断。要解决此问题,将删除以前用于配置消息转换器的方法。

@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
  converters.add(jsonConverter());
  addDefaultHttpMessageConverters(converters);
}

@Bean
public MappingJackson2HttpMessageConverter jsonConverter() {
  List<MediaType> supportedMediaTypes = new ArrayList<>();
  supportedMediaTypes.add(MediaType.APPLICATION_JSON);

  Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder();
  builder.serializationInclusion(JsonInclude.Include.NON_NULL);
  builder.timeZone(TimeZone.getTimeZone(timeZone));

  MappingJackson2HttpMessageConverter jsonConverter = new MappingJackson2HttpMessageConverter(builder.build());
  jsonConverter.setSupportedMediaTypes(supportedMediaTypes);
  return jsonConverter;
}

并替换为以下方法。配置消息转换器的其他方法似乎不起作用,请参阅有关已尝试的配置变体的问题。

public void extendMessageConverters(List<HttpMessageConverter<?>> converters) {
  for (HttpMessageConverter<?> converter : converters) {
    if (converter instanceof MappingJackson2HttpMessageConverter) {
      MappingJackson2HttpMessageConverter jsonMessageConverter = (MappingJackson2HttpMessageConverter) converter;
      ObjectMapper objectMapper = jsonMessageConverter.getObjectMapper();
      objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
      objectMapper.setTimeZone(TimeZone.getTimeZone(timeZone));
      jsonMessageConverter.setSupportedMediaTypes(supportedMediaTypes);
      break;
    }
  }
}

项目当前使用springdoc版本 1.4.8 和spring boot版本 2.3.5.RELEASE 作为依赖项,使用最新的可用版本springdoc 1.5.1 和Spring Boot 2.4.0 在撰写本文时,tomcat9下出现了一个异常,阻止了项目的启动。

相关问题