java - 尝试使用 java spring boot、hibernate 和 thmyeleaf 来支持 MAMP,但在验证时遇到错误
问题描述
我已经构建了我的控制器、客户端和地址类(模型)以及用于索引和添加的模板。当我尝试测试验证时,弹簧返回:
出现意外错误(类型=内部服务器错误,状态=500)。组 [javax.validation.groups.Default, ] 约束冲突列表:[ ConstraintViolationImpl {interpolatedMessage ='Invalid',propertyPath = zipCode,rootBeanClass = class com .pooltracker.models.Address, messageTemplate='Invalid'} ConstraintViolationImpl{interpolatedMessage='可能不为空', propertyPath=state, rootBeanClass=class com.pooltracker.models.Address, messageTemplate='{javax.validation.constraints.NotNull .message}'} ConstraintViolationImpl{interpolatedMessage='不能为空', propertyPath=street, rootBeanClass=class com.pooltracker.models.Address, messageTemplate='不能为空'
我了解到hibernate无法处理验证地址类。我偶然发现了 bean 验证的主题。我对此并不陌生,并试图找到解决我的问题的最佳方法,而目前不会在兔子洞中走得太远。下面是我的控制器和模型。
@Controller
@RequestMapping("clients")
public class ClientController {
@Autowired
private ClientDao clientDao;
@Autowired
private AddressDao addressDao;
@RequestMapping(value = "")
public String index(Model model) {
model.addAttribute("clients", clientDao.findAll());
model.addAttribute("title", "My Clients");
return "clients/index";
}
@RequestMapping(value = "add", method = RequestMethod.GET)
public String displayAddClientForm(Model model) {
model.addAttribute("title", "Add Client");
model.addAttribute(new Client());
return "clients/add";
}
@RequestMapping(value = "add", method = RequestMethod.POST)
public String processAddClientForm(@ModelAttribute @Valid Client newClient,
Errors errors, Model model) {
if (errors.hasErrors()) {
model.addAttribute("title", "Add Client");
model.addAttribute("client", newClient);
return "clients/add";
}
clientDao.save(newClient);
return "redirect:";
}
}
Client
实体:
@Entity
@Table(name = "client")
public class Client {
@Id
@GeneratedValue
@Column(name = "client_id")
private int id;
@NotNull
@Size(min=1, message = "Client must have a first name")
@Column(name = "first_name")
private String firstName;
@NotNull
@Size(min=1, message = "Client must have a last name")
@Column(name = "last_name")
private String lastName;
@OneToOne(cascade = CascadeType.ALL,
fetch = FetchType.EAGER, optional = false)
@JoinColumn(name = "address_id")
private Address address;
@NotNull
@Size(min=10, max=10, message = "Must be 10 digits only")
private String phone;
// setters/getters
}
Address
实体:
@Entity
@Table(name = "address")
public class Address {
@Id
@GeneratedValue
@Column(name = "address_id")
private int id;
@NotNull
@Size(min=1, message = "Can not be empty")
@Column(name = "street")
private String street;
@NotNull
@Size(min=1, message = "Can not be empty")
@Column(name = "city")
private String city;
@NotNull
@Enumerated(EnumType.STRING)
@Column(name = "state")
private State state;
@NotNull
@Size(min=5, max=5, message = "Invalid")
@Column(name = "zip_code")
private String zipCode;
@OneToOne(mappedBy = "address" , fetch = FetchType.EAGER)
//@JoinColumn(name = "client_id")
public Client client;
// setters and getters
}
解决方案
在Client
课堂上,添加@Valid
到address
字段:
@Valid
@OneToOne(cascade = CascadeType.ALL,
fetch = FetchType.EAGER,
optional = false)
@JoinColumn(name = "address_id")
private Address address;
public @interface Valid
标记验证级联的属性、方法参数或方法返回类型。
推荐阅读
- javascript - 如何通过将局部变量放入查询字符串中,使用 JSONPATH 从 JSON 中获取特定对象?
- google-apps-script - Google Scripts 获取星期几
- python - 如何使用附加配置自动化调度
- reactjs - 在渲染中调用的函数中的反引号
- python - 如何在 Windows 中对非套接字 FD 执行 I/O 多路复用?(Python)
- python - GoogleTrans 突然停止工作并给出错误
- python - Dijkstra 算法的 Python 实现不适用于所有图形类型
- python - 在 Pandas 中使用 GroupBy 时如何设置条件?
- r - 运行 CFA 时 lav_data 错误“数据集中缺少观察到的变量:”
- java - 使用 jenkins 和 maven 时出现 ClassNotFound 异常