- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
由于 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/
这是我正在处理的 WebConfig 代码: package hello.config; import org.springframework.context.annotation.Configura
我已经在 Spring MVC 中使用以下代码将 xml 转换为 Java 类。谁能告诉我如何设置 bean 创建的顺序。当我运行以下代码时。 JDBCTemplate bean 在 DataSour
我在我的 Spring Boot 应用程序中成功地使用了 @EnableSpringDataWebSupport 来启用分页、排序等等。然而,在某些时候,我不得不引入一个自定义参数解析器,并使用 Ja
我只是迁移到 spring mvc 版本 5.0.1.RELEASE 但突然在 eclipse STS WebMvcConfigurerAdapter 被标记为已弃用 public class Mvc
我正在尝试在 Spring Boot 应用程序中实现转换器,但由于某种原因,我对 addFormatters(FormatterRegistry formatterRegistry) 的重写从未被调用
注意以前已将其标记为已回答,但是随着我对基本组件的理解得到了改善,我看到我们的最初答案仅解决了空指针,但未解决根本原因。目前,根本原因似乎是缺少对分发侦听器和附加到容器的dwrServlet上上下文的
我想在 Windows 中使用 WebMvcConfigurerAdapter 添加资源处理程序,但在 Linux 中它不起作用,所以我添加了 WebMvcConfigurationSupport。
我想添加资源处理程序。在论坛中,他们使用 WebMvcConfigurationSupport:http://forum.springsource.org/showthread.php?116068-
由于 WebMvcConfigurerAdapter 从 Spring 5.0 开始已被弃用,我已将其更改为 WebMvcConfigurationSupport,但我得到的响应为空值。 { "key
我的应用有以下配置: @EnableTransactionManagement @EnableWebSecurity public class SecurityApiConfiguration e
有2个配置,在不同的jar文件中,我想控制拦截器注册的顺序。一个拦截器可能依赖于另一个拦截器设置的数据。 我已经在 addInterceptors 方法上尝试过 @Order。 @Configurat
我有一个使用 Spring Security 和 Thymeleaf 模板的 Spring Boot 应用程序。当 Controller 由 WebConfigurerAdapter 的子类管理时,我
我可以同时使用以下配置吗? 和 @Configuration @EnableWebMvc public class ResourceMappingsAdapter extends WebMvcCon
我想将两个配置合并为一个配置。这是它们目前的样子: WebMvcConfigurerAdapter @Configuration @EnableWebMvc @Import(...) public c
我有一个用于 web 的 Spring Boot hello world 和一些配置混淆: Spring 版本:1.2.6.RELEASE 我的项目结构: 我需要提供一些静态内容,所以我决定在一些自定
我是一名优秀的程序员,十分优秀!