我使用@JsonProperty
通过Spring RestTemplate
的exchange
序列化来自JSON的数据。
@JsonProperty("ip_address")
public String ipAddress;
@JsonProperty("port")
public Integer port;
我需要此属性来识别属性名称的大写和小写版本,即应识别在@JsonProperty
中设置的“ip_address”和“IP_ADDRESS”。
我尝试了以下但没有工作:
@JsonFormat(with=JsonFormat.Feature.ACCEPT_CASE_INSENSITIVE_PROPERTIES)
不适用于GitHub中报道的existing issue。应用于模型类中的每个属性时也不起作用。MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES
on an ObjectMapper
bean。ObjectMapper
and RestTemplate
甚至没有使用组合代码与之前的项目示例。这三个只有各自属性的null
值,因为我禁用了模板的未知属性(例如,不同的字母大小写)的错误。
您可以告诉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")
我能够在不改变我原来的pojo类@JsonProperty配置的情况下完成这项工作。使用您链接到的对象映射器和静态模板示例,而不是属性命名策略使用不区分大小写的映射器功能
ObjectMapper mapper = new ObjectMapper(); mapper.configure(MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES,true);