javax validation does not validate notNull

Sandro Rey :

I have a springBoot 2.1.9.RELEASE application that uses Spring Data for Couchbase

I have this object

@Data
@AllArgsConstructor
@NoArgsConstructor
public class Hostel<T> {

    @NotNull
    @JsonProperty("_location")
    private T location;

}

and this other one

@Document
@Data
@AllArgsConstructor
@NoArgsConstructor
@EqualsAndHashCode(of = { "id" })
@Builder
public class HTMDoc {

    @Id
    private String id;
    @NotNull
    @Field
    private Hostel hostel;

}

on the service

public HTMDoc create(@Valid HTMDoc doc) {
    return repository.save(doc);
}

on the test

service.create(new HTMDoc());

but when I save I got this error instead of the validation NotNull in the hostel field

 org.springframework.data.mapping.MappingException: An ID property is needed, but not found/could not be generated on this entity.
George :

You need to use the @org.springframework.validation.annotation.Validated annotation over your service class to enable validation.

@Validated
@Service
public class DocService {
  public HTMDoc create(@Valid HTMDoc doc) {
    return repository.save(doc);
  }
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=323693&siteId=1