简体   繁体   English

在spring boot中反序列化JSON,其中一个字段是两个字段的组合

[英]Desrialzing JSON in spring boot, where a field is the combination of two fields

I have a controller with the following signiture:我有一个带有以下签名的 controller:

public HttpEntity<RepresentationModel> confirmRegistration(@Valid @RequestBody RegistrationRequest request{}

the RegistrationRequest Json looks like this RegistrationRequest Json 看起来像这样

{
//other fields
"countryCode":"44",
"mobileNumber": "07545878096"
}

I am trying to write a custom deserializer for this json我正在尝试为此 json 编写自定义解串器

My mobileNumber class looks like this:我的手机号码 class 如下所示:

@Getter
@Setter
@EqualsAndHashCode
@ToString
@AllArgsConstructor
public class MobileNumber {
  @JsonProperty("mobilePhoneNumber")
  @JsonAlias("mobileNumber")
  String number;
  @JsonProperty(value = "countryCode", defaultValue = "44")
  String countryCode;
}

and a request object like so:和请求 object 像这样:

public class RegistrationRequest {
//other fields
  @JsonDeserialize(using = MobileNumberDeserializer.class)
  @MobileNumberValidator
  private final MobileNumber mobilePhoneNumber;

}

where the MobileNumberDeserializer looks like this: MobileNumberDeserializer 看起来像这样:

public class ContactNumberDeserializer extends StdDeserializer<MobileNumber> {

  private static final long serialVersionUID = 1L;

  protected ContactNumberDeserializer() {
    super(MobileNumber.class);
  }


  @Override
  public MobileNumber deserialize(JsonParser jsonParser, DeserializationContext ctxt)
      throws IOException {

    JsonNode node = jsonParser.getCodec().readTree(jsonParser);
    String mobileNumber = "";
    if (node.has("mobilePhoneNumber")) {
      mobileNumber = node.get("mobilePhoneNumber").asText();
    } else if (node.has("phoneNumber")) {
      mobileNumber = node.get("phoneNumber").asText();
    } else if (node.has("mobileNumber")) {
      mobileNumber = node.get("mobileNumber").asText();
    }
    String countryCode = node.get("countryCode").asText();

    return new MobileNumber(mobileNumber, countryCode);

  }

when the ContactNumberDeserializer is invoked by the controller, jsonParser.getCodec().readTree(jsonParser);当 controller 调用 ContactNumberDeserializer 时, jsonParser.getCodec().readTree(jsonParser); it's just the mobilePhoneNumber node and cant access countryCode .它只是mobilePhoneNumber节点,无法访问countryCode

Quick check if ContactNumber and MobileNumber same classes.快速检查 ContactNumber 和 MobileNumber 是否相同。

Ideally it should be理想情况下应该是

public class ContactNumberDeserializer extends StdDeserializer<MobileNumber {公共 class ContactNumberDeserializer 扩展 StdDeserializer<MobileNumber {

In your MobileNumber class:在您的手机号码 class 中:

@Getter
@Setter
@EqualsAndHashCode
@ToString
@AllArgsConstructor
public class MobileNumber {
  @JsonProperty("mobilePhoneNumber")
  @JsonAlias("mobileNumber")
  String number;
  @JsonProperty("countryCode")
  String countryCode = "44";
}

Update JsonProperty annotation like above for countryCode.像上面那样为 countryCode 更新 JsonProperty 注释。 Hope it helps!希望能帮助到你!

You don't need to write ContactNumberDeserializer .您不需要编写ContactNumberDeserializer If you wrote your class MobileNumber it would simply work.如果您编写MobileNumber手机号码,它就可以正常工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM