Akash tiwari
Akash tiwari

Reputation: 245

@AssertTrue giving JSR-303 issue when using inside a nested Object on which @Valid annotation is present

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

Upvotes: 4

Views: 688

Answers (2)

Unbearables
Unbearables

Reputation: 1

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

Upvotes: 0

Panagiotis Bougioukos
Panagiotis Bougioukos

Reputation: 18979

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;
 }

Upvotes: 6

Related Questions