Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/313.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 在Gson自定义序列化日期字段中指定默认空值(“日期”)_Java_Spring_Serialization - Fatal编程技术网

Java 在Gson自定义序列化日期字段中指定默认空值(“日期”)

Java 在Gson自定义序列化日期字段中指定默认空值(“日期”),java,spring,serialization,Java,Spring,Serialization,我正在尝试使用自定义序列化程序将默认空(“”)值分配给日期字段 @JsonAdapter(AddInfoSerializer.class) public class AddInfo { @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyyMMdd") protected Date deliveryDate; protected DeliveryTimeZone deliveryTime

我正在尝试使用自定义序列化程序将默认空(
“”
)值分配给日期字段

@JsonAdapter(AddInfoSerializer.class)
public class AddInfo {
    @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyyMMdd")
    protected Date deliveryDate;
    protected DeliveryTimeZone deliveryTimeZone;
    protected ReturnKit returnKit;

    public Date getDeliveryDate() {
        return deliveryDate;
    }

    public void setDeliveryDate(Date value) {

        this.deliveryDate = value;
    }

    public DeliveryTimeZone getDeliveryTimeZone() {

        return deliveryTimeZone;
    }

    public void setDeliveryTimeZone(DeliveryTimeZone value) {

        this.deliveryTimeZone = value;
    }

    public ReturnKit getReturnKit() {

        return returnKit;
    }

    public void setReturnKit(ReturnKit value) {

        this.returnKit = value;
    }

    public enum DeliveryTimeZone {
        @SerializedName("00")
        OPTIONAL("00"),
        @SerializedName("08")
        ZONE_00_12("08"),
        @SerializedName("12")
        ZONE_12_14("12"),
        @SerializedName("14")
        ZONE_14_16("14"),
        @SerializedName("16")
        ZONE_16_18("16"),
        @SerializedName("18")
        ZONE_18_20("18"),
        @SerializedName("19")
        ZONE_18_21("19"),
        @SerializedName("20")
        ZONE_19_21("20");

        private final String value;

        DeliveryTimeZone(String value) {
            this.value = value;
        }
    }

    public enum ReturnKit {
        @SerializedName("1")
        REQUESTED("1"),
        @SerializedName("0")
        NOT_REQUESTED("0"),
        @SerializedName("1")
        ACCOMPANIED_BY_DEVICE("1"),
        @SerializedName("0")
        ALONE("0"),
        @SerializedName("0")
        NOT_NEEDED("0");

        private final String value;

        ReturnKit(String value) {
            this.value = value;
        }
        public String getValue() {
            return value;
        }
    }
}
我在jsp页面中传递AddInfo标识的对象,但没有将值设置为父类AddInfo的deliveryDate字段

序列化时,Json中应包含deliveryDate参数,并将其设置为
“”


敬请告知。

为什么要将其设置为
?它是
日期
它不能保存
它可以是
空的
,但不能是
。您还使用了
Optional
作为美化的
null
检查,这不是
Optional
的用途。请将代码作为代码而不是图像发布,因为这是不可读的,需要额外的点击等。这不会激励人们帮助您。感谢您的快速响应。我已经添加了代码。我尝试使用null而不是“”,但json仍然不包含deliveryDate参数。这不会改变任何事情,因为您使用了optional(您不应该这样做,这也是错误的)。使用
if
语句编辑的代码是错误的。所以不,那也不行。你能建议正确的方法,而不是什么让你认为它不起作用?您认为文本字段中如何显示
public class AddInfoSerializer implements JsonSerializer<AddInfo> {
    @Override
    public JsonElement serialize(AddInfo addInfo, Type type, JsonSerializationContext context) {
        JsonObject object = new JsonObject();

        if (addInfo.getDeliveryDate() == null) {
            object.addProperty("deliveryDate", EMPTY);
        } else {
            object.addProperty("deliveryDate", addInfo.getDeliveryDate().toString());
        }
       object.addProperty("deliveryTimeZone",addInfo.getDeliveryTimeZone().toString());
       object.addProperty("returnKit",addInfo.getReturnKit().toString());
        return object;
    }
}
public class AddInfoIdentification extends AddInfo {

    @NotNull
    protected IdentificationType identificationType;

    public IdentificationType getIdentificationType() {
        return identificationType;
    }

    public void setIdentificationType(IdentificationType value) {
        this.identificationType = value;
    }

    public enum IdentificationType {
        @SerializedName("0")
        UNNECESSARY("0"),
        @SerializedName("1")
        NECESSARY("1");

        private final String value;

        IdentificationType(String value) {
            this.value = value;
        }
    }
}
public abstract class ABCRequest :- code

 GsonBuilder builder = new GsonBuilder();
        Gson gson = builder
                .registerTypeAdapter(AddInfo.class, new AddInfoSerializer())
                .create();
        try {
            this.setRequestBody(gson.toJson(this));
            }