我正在编写我的第一个春季启动应用程序而且我遇到了这个问题。我无法向用户显示错误消息。没有该数据的对象不会保存在数据库中,这没关系。但显示错误消息是问题所在。当我调试我得到错误size = 0
这是模型
hadoop fs
控制器
@Size(min = 1, message = "Address is invalid.")
@NotNull
@Column
private String address;
Thymeleaf
@RequestMapping(value = "/create", method = RequestMethod.POST, consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
public String createNewBusiness(@Valid @ModelAttribute("business") Business business,
BindingResult result, Model model) {
model.addAttribute("userEmail", getUserEmail());
logger.info("/business/create:" + business.toString());
LocationResponse locationResponse = geoService.getCoords(business.getAddress());
if (locationResponse.getStatus().equals("OK")) {
business.setLatitude(locationResponse.getResults().get(0).getGeometry().getLocation().getLat());
business.setLongitude(locationResponse.getResults().get(0).getGeometry().getLocation().getLng());
business.setUserId(getUserId());
businessService.createNew(business);
model.addAttribute("business", business);
} else {
business.setAddress(null);
model.addAttribute("business", business);
}
if (result.hasErrors()) {
List<FieldError> errors = result.getFieldErrors();
for (FieldError error : errors ) {
System.out.println (error.getObjectName() + " - " + error.getDefaultMessage());
}
return "newBusiness";
}
return "business";
}
答案 0 :(得分:1)
您是否在Validator
中定义了@SpringBootApplication
?
@SpringBootApplication
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class);
}
@Bean
public Validator validator() {
return new LocalValidatorFactoryBean();
}
}