如何在 Spring 引导中拒绝请求中的 3 位数时间?
How to reject 3-digit time in request in Spring Boot?
我是 Java 的新手。我在 json、
中获取以下格式的日期和时间
{
"appointmentDate":"2017-05-30",
"appointmentTime":"23:30:00"
}
在请求中,我是这样做的,
@NotNull(message = "appointmentDate is required")
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd")
private Date appointmentDate;
@NotNull(message = "appointmentTime is required")
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "HH:mm:ss")
private String appointmentTime;
在上述请求 class 中,我使用 Date
class 获取日期并将时间视为字符串。
然后在服务 class 中,我尝试将字符串对象转换为日期对象,然后在 table 中搜索以在 table 中找到约会列表。
// convert into string
String dateString = null;
SimpleDateFormat sdfr = new SimpleDateFormat("yyyy-MM-dd");
dateString = sdfr.format( appointmentDate );
String dt = dateString + " " + appointmenTime;
Date startDateAndTime = null;
try {
//startDateAndTime = yyyyMMddFormat.parse(yyyyMMddFormat.format(dt));
startDateAndTime = new SimpleDateFormat(Constants.DATEANDTIME).parse(dt);
} catch (ParseException e) {
throw new IllegalArgumentException("Illegal date format");
}
但我面临的问题是,即使我输入了错误的日期,它也会给我输出。日期中的错误没有抛出解析异常。
{
"appointmentDate":"20171-05-30",
"appointmentTime":"231:30:00"
}
这是我的常数
public static final String DATEANDTIME = "yyyy-MM-dd HH:mm:ss";
这是我的存储库查询,
@Query(value = "select COUNT(a.doctorId) from Appointment a WHERE a.doctorId = :doctorId AND a.scheduleFromTime = :appointmentDateTime")
Long findAppointmentExists(@Param("doctorId") Long doctorId,
@Param("appointmentDateTime") Date appointmentDateTime);
请允许我建议您使用现代 类 LocalDate
和 LocalTime
作为您的日期和时间。那么直接做例如
if (appointmentDate.isAfter(LocalDate.now().plusYears(5)) {
throw new IllegalArgumentException("Too far into the future");
}
这将捕获 5 位数年份和年份值中的许多其他错误。请自己设置一个合适的限制。您可能类似地禁止过去的日期,也许有更严格的限制。目前这将是内置的,因为 LocalTime
只接受最多 23:59:59.999999999.
的时间
将两者合并为一个对象
LocalDateTime startDateAndTime = LocalDateTime.of(appointmentDate, appointmentTime);
我已经输入了我头脑中的代码片段,可能有一两个错字。如果无法修复,请回复。
我最近没有使用 Spring Boot 的经验。在这个问题中,有更多关于使用现代 Java 日期和时间 类 以及 Spring Boot 和 Jackson 的内容:。毫无疑问,在其他地方更多。
您代码中的第一个日期格式毫无意义,它只是转换为日期(我尝试时失败了)然后立即转换回字符串。
我已经从您的示例中删除了不必要的行,并在下面发布了我的 "working" 示例。
String dt = appointmentDate + " " + appointmentTime;
Date startDateAndTime = null;
try
{
startDateAndTime = new SimpleDateFormat(Constants.DATEANDTIME).parse(dt);
} catch (ParseException e)
{
throw new IllegalArgumentException("Illegal date format");
}
System.out.println(startDateAndTime);
当某些东西不工作时,这种情况往往会发生很多,而你不断地向它扔代码,直到你完全卡住。有时退后一步,尝试弄清楚您实际想要实现的目标,然后再尝试一下,这很好。
我是 Java 的新手。我在 json、
中获取以下格式的日期和时间{
"appointmentDate":"2017-05-30",
"appointmentTime":"23:30:00"
}
在请求中,我是这样做的,
@NotNull(message = "appointmentDate is required")
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd")
private Date appointmentDate;
@NotNull(message = "appointmentTime is required")
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "HH:mm:ss")
private String appointmentTime;
在上述请求 class 中,我使用 Date
class 获取日期并将时间视为字符串。
然后在服务 class 中,我尝试将字符串对象转换为日期对象,然后在 table 中搜索以在 table 中找到约会列表。
// convert into string
String dateString = null;
SimpleDateFormat sdfr = new SimpleDateFormat("yyyy-MM-dd");
dateString = sdfr.format( appointmentDate );
String dt = dateString + " " + appointmenTime;
Date startDateAndTime = null;
try {
//startDateAndTime = yyyyMMddFormat.parse(yyyyMMddFormat.format(dt));
startDateAndTime = new SimpleDateFormat(Constants.DATEANDTIME).parse(dt);
} catch (ParseException e) {
throw new IllegalArgumentException("Illegal date format");
}
但我面临的问题是,即使我输入了错误的日期,它也会给我输出。日期中的错误没有抛出解析异常。
{
"appointmentDate":"20171-05-30",
"appointmentTime":"231:30:00"
}
这是我的常数
public static final String DATEANDTIME = "yyyy-MM-dd HH:mm:ss";
这是我的存储库查询,
@Query(value = "select COUNT(a.doctorId) from Appointment a WHERE a.doctorId = :doctorId AND a.scheduleFromTime = :appointmentDateTime")
Long findAppointmentExists(@Param("doctorId") Long doctorId,
@Param("appointmentDateTime") Date appointmentDateTime);
请允许我建议您使用现代 类 LocalDate
和 LocalTime
作为您的日期和时间。那么直接做例如
if (appointmentDate.isAfter(LocalDate.now().plusYears(5)) {
throw new IllegalArgumentException("Too far into the future");
}
这将捕获 5 位数年份和年份值中的许多其他错误。请自己设置一个合适的限制。您可能类似地禁止过去的日期,也许有更严格的限制。目前这将是内置的,因为 LocalTime
只接受最多 23:59:59.999999999.
将两者合并为一个对象
LocalDateTime startDateAndTime = LocalDateTime.of(appointmentDate, appointmentTime);
我已经输入了我头脑中的代码片段,可能有一两个错字。如果无法修复,请回复。
我最近没有使用 Spring Boot 的经验。在这个问题中,有更多关于使用现代 Java 日期和时间 类 以及 Spring Boot 和 Jackson 的内容:
您代码中的第一个日期格式毫无意义,它只是转换为日期(我尝试时失败了)然后立即转换回字符串。
我已经从您的示例中删除了不必要的行,并在下面发布了我的 "working" 示例。
String dt = appointmentDate + " " + appointmentTime;
Date startDateAndTime = null;
try
{
startDateAndTime = new SimpleDateFormat(Constants.DATEANDTIME).parse(dt);
} catch (ParseException e)
{
throw new IllegalArgumentException("Illegal date format");
}
System.out.println(startDateAndTime);
当某些东西不工作时,这种情况往往会发生很多,而你不断地向它扔代码,直到你完全卡住。有时退后一步,尝试弄清楚您实际想要实现的目标,然后再尝试一下,这很好。