无法转换类型为';java.lang.String';至所需类型';java.util.Date';对于不动产';日期';:它不是精确的10个字符长

无法转换类型为';java.lang.String';至所需类型';java.util.Date';对于不动产';日期';:它不是精确的10个字符长,java,hibernate,spring-boot,jpa,hibernate-mapping,Java,Hibernate,Spring Boot,Jpa,Hibernate Mapping,在我的代码中,我有两个实体BusDetails和User。用户和总线详细信息具有多对多关系。每当我尝试预订总线时,数据都会保存在数据库的联接表中,但我会遇到以下异常:未能将类型为“java.lang.String”的属性值转换为属性“Date”所需的类型“java.util.Date”;嵌套异常为java.lang.IllegalArgumentException:无法分析日期:它不是精确的10个字符长]] 用户表: public class User { @Id @Gene

在我的代码中,我有两个实体BusDetails和User。用户和总线详细信息具有多对多关系。每当我尝试预订总线时,数据都会保存在数据库的联接表中,但我会遇到以下异常:
未能将类型为“java.lang.String”的属性值转换为属性“Date”所需的类型“java.util.Date”;嵌套异常为java.lang.IllegalArgumentException:无法分析日期:它不是精确的10个字符长]]

用户表:

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

图书服务:

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;


    }

因为您在控制器中使用了@modeldattribute,这意味着所有参数都是以字符串格式传递的

在您的情况下,是从
字符串
格式化为
日期

@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默认情况下不会注册任何格式化程序


然后,我们在DateTimeFormatterRegistrator对象中手动注册日期和日期时间格式的新模式。

在没有上述修改之前,我不知道我做了什么更改。非常感谢。@PariskritA.Moktan如果您使用
@RequestBody
而不是
@modeldattribute
,一切都会好的。
@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;
    }
}