亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在java中使用jackson反序列化Date字段時引發自定義異常

在java中使用jackson反序列化Date字段時引發自定義異常

RISEBY 2022-08-17 16:02:06
斷續器:@Getter@Setter@ToStringpublic class TestDto {    @NotNull    private String id;    @NotNull    @DateTimeFormat(pattern = "YYYY-MM-DD'T'hh:mm:ss.SSSZ")    private Instant timestamp;}當我給出這個輸入{"timestamp":"4/23/2018 11:32 PM","id":"132"}它給出了BAD_REQUEST(它應該),但我想處理這個格式錯誤的日期,并引發一個帶有我的自定義異常的異常。如何添加此內容?
查看完整描述

1 回答

?
互換的青春

TA貢獻1797條經驗 獲得超6個贊

由于尚不支持 OP 請求的功能:https://github.com/FasterXML/jackson-annotations/issues/130


嘗試使用更長的方法通過對字段使用自定義反序列化程序來執行相同的操作timestamp


自定義異常類:


import com.fasterxml.jackson.core.JsonProcessingException;


public class MyException extends JsonProcessingException {

    public MyException(String message) {

        super(message);

    }

}

自定義反序列化程序類:


import com.fasterxml.jackson.core.JsonParser;

import com.fasterxml.jackson.core.JsonProcessingException;

import com.fasterxml.jackson.databind.DeserializationContext;

import com.fasterxml.jackson.databind.JsonNode;

import com.fasterxml.jackson.databind.deser.std.StdDeserializer;


import java.io.IOException;

import java.text.SimpleDateFormat;

import java.time.Instant;

import java.util.Date;

public class InstantDeserializer extends StdDeserializer<Instant> {


public InstantDeserializer() {

    this(null); 


public InstantDeserializer(Class<?> vc) {

    super(vc); 

}


private SimpleDateFormat sdf = new SimpleDateFormat("YYYY-MM-DD'T'hh:mm:ss.SSS'Z'");


@Override

public Instant deserialize(JsonParser jp, DeserializationContext ctxt)

  throws IOException, JsonProcessingException {

    JsonNode node = jp.getCodec().readTree(jp);

    Date date = null;

    try {

        date = sdf.parse(node.asText());

    } catch (Exception e) {

        throw new MyException("Instant field deserialization failed");

    }

    return date.toInstant();

}

}

更新的 TestDto 類:


import com.fasterxml.jackson.databind.annotation.JsonDeserialize;

import lombok.Getter;

import lombok.Setter;

import lombok.ToString;

import org.springframework.format.annotation.DateTimeFormat;


import javax.validation.constraints.NotNull;

import java.time.Instant;


@Getter

@Setter

@ToString

public class TestDto {


    @NotNull

    private String id;


    @NotNull

    @JsonDeserialize(using = InstantDeserializer.class)

    @DateTimeFormat(pattern = "YYYY-MM-DD'T'hh:mm:ss.SSS'Z'")

    private Instant timestamp;

}

無效的輸入請求:


{"timestamp":"4/23/2018 11:32 PM","id":"132"}

響應:


{

    "timestamp": 1552845180271,

    "status": 400,

    "error": "Bad Request",

    "message": "JSON parse error: Instant field deserialization failed; nested exception is com.fasterxml.jackson.databind.JsonMappingException: Instant field deserialization failed (through reference chain: TestDto[\"timestamp\"])"

}

有效輸入請求:


{"timestamp":"2018-04-23T11:32:22.213Z","id":"132"}

響應:


{

    "id": "132",

    "timestamp": {

        "epochSecond": 1514700142,

        "nano": 213000000

    }

}

如果您不喜歡時間戳字段被反序列化的方式,并希望更改它,那么這篇SO帖子將很有幫助。


查看完整回答
反對 回復 2022-08-17
  • 1 回答
  • 0 關注
  • 203 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號