SpringBoot URL 传参对象类型转换:日期、枚举注解
在 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/oXpn 著作权归作者所有。请勿转载和采集!