0

I have a problem:

public class hello{
    protected String xx;

    @NotEmpty
    @JsonProperty("xx")
    @JacksonXmlProperty(localName = "xx")
    public String getXx() {
        return xx;
    }

    public void setxx(String xx) {
        this.xx= xx;
    }
}

public class hi extends hello {
    @Override
    @Nullable
    @JsonProperty("xx")
    @JacksonXmlProperty(localName = "xx")
    public String getXx() {
        return xx;
    }

    @Override
    public void setXx(String xx) {
        this.xx = xx;
    }
}

The superclass has to validate that the xx attribute cannot be null, but in the subclass, it's possible to have a null value. Is there any solution?

khelwood
  • 55,782
  • 14
  • 81
  • 108
Xavi Guirao
  • 338
  • 4
  • 17
  • Where do `@Nullable` and `@NotEmpty` come from? What packages? At least for `@Nullable`, if it's `org.springframework.lang.Nullable` that doesn't actually enforce any validation. – Christopher Schneider Nov 25 '19 at 15:52
  • And a child class of hi could again enforce `@NotEmpty`? Definitely a case for not using inheritance, but extending hello with an almost-xx field, `getOptionalXx()` or such, and additional `isXxActuallyNull()`. – Joop Eggen Nov 25 '19 at 15:56
  • import org.hibernate.validator.constraints.NotEmpty; – Xavi Guirao Nov 26 '19 at 14:24

0 Answers0