忽略使用 @JsonProperty 和 Spring RestTemplate 声明的 JSON 属性情况 [英] Ignore JSON attribute cases declared with @JsonProperty with Spring RestTemplate

查看:355
本文介绍了忽略使用 @JsonProperty 和 Spring RestTemplate 声明的 JSON 属性情况的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 @JsonProperty 通过 Spring RestTemplateexchange 从 JSON 序列化数据.

I use @JsonProperty to serialize data from a JSON through Spring RestTemplate's exchange.

@JsonProperty("ip_address")
public String ipAddress;

@JsonProperty("port")
public Integer port;

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

I need this property to recognize both upper and lowercase versions of the attribute names i.e. "ip_address" and "IP_ADDRESS" set in @JsonProperty should be recognized.

我尝试了以下方法,但没有任何效果:

I have tried the following and none worked:

  • @JsonFormat(with=JsonFormat.Feature.ACCEPT_CASE_INSENSITIVE_PROPERTIES) 应用于类级别不适用于 现有问题 在 GitHub 中报告.应用于模型类中的每个属性时也不起作用.

  • @JsonFormat(with=JsonFormat.Feature.ACCEPT_CASE_INSENSITIVE_PROPERTIES) applied on a class level does not work with existing issue reported in GitHub. Also does not work when applied to each attribute in the model class.

ObjectMapper bean 上使用 MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIESa>.

use MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES on an ObjectMapper bean.

本示例使用 ObjectMapperRestTemplate即使使用组合代码与之前项目上的示例也不起作用.

This example using ObjectMapper and RestTemplate also didn't work even with a combination code with the example on the item before.

所有三个都只有它们各自属性的 null 值,因为我禁用了模板的未知属性(即不同的字母大小写)的错误.

All three just have null values for their respective attributes because I disabled error on unknown attributes (a.k.a. the different letter case) for the template.

推荐答案

Kishore Diyyana:

Kishore Diyyana:

对于 Spring Boot 应用程序忽略 JSON 属性名称的大小写:

For Spring Boot App to Ignore Case for JSON attributes names:

步骤 1:确保您的 POJO/Domain/Model 类具有带相应参数的构造函数,删除任何零参数构造函数.第 2 步:添加 Jackson lib 作为依赖项例如:

Step 1: Make sure your POJO/Domain/Model class has constructor with respective parameters, remove any zero arg constructor. Step 2: Add Jackson lib as dependency Ex:

<dependency>
            <groupId>com.fasterxml.jackson.module</groupId>
            <artifactId>jackson-module-jaxb-annotations</artifactId>
            <version>2.6.5</version>
        </dependency> 

第 3 步:在 application.properties 文件中启用,如下所示spring.jackson.mapper.accept_case_insensitive_properties=true

Step 3: Enable in application.properties file as below spring.jackson.mapper.accept_case_insensitive_properties=true

这篇关于忽略使用 @JsonProperty 和 Spring RestTemplate 声明的 JSON 属性情况的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆