gpt4 book ai didi

java - 带有空白字符串的Jackson com.fasterxml.jackson.databind.exc.MismatchedInputException

转载 作者:行者123 更新时间:2023-12-03 03:55:19 25 4
gpt4 key购买 nike

具有以下json片段:

"Location": {
"Address": ""
},

还有我的Pojo
public class Address implements Serializable
{

@JsonProperty("City")
private String city;
@JsonProperty("StateProvinceCode")
private String stateProvinceCode;
@JsonProperty("PostalCode")
private String postalCode;
@JsonProperty("CountryCode")
private String countryCode;
private final static long serialVersionUID = -4475717488164417476L;

/**
* No args constructor for use in serialization
*
*/
public Address() {
}

/**
*
* @param city
* @param countryCode
* @param postalCode
* @param stateProvinceCode
*/
public Address(String city, String stateProvinceCode, String postalCode, String countryCode) {
super();
this.city = city;
this.stateProvinceCode = stateProvinceCode;
this.postalCode = postalCode;
this.countryCode = countryCode;
}

getters and settes ...

我有
com.fasterxml.jackson.databind.exc.MismatchedInputException:无法构造 com.xxx.Address实例(尽管至少存在一个Creator):在[来源:UNKNOWN中,没有从字符串值('')\ n反序列化的String参数构造函数/工厂方法;行:-1,列:-1]

这似乎很明显,空字符串不能反序列化为Address。这里的问题是响应有时是空字符串,而其他则是完整的地址对象。

我如何配置Jackson objectMapper告诉忽略是否存在空字符串?

该响应来自外部来源,因此我无法修改该响应。

这是我的对象映射器配置
@Bean
public ObjectMapper objectMapper() {
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new Jdk8Module());
mapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
mapper.configure(DeserializationFeature.FAIL_ON_IGNORED_PROPERTIES, false);
mapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
return mapper;
}

附加说明:Pojo类是使用jsonschema2pojo gradle插件生成的,并且基于json模式

最佳答案

根据@Dmitry Zagorulkin的建议,将DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT转换为true。有关更多详细信息,请检查docs

这是我尝试复制该问题并通过添加注释突出显示的行来解决的代码。

public class JacksonExample2 {

private static String json1 = "{\r\n" +
" \"Location\": {\r\n" +
" \"Address\": \"\"\r\n" +
" }\r\n" +
"}";


public static void main(String[] args) throws JSONException, IOException {
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new Jdk8Module());
mapper.configure(DeserializationFeature.ACCEPT_SINGLE_VALUE_AS_ARRAY, true);
mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
mapper.configure(DeserializationFeature.FAIL_ON_IGNORED_PROPERTIES, false);
// Add this line
mapper.configure(DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT, true);
ParentData parentData = mapper.readValue(json1, ParentData.class);
Address address = new Address("New York", null, null, null);
LocationData locationData = new LocationData(address);
System.out.println(parentData);
}

}

@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@ToString
class ParentData {
@JsonProperty("Location")
private LocationData locationData;
}

@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@ToString
class LocationData {
@JsonProperty("Address")
private Address address;
}

@Getter
@Setter
@AllArgsConstructor
@NoArgsConstructor
@ToString
class Address implements Serializable {

@JsonProperty("City")
private String city;
@JsonProperty("StateProvinceCode")
private String stateProvinceCode;
@JsonProperty("PostalCode")
private String postalCode;
@JsonProperty("CountryCode")
private String countryCode;
private final static long serialVersionUID = -4475717488164417476L;
}

关于java - 带有空白字符串的Jackson com.fasterxml.jackson.databind.exc.MismatchedInputException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60844488/

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