使用Spring RestTemplate忽略使用@JsonProperty声明的JSON属性情况

问题描述 投票:0回答:2

我使用@JsonProperty通过Spring RestTemplateexchange序列化来自JSON的数据。

@JsonProperty("ip_address")
public String ipAddress;

@JsonProperty("port")
public Integer port;

我需要此属性来识别属性名称的大写和小写版本,即应识别在@JsonProperty中设置的“ip_address”和“IP_ADDRESS”。

我尝试了以下但没有工作:

这三个只有各自属性的null值,因为我禁用了模板的未知属性(例如,不同的字母大小写)的错误。

java json spring serialization resttemplate
2个回答
0
投票

您可以告诉jackson将您的所有属性名称转换为例如SNAKE_CASE变种并相应地设置你的@JsonProperty

例:

在spring boot中,在application.properties中设置该属性

spring.jackson.property-naming-strategy=SNAKE_CASE

或者您可以为单个类启用它并使用以下内容注释该类:

@JsonNaming(PropertyNamingStrategy.SNAKE_CASE.class)

然后设置@JsonProperty:

@JsonProperty(vale="ip_address")

0
投票

我能够在不改变我原来的pojo类@JsonProperty配置的情况下完成这项工作。使用您链接到的对象映射器和静态模板示例,而不是属性命名策略使用不区分大小写的映射器功能

ObjectMapper mapper = new ObjectMapper(); mapper.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES,true);

© www.soinside.com 2019 - 2024. All rights reserved.