无法将类型 'java.lang.String' 的 属性 值转换为 属性 'date' 所需的类型 'java.util.Date':长度不完全为 10 个字符
Failed to convert property value of type 'java.lang.String' to required type 'java.util.Date' for property 'date': it is not exactly10characters long
在我的代码中,我有两个实体 BusDetails 和 User。 User 和 BusDetails 有多对多的关系。每当我尝试预订巴士时,数据都保存在数据库中的连接 table 中,但我收到此异常:
无法转换 属性 类型的值 'java.lang.String' 到 属性 'date' 所需的类型 'java.util.Date';嵌套异常是 java.lang.IllegalArgumentException:无法解析日期:它不完全是 10 个字符长]]
用户Table:
public class User {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int u_id;
@Column
@NotEmpty(message = "Name cannot be empty")
private String name;
@Column
@NotEmpty(message = "Username cannot be empty")
private String userName;
@Column
@NotEmpty(message = "please enter number")
@Size(min = 10,max = 10, message = "10 digits required")
private String number;
@Column
@NotEmpty
@Size(min=8,message = "Minimum 8 characters required")
private String password;
@ManyToMany(cascade = CascadeType.MERGE,fetch = FetchType.EAGER)
@JoinTable(name = "user_role",joinColumns = @JoinColumn(name = "u_id"), inverseJoinColumns = @JoinColumn(name = "r_id"))
public Set<Role> roles;
@ManyToMany(cascade = CascadeType.PERSIST,fetch = FetchType.EAGER)
@JoinTable(name = "user_busdetails", joinColumns = @JoinColumn(name = "u_id") , inverseJoinColumns = @JoinColumn(name = "bus_Id"))
public Set<BusDetails> bus = new HashSet<BusDetails>();
//gettersAndSetters
巴士详情:
@Entity
@Component("BusDetails")
public class BusDetails {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int bus_Id;
@Column
public String fromDestination;
@Column
public String toDestination;
@Column
@DateTimeFormat
@Temporal(TemporalType.DATE)
private Date date;
@Column
private String travels;
@Column
private String bus_Type;
@Column
private String seats_Available;
@Column
public String fare;
@Column
private String departure;
@ManyToMany(fetch = FetchType.EAGER,mappedBy = "bus")
@JsonIgnore
public Set<User> user = new HashSet<User>();
//gettersAndSetters
图书控制器:
@PostMapping("/bookbus")
@ResponseBody
public BusDetails bookBus(@ModelAttribute BusDetails bus) {
System.out.println(bus.getDate());
return busDetail.bookBus(bus);
}
@InitBinder
public void initBinder(WebDataBinder binder){
binder.registerCustomEditor( Date.class,new CustomDateEditor(new SimpleDateFormat("yyyy-
MM-dd"), true, 10));
}
图书服务:
public BusDetails bookBus(BusDetails bus) {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
String currentPrincipleName = authentication.getName();
User user = userRepo.findByUserName(currentPrincipleName);
user.getBus().add(bus);
System.out.println(user);
System.out.println(bus);
userRepo.save(user);
return bus;
}
因为您在控制器中使用了@ModelAttribute 意味着所有参数都以字符串格式传递。
您的情况是从 String
格式化为 Date
。
@Entity
@Component("BusDetails")
public class BusDetails {
//...
@Column
private Date date;
//setter(can add or modify) should be custom like below :
public void setDate(String date){
try {
this.date = new SimpleDateFormat("yyyy-MM-dd").parse(date);
} catch (ParseException e) {
e.printStackTrace();
}
}
// ...getter & setter
}
在应用程序级别转换日期参数
@Configuration
class DateTimeConfig {
@Bean
public FormattingConversionService conversionService() {
DefaultFormattingConversionService conversionService =
new DefaultFormattingConversionService(false);
DateTimeFormatterRegistrar registrar = new DateTimeFormatterRegistrar();
registrar.setDateFormatter(DateTimeFormatter.ofPattern("dd.MM.yyyy"));
registrar.setDateTimeFormatter(DateTimeFormatter.ofPattern("dd.MM.yyyy HH:mm:ss"));
registrar.registerFormatters(conversionService);
// other desired formatters
return conversionService;
}
}
首先,我们使用 false 参数创建 DefaultFormattingConversionService,这意味着 Spring 默认情况下不会注册任何格式化程序。
然后,我们在 DateTimeFormatterRegistrar 对象中手动注册日期和日期时间格式的新模式。
在我的代码中,我有两个实体 BusDetails 和 User。 User 和 BusDetails 有多对多的关系。每当我尝试预订巴士时,数据都保存在数据库中的连接 table 中,但我收到此异常:
无法转换 属性 类型的值 'java.lang.String' 到 属性 'date' 所需的类型 'java.util.Date';嵌套异常是 java.lang.IllegalArgumentException:无法解析日期:它不完全是 10 个字符长]]
用户Table:
public class User {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int u_id;
@Column
@NotEmpty(message = "Name cannot be empty")
private String name;
@Column
@NotEmpty(message = "Username cannot be empty")
private String userName;
@Column
@NotEmpty(message = "please enter number")
@Size(min = 10,max = 10, message = "10 digits required")
private String number;
@Column
@NotEmpty
@Size(min=8,message = "Minimum 8 characters required")
private String password;
@ManyToMany(cascade = CascadeType.MERGE,fetch = FetchType.EAGER)
@JoinTable(name = "user_role",joinColumns = @JoinColumn(name = "u_id"), inverseJoinColumns = @JoinColumn(name = "r_id"))
public Set<Role> roles;
@ManyToMany(cascade = CascadeType.PERSIST,fetch = FetchType.EAGER)
@JoinTable(name = "user_busdetails", joinColumns = @JoinColumn(name = "u_id") , inverseJoinColumns = @JoinColumn(name = "bus_Id"))
public Set<BusDetails> bus = new HashSet<BusDetails>();
//gettersAndSetters
巴士详情:
@Entity
@Component("BusDetails")
public class BusDetails {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private int bus_Id;
@Column
public String fromDestination;
@Column
public String toDestination;
@Column
@DateTimeFormat
@Temporal(TemporalType.DATE)
private Date date;
@Column
private String travels;
@Column
private String bus_Type;
@Column
private String seats_Available;
@Column
public String fare;
@Column
private String departure;
@ManyToMany(fetch = FetchType.EAGER,mappedBy = "bus")
@JsonIgnore
public Set<User> user = new HashSet<User>();
//gettersAndSetters
图书控制器:
@PostMapping("/bookbus")
@ResponseBody
public BusDetails bookBus(@ModelAttribute BusDetails bus) {
System.out.println(bus.getDate());
return busDetail.bookBus(bus);
}
@InitBinder
public void initBinder(WebDataBinder binder){
binder.registerCustomEditor( Date.class,new CustomDateEditor(new SimpleDateFormat("yyyy-
MM-dd"), true, 10));
}
图书服务:
public BusDetails bookBus(BusDetails bus) {
Authentication authentication = SecurityContextHolder.getContext().getAuthentication();
String currentPrincipleName = authentication.getName();
User user = userRepo.findByUserName(currentPrincipleName);
user.getBus().add(bus);
System.out.println(user);
System.out.println(bus);
userRepo.save(user);
return bus;
}
因为您在控制器中使用了@ModelAttribute 意味着所有参数都以字符串格式传递。
您的情况是从 String
格式化为 Date
。
@Entity
@Component("BusDetails")
public class BusDetails {
//...
@Column
private Date date;
//setter(can add or modify) should be custom like below :
public void setDate(String date){
try {
this.date = new SimpleDateFormat("yyyy-MM-dd").parse(date);
} catch (ParseException e) {
e.printStackTrace();
}
}
// ...getter & setter
}
在应用程序级别转换日期参数
@Configuration
class DateTimeConfig {
@Bean
public FormattingConversionService conversionService() {
DefaultFormattingConversionService conversionService =
new DefaultFormattingConversionService(false);
DateTimeFormatterRegistrar registrar = new DateTimeFormatterRegistrar();
registrar.setDateFormatter(DateTimeFormatter.ofPattern("dd.MM.yyyy"));
registrar.setDateTimeFormatter(DateTimeFormatter.ofPattern("dd.MM.yyyy HH:mm:ss"));
registrar.registerFormatters(conversionService);
// other desired formatters
return conversionService;
}
}
首先,我们使用 false 参数创建 DefaultFormattingConversionService,这意味着 Spring 默认情况下不会注册任何格式化程序。
然后,我们在 DateTimeFormatterRegistrar 对象中手动注册日期和日期时间格式的新模式。