gpt4 book ai didi

java - 使用 WebMvcConfigurationSupport 而不是 WebMvcConfigurerAdapter 返回空值

转载 作者:行者123 更新时间:2023-12-02 09:49:15 25 4
gpt4 key购买 nike

由于 WebMvcConfigurerAdapter 从 Spring 5.0 开始已被弃用,我已将其更改为 WebMvcConfigurationSupport,但我得到的响应为空值。

{
"key": null,
"value": null,
"name":"test"
}

如果我将其改回 WebMvcConfigurerAdapter,我会得到预期的响应:

{
"name":"test"
}

Spring 版本:5.7.0 jackson 版本:2.9.7

我用谷歌搜索了很多东西,但仍然没有运气。我不想使用已弃用的类。添加 WebMvcConfigurationSupport 后尝试删除 @EnableWebMvc 注释,因为它不是必需的。

我已经覆盖了configureMessageConverters,其中我设置了包含NOT_NULL的MappingJackson2HttpMessageConverter

@Configuration
@EnableAspectJAutoProxy
@EnableSwagger2
@PropertySource("classpath:test.properties")
@ComponentScan(basePackages = {"com.test.web"})
public class UmwWebConfig extends WebMvcConfigurationSupport {

@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
ByteArrayHttpMessageConverter bahHumbug = new ByteArrayHttpMessageConverter();
bahHumbug.setSupportedMediaTypes(Collections.singletonList(MediaType.parseMediaType("application/pdf")));
converters.add(bahHumbug);

final MappingJackson2HttpMessageConverter converter = new MappingJackson2HttpMessageConverter();
final ObjectMapper objectMapper = new ObjectMapper();
objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
objectMapper.getFactory().configure(JsonGenerator.Feature.ESCAPE_NON_ASCII, true);
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
converter.setObjectMapper(objectMapper);
converters.add(converter);
super.configureMessageConverters(converters);
}

想要从响应中删除空字段。如果配置有任何问题,请告诉我。

最佳答案

不要扩展 WebMvcConfigurationSupport,因为这并不等同于扩展已弃用的 WebMvcConfigurerAdapter

而是实现WebMvcConfigurer,这也是javadoc of `WebMvcConfigurerAdapter中的弃用文档中建议的内容。 .

Deprecated. as of 5.0 WebMvcConfigurer has default methods (made possible by a Java 8 baseline) and can be implemented directly without the need for this adapter

所以你的配置类头应该看起来像这样

@Configuration
@EnableAspectJAutoProxy
@EnableSwagger2
@EnableWebMvc
@PropertySource("classpath:test.properties")
@ComponentScan(basePackages = {"com.test.web"})
public class UmwWebConfig implements WebMvcConfigurer { ... }

您需要再次添加@EnableWebMvc(因为它应该`并覆盖/实现您需要使用的接口(interface)方法。在这种情况下可能只有一个。

专业提示:使用 Jackson2ObjectMapperBuilder 构建 ObjectMapper 并使用 MappingJackson2HttpMessageConverter 的构造函数。这可以节省创建额外的ObjectMapper(这发生在默认构造函数中)。

@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
ByteArrayHttpMessageConverter bahHumbug = new ByteArrayHttpMessageConverter();
bahHumbug.setSupportedMediaTypes(Collections.singletonList(MediaType.parseMediaType("application/pdf")));
converters.add(bahHumbug);

final ObjectMapper objectMapper =
Jackson2ObjectMapperBuilder.json()
.serializationInclusion(JsonInclude.Include.NON_NULL)
.failOnUnknownProperties(true)
.featuresToEnable(JsonGenerator.Feature.ESCAPE_NON_ASCII)
.build();

converters.add(new MappingJackson2HttpMessageConverter(objectMapper);
super.configureMessageConverters(converters);
}

关于java - 使用 WebMvcConfigurationSupport 而不是 WebMvcConfigurerAdapter 返回空值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56434909/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com