为了账号安全,请及时绑定邮箱和手机立即绑定

在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 关注
  • 277 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号