springboot url传参直接用对象接收时用什么注解能对属性做类型转换包括日期和自定义枚举类型Failed to convert property value of type javalangString to required type javautilDate for property endTime; 依赖包DateTimeFormatpattern = yyyy-MM-dd的类路径
在SpringBoot中,可以使用@ModelAttribute注解对属性进行类型转换,包括日期和自定义枚举类型。同时,可以使用@DateTimeFormat注解指定日期格式,以解决日期转换的问题。
示例代码如下:
@GetMapping("/user")
public String getUser(@ModelAttribute User user) {
// ...
}
public class User {
private String name;
private Date endTime;
private Gender gender;
// getters and setters
}
public enum Gender {
MALE, FEMALE
}
在上面的示例中,getUser方法中的@ModelAttribute注解指定了参数为User类型,SpringBoot会自动将请求参数转换为User对象。同时,User类中的endTime属性使用了@DateTimeFormat注解指定了日期格式。Gender为自定义枚举类型,不需要额外的注解进行转换
原文地址: https://www.cveoy.top/t/topic/huAt 著作权归作者所有。请勿转载和采集!