我是Spring Boot的新手。我在使用基于Javax的验证时遇到麻烦。
控制器期望从POST端点获取json obj的列表。在JSON请求json数组中,
如果engine
或name
为空,我得到正确的400错误。但是,当engine.type
或engine.name
为null,则会引发500个异常并显示错误消息:
Caused by: org.springframework.beans.NotReadablePropertyException: Invalid property 'engine' of bean class
[java.util.ArrayList]: Bean property 'engine' is not readable or has an invalid
getter method: Does the return type of the getter match the parameter type of
the setter?
Controller.java
@RestController
@Validated
public class CarController {
@PostMapping(path = "/cars")
public ResponseEntity<PostModelResponse> createModel(
@Valid @RequestBody ArrayList<Car> cars)
throws JSONException, ConstraintViolationException {
...
return new ResponseEntity<>(response, HttpStatus.CREATED);
}
}
Car.java
class Car {
@NotNull
@Valid
private Engine engine;
@NotNull
private String name;
Cars(Engine engine, String name) {
this.engine = engine;
this.name = name;
}
public String getName(){ return name; }
public void setName(String name) { this.name = name;}
public Engine getEngine() { return engine; }
public void setEngine(Engine engine){ this.engine = engine; }
}
Engine.java
class Engine {
@NotNull
private String type;
@NotNull
private String name;
Engine(String type, String name) {
this.type = type;
this.name = name;
}
public String getType() { return type; }
public void setType(String type) { this.type = type; }
public String getName() { return name; }
public void setName(String name) { this.name = name;}
}
CollectionValidator.java
public class CollectionValidator implements Validator {
private final Validator validator;
public CollectionValidator(LocalValidatorFactoryBean validator) {
this.validator = validator;
}
@Override
public boolean supports(Class<?> clazz) {
return Collection.class.isAssignableFrom(clazz);
}
@Override
public void validate(Object target, Errors errors) {
Collection col = (Collection) target;
for ( Object obj : col ) {
ValidationUtils.invokeValidator(validator, obj, errors);
}
}
}
ValidatorAdvice.java
@ControllerAdvice
public class ValidatorAdvice {
@Autowired
protected LocalValidatorFactoryBean validator;
@InitBinder
public void initBinder(WebDataBinder binder) {
binder.initDirectFieldAccess();
binder.addValidators(new CollectionValidator(validator));
}
}
请求/响应:URL:http://localhost:8080/cars
POST正文:[{ “名称”:“沃尔沃”, “引擎”:{ “ type”:“ v3”, “名称”:“ volvo_engine” } }]
响应:200(确定)
POST正文:[{ “引擎”:{ “ type”:“ v3”, “名称”:“ volvo_engine” } }]
响应:400(预计缺少coz name
)
POST正文:[{“ name”:“ Volvo”}]
响应:400(预计缺少coz engine
)
POST正文:[{ “名称”:“沃尔沃”, “引擎”:{ “名称”:“ volvo_engine” } }]
响应:500(我当时预期为400),错误:
原因:org.springframework.beans.NotReadablePropertyException:Bean类的无效属性“ engine” [java.util.ArrayList]:Bean属性“ id”不可读或无效 getter方法:getter的返回类型是否与的参数类型匹配 设置者?
在发布此内容之前,我查看了其他stackOverflow结果。如果我接受Car
对象而不是ArrayList<Car>
非常感谢您的帮助。
答案 0 :(得分:0)
验证器对bean起作用,而不对list起作用;一种可能的解决方案是将列表包装在具有list属性上的@Valid
批注的bean中:
public class MyList<E> {
@Valid
private List<E> list;
public List<E> getList() {
return list;
}
public void setList(List<E> list) {
this.list = list;
}
}
并在控制器中使用以下命令进行验证:
createModel(@Valid @RequestBody MyList<Car> cars)