@JsonIgnoreProperties(ignoreUnknown = true) 没有预期的行为

@JsonIgnoreProperties(ignoreUnknown = true) does not have expected behaviour

我正在开发一项从外部获取地址的功能,需要将其解析为 Java class:

@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
public class AddressDTO {
    @JsonProperty(value = "Name")
    private String name;
    @JsonProperty("Street")
    private String street;
    @JsonProperty("City")
    private String city;
    @JsonProperty("PostalCode")
    private String postalCode;
    @JsonProperty("CountryISO")
    private String countryIso;
    @JsonProperty("Region")
    private String region;
}

和这个 os 我如何转换它:

        try {
            final var type = new TypeReference<AddressDTO>() {
            };
            final var address = (AddressDTO) new ObjectMapper()
                    .readerFor(type)
                    .readValue(jsonAddress);

            return mapper.toAddress(address);
        } catch (final Exception e) {
            LOG.error("unable to parse the variable 'address'", e);
            throw new BadFormatException();
        }

我只想要 ose 个字段,但有时来自外部的有效负载可能还包含“Street2”、“Name2”或其他字段。当这件事发生时, 抛出此异常:com.fasterxml.jackson.databind.exc.UnrecognizedPropertyException: Unrecognized field "Street2".

在搜索了如何解决这个问题之后,我得到了 os 的 属性 @JsonIgnoreProperties(ignoreUnknown = true),支持os忽略未知字段:

@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
@JsonIgnoreProperties(ignoreUnknown = true) // Some old value might contain additional old fields 
public class AddressDTO {
    @JsonProperty(value = "Name")
    private String name;
    @JsonProperty("Street")
    private String street;
    @JsonProperty("City")
    private String city;
    @JsonProperty("PostalCode")
    private String postalCode;
    @JsonProperty("CountryISO")
    private String countryIso;
    @JsonProperty("Region")
    private String region;
}

当我放置该注释时,我不再有上述异常,但我遇到了一个新问题:所有其他字段都被忽略了:

{
  "NonExistingKey1": "foo",
  "Non_existing_key2": "bar"
}

变成了这样:.

如何注释我的 class 以便它在字段多于预期时不会抛出异常,但仍会检查所有属性是否存在且不为空?

首先你可以使用constructor-based 属性赋值

@Data
@Builder
@NoArgsConstructor
@JsonIgnoreProperties(ignoreUnknown = true) // Some old value might contain additional old fields 
public class AddressDTO {
    private String name;
    private String street;
    private String city;
    private String postalCode;
    private String countryIso;
    private String region;

    public AddressDTO(@JsonProperty(value = "Name") String name,
                      @JsonProperty(value = "Street") String street,
                      @JsonProperty("City") String city,
                      @JsonProperty("PostalCode") String postalCode,
                      @JsonProperty("CountryISO") String countryIso,
                      @JsonProperty("Region") String region) {
        this.name = name;
        this.street = street;
        this.city = city;
        this.postalCode = postalCode;
        this.countryIso = countryIso;
        this.region = region;
    }
}

然后您可以配置 ObjectMapper 以在缺少属性时失败:

objectMapper.configure(DeserializationFeature.FAIL_ON_MISSING_CREATOR_PROPERTIES, true);