Java-如何进行JSON日期验证? [英] Java - How to make JSON date validation?

查看:99
本文介绍了Java-如何进行JSON日期验证?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这段代码:

@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd")
    protected Date statusDate;

但是出于某种原因,它接受像-

But for somehow it accepts date formats like -

"statusDate": "2017-13-27"

"statusDate": "201823-12-12"

是否可以验证请求中的格式(而不是手动验证)?

Is it possible to validate the format within the request (not manually)?

推荐答案

好吧,如果您在截取数据时收到的日期格式不是什么,则需要编写一个自定义的日期序列化/反序列化类,并抛出一个自定义的异常.如您所愿.

Well, you need to write a custom date serialisation/de-serialisation class and throw a custom exception if the date format you receive while intercepting the data is not what you'd expected.

答案将为您指出正确的方法.

This answer will point you in the right direction of how to do so.

此外,您还可以在代码中包含验证器,以验证有效载荷并使Jackson保持简单状态.

Also you can have the validator in your code to validate the payload and leave Jackson to it's simple state.

这篇关于Java-如何进行JSON日期验证?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆