带有空字符串的Jackson com.fasterxml.jackson.databind.exc.MismatchedInputException [英] Jackson com.fasterxml.jackson.databind.exc.MismatchedInputException with empty string

查看:1983
本文介绍了带有空字符串的Jackson com.fasterxml.jackson.databind.exc.MismatchedInputException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

具有以下json片段:

Having following json fragment:

"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):没有在[Source]中从String值('')\ n反序列化的String-argument构造函数/工厂方法:未知;行:-1,列:-1]

I got com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot construct instance of com.xxx.Address (although at least one Creator exists): no String-argument constructor/factory method to deserialize from String value ('')\n at [Source: UNKNOWN; line: -1, column: -1]

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

Which seems pretty obvious, empty string cannot be deserialized as Address. The issue here is sometimes response comes as empty string and others as complete Address Objec.

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

How can I configure Jackson objectMapper to tell to ignore if empty string is present??

此回复来自外部来源,因此我无法修改该回复.

This response is from an external source, so I cannot modify that response.

这是我的对象映射器配置

Here is my Object Mapper configuration

@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模式

Additional note: Pojo classes are generated using jsonschema2pojo gradle plugin and are based on json schemas

推荐答案

按照@Dmitry Zagorulkin的建议,将DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT转到true.有关更多详细信息,请检查文档.

As suggested by @Dmitry Zagorulkin, turn DeserializationFeature.ACCEPT_EMPTY_STRING_AS_NULL_OBJECT to true. For more details, check docs.

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

Here is the code I tried to replicate the issue and fixed by adding the line highlighted by comment.

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;
}

这篇关于带有空字符串的Jackson com.fasterxml.jackson.databind.exc.MismatchedInputException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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