1

我有两个班级PaymentDetailsDTOUserDTO. 我只想序列化present inside的id字段(一个Long)。我正在使用 Jackson 注释 和.UserDTOPaymentDetailsDTO@JsonIdentityInfo@JsonIdentityReference(alwaysAsId = true)

问题是当内部idUserDTOnull时,序列化以空指针异常结束(但id不为null时有效)。

不确定这是否是它应该表现的方式(如果是,为什么)或者我错过了什么。

相反,我期望的是,如果id 为 null,只需编写 null 或不包含键(取决于@JsonInclude注释是否允许 null)。

以下是有问题的代码。

付款详情DTO

@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@JsonInclude(JsonInclude.Include.NON_NULL)
public class PaymentDetailsDTO {

    private Long id;
    @NotNull
    @Builder.Default
    private Long appId = 10151L;

    // Represents user of payment system with information to find it on client
    @JsonProperty("userId")
    @JsonIdentityReference(alwaysAsId = true)
    private UserDTO userDTO;

    // Removed for normalization
    @JsonProperty(access = Access.READ_ONLY)
    private int reqId;

    // @DecimalMin("1.00")
    @NotNull
    private double amountDue;
    
    ...
}

用户DTO

@Data
@Builder(toBuilder = true)
@NoArgsConstructor
@AllArgsConstructor
@JsonIdentityInfo(
    generator = ObjectIdGenerators.PropertyGenerator.class,
    property = "id"
)
public class UserDTO {
    private Long id;
    // userId of the user on specific client
    @NotNull
    private Long clientUserId;
    // clientName like "eYRC", "eYIC", "mooc"
    @NotBlank
    private String clientName;
    @NotBlank
    private String name;
    private String emailId;
   
    ...
}

这是个例外

com.fasterxml.jackson.databind.JsonMappingException: (was java.lang.NullPointerException) (through reference chain: com.eyantra.payment.features.payment.data.dto.PaymentDetailsDTO["userId"])

    at com.fasterxml.jackson.databind.JsonMappingException.wrapWithPath(JsonMappingException.java:394)
    at com.fasterxml.jackson.databind.JsonMappingException.wrapWithPath(JsonMappingException.java:353)
    at com.fasterxml.jackson.databind.ser.std.StdSerializer.wrapAndThrow(StdSerializer.java:316)
    at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:730)
    at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:166)
    at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider._serialize(DefaultSerializerProvider.java:480)
    at com.fasterxml.jackson.databind.ser.DefaultSerializerProvider.serializeValue(DefaultSerializerProvider.java:319)
    at com.fasterxml.jackson.databind.ObjectMapper._configAndWriteValue(ObjectMapper.java:4094)
    at com.fasterxml.jackson.databind.ObjectMapper.writeValueAsString(ObjectMapper.java:3404)
    at com.eyantra.payment.features.payment.data.dto.PaymentDetailsDTOTest.Serialize_IncludeOnlyUserID_ReturnsJson(PaymentDetailsDTOTest.java:27)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at java.base/jdk.internal.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
    at java.base/jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    ...
    at com.intellij.junit5.JUnit5IdeaTestRunner.startRunnerWithArgs(JUnit5IdeaTestRunner.java:69)
    at com.intellij.rt.junit.IdeaTestRunner$Repeater.startRunnerWithArgs(IdeaTestRunner.java:33)
    at com.intellij.rt.junit.JUnitStarter.prepareStreamsAndStart(JUnitStarter.java:230)
    at com.intellij.rt.junit.JUnitStarter.main(JUnitStarter.java:58)
Caused by: java.lang.NullPointerException
    at com.fasterxml.jackson.databind.ser.std.NumberSerializers$LongSerializer.serialize(NumberSerializers.java:194)
    at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase._serializeWithObjectId(BeanSerializerBase.java:624)
    at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:159)
    at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:727)
    at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:722)
    ... 57 more



Process finished with exit code 255
4

0 回答 0