4

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

2 Answers 2

6

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;
 }
Sign up to request clarification or add additional context in comments.

1 Comment

by mamking the method public, it worked :) . Thanks
0

Also for others who may be wondering, the method name needs to start with is..., has... or other variations do not work.

1 Comment

Better suited as comment to accepted answer, rather than separate answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.