gpt4 book ai didi

java - bean 类 [java.util.ArrayList] : Bean property `xyz` is not readable or has an invalid getter method 的属性 'xyz' 无效

转载 作者:太空宇宙 更新时间:2023-11-04 09:28:48 29 4
gpt4 key购买 nike

我是 Spring 启动新手。我在基于 javax 的验证方面遇到问题。 Controller 需要来自 POST 端点的 json obj 列表。在JSON请求json数组中,如果enginename为 null,我得到正确的 400 错误。但是,当 engine.typeengine.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);
}
}

汽车.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; }

}

引擎.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

  1. 发布正文:[{“名称”:“沃尔沃”,“引擎” : { “类型”:“v3”, “名称”:“沃尔沃发动机”}}]

    响应:200(好的)

  2. 发布正文:[{“引擎” : { “类型”:“v3”, “名称”:“沃尔沃发动机”}}]

    响应:400(这是预期的,因为缺少 name)

  3. POST 正文:[{"name": "Volvo"}]

    响应:400(这是预期的,因为缺少 engine)

  4. 发布正文:[{“名称”:“沃尔沃”,“引擎” : { “名称”:“沃尔沃发动机”}}]

    响应:500(我期待 400),错误:

    引起:org.springframework.beans.NotReadablePropertyException:bean类的无效属性“engine” [java.util.ArrayList]:Bean 属性“id”不可读或无效 getter方法:getter的返回类型与参数类型是否匹配 设置者?

在发布此内容之前,我查看了其他 stackOverflow 结果。如果我接受 Car 一切都会正常对象而不是 ArrayList<Car>

非常感谢任何帮助。

最佳答案

validator 在 bean 上工作,而不是列表;一种可能的解决方案是将列表包装在一个 bean 中,并在列表属性上使用 @Valid 注释:

public class MyList<E> {

@Valid
private List<E> list;

public List<E> getList() {
return list;
}

public void setList(List<E> list) {
this.list = list;
}
}

并在 Controller 中进行验证:

createModel(@Valid @RequestBody MyList<Car> cars)

关于java - bean 类 [java.util.ArrayList] : Bean property `xyz` is not readable or has an invalid getter method 的属性 'xyz' 无效,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57366041/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com