Home > Blockchain >  @AssertTrue giving JSR-303 issue when using inside a nested Object on which @Valid annotation is pre
@AssertTrue giving JSR-303 issue when using inside a nested Object on which @Valid annotation is pre

Time:08-21

My Custom DTO class is as follows :

public class TestDto1 {

private String key;
private String val;

@AssertTrue
private boolean isValid() {
    return key !=null || val !=null;
}public class TestDto1 {

private String key;
private String val;

@AssertTrue
private boolean isValid() {
    return key !=null || val !=null;
}

My Parent DTO class :

public class TestDto {


private String id;

@Valid
private TestDto1 tes;

public TestDto1 getTes() {
    return tes;
}

public void setTes(TestDto1 tes) {
    this.tes = tes;
}

public String getId() {
    return id;

While running the app and hitting api with following JSON getting following error:

{
"id":"1234",
"tes":{
    
}

}

  JSR-303 validated property 'tes.valid' does not have a corresponding accessor for Spring data binding - check your DataBinder's configuration (bean property versus direct field access)] with root cause

org.springframework.beans.NotReadablePropertyException: Invalid property 'tes.valid' of bean class [com.example.thirdparty.controller.TestDto]: Bean property 'tes.valid' is not readable or has an invalid getter method: Does the return type of the getter match the parameter type of the setter?

Please let me know what needs to be done here

CodePudding user response:

This is not a field that gets validated but a method which is kind of read as a virtual field from that method.

I think the method has to be declared as public to become accessible for validation

 @AssertTrue
 public boolean isValid() {
     return key !=null || val !=null;
 }
  • Related