在java中使用jackson反序列化Date字段时引发自定义异常

断续器:


@Getter

@Setter

@ToString

public 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(它应该),但我想处理这个格式错误的日期,并引发一个带有我的自定义异常的异常。


如何添加此内容?


RISEBY
浏览 278回答 1
1回答

互换的青春

由于尚不支持 OP 请求的功能:https://github.com/FasterXML/jackson-annotations/issues/130尝试使用更长的方法通过对字段使用自定义反序列化程序来执行相同的操作timestamp自定义异常类:import com.fasterxml.jackson.core.JsonProcessingException;public class MyException extends JsonProcessingException {&nbsp; &nbsp; public MyException(String message) {&nbsp; &nbsp; &nbsp; &nbsp; super(message);&nbsp; &nbsp; }}自定义反序列化程序类: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() {&nbsp; &nbsp; this(null);&nbsp;}&nbsp;public InstantDeserializer(Class<?> vc) {&nbsp; &nbsp; super(vc);&nbsp;}private SimpleDateFormat sdf = new SimpleDateFormat("YYYY-MM-DD'T'hh:mm:ss.SSS'Z'");@Overridepublic Instant deserialize(JsonParser jp, DeserializationContext ctxt)&nbsp; throws IOException, JsonProcessingException {&nbsp; &nbsp; JsonNode node = jp.getCodec().readTree(jp);&nbsp; &nbsp; Date date = null;&nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; date = sdf.parse(node.asText());&nbsp; &nbsp; } catch (Exception e) {&nbsp; &nbsp; &nbsp; &nbsp; throw new MyException("Instant field deserialization failed");&nbsp; &nbsp; }&nbsp; &nbsp; 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@ToStringpublic class TestDto {&nbsp; &nbsp; @NotNull&nbsp; &nbsp; private String id;&nbsp; &nbsp; @NotNull&nbsp; &nbsp; @JsonDeserialize(using = InstantDeserializer.class)&nbsp; &nbsp; @DateTimeFormat(pattern = "YYYY-MM-DD'T'hh:mm:ss.SSS'Z'")&nbsp; &nbsp; private Instant timestamp;}无效的输入请求:{"timestamp":"4/23/2018 11:32 PM","id":"132"}响应:{&nbsp; &nbsp; "timestamp": 1552845180271,&nbsp; &nbsp; "status": 400,&nbsp; &nbsp; "error": "Bad Request",&nbsp; &nbsp; "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"}响应:{&nbsp; &nbsp; "id": "132",&nbsp; &nbsp; "timestamp": {&nbsp; &nbsp; &nbsp; &nbsp; "epochSecond": 1514700142,&nbsp; &nbsp; &nbsp; &nbsp; "nano": 213000000&nbsp; &nbsp; }}如果您不喜欢时间戳字段被反序列化的方式,并希望更改它,那么这篇SO帖子将很有帮助。
打开App,查看更多内容
随时随地看视频慕课网APP

相关分类

Java