我正在尝试使用springboot-java创建一个应用程序, 前端为html / css / javascript。在创建员工记录时,我收到以下错误。我正在传递导致错误的加入日期。
failed - {"readyState":4, "responseText":"{\"timestamp\":1515066928232, \"status\":500, \"error\":\"Internal Server Error\", \"exception\":\"java.time.format.DateTimeParseException\", \"message\":\"Text '01-17-2018' could not be parsed at index 0\", \"path\":\"/employee/\"}", "responseJSON":{"timestamp":1515066928232, "status":500, "error":"Internal Server Error", "exception":"java.time.format.DateTimeParseException", "message":"Text '01-17-2018' could not be parsed at index 0", "path":"/employee/"}, "status":500, "statusText":"error"}
以下是我在java中使用的代码:
private static final long serialVersionUID = -7314008048174880868L;
private static final DateTimeFormatter DATE_FORMAT = DateTimeFormatter.ofPattern("MM-dd-yyyy");
private Integer organisation;
private String empName;
private String joinDate;
private String gender;
private String designation;
private String email;
public EmployeeDto(){
}
public EmployeeDto(Integer organisation, String empName, String joinDate, String gender,
String designation, String email) {
super();
this.organisation = organisation;
this.empName = empName;
this.joinDate = joinDate;
this.gender = gender;
this.designation = designation;
this.email = email;
}
public Employee buildEmployee(){
return new Employee(this.empName,LocalDate.parse(this.joinDate),this.gender,this.designation,this.email);
}
我使用以下日期转换器:
@Converter(autoApply = true)
public class LocalDateAttributeConverter implements AttributeConverter<LocalDate, Date> {
@Override
public Date convertToDatabaseColumn(LocalDate locDate) {
return (locDate == null ? null : Date.valueOf(locDate));
}
@Override
public LocalDate convertToEntityAttribute(Date sqlDate) {
return (sqlDate == null ? null : sqlDate.toLocalDate());
}
}
我无法找到根本原因....
答案 0 :(得分:3)
在buildEmployee()
中,我认为您需要LocalDate.parse(this.joinDate, DATE_FORMAT)
而不是LocalDate.parse(this.joinDate)
。
Stack Overflow上的一个经常发布的链接是How to debug small programs。在该博客文章中,第一个提示是打开编译器警告并仔细阅读它们。将代码放入Eclipse中会产生以下警告:
未使用字段EmployeeDto.DATE_FORMAT的值
这可能会让您想知道为什么DATE_FORMAT
没有被使用,以及为什么你想首先将它放在那里。这反过来可以激发您按预期使用它,从而修复您的错误。