4 回答

TA贡献1850条经验 获得超11个赞
您需要@org.springframework.validation.annotation.Validated在服务类上使用注释来启用验证。
@Validated
@Service
public class DocService {
public HTMDoc create(@Valid HTMDoc doc) {
return repository.save(doc);
}
}

TA贡献2051条经验 获得超10个赞
将以下注释添加到 id 中并尝试一下:
@Id
@GeneratedValue(strategy = GenerationStrategy.UNIQUE)
private String id;
有关注释的更多信息@GeneratedValue
可以在这个很好的答案中找到:Spring generatedValue注释的用法

TA贡献2065条经验 获得超14个赞
请在您的 id 字段中添加以下语法
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Hostel<T> {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "id", nullable = false, updatable = false)
private Long id;
@NotNull
@JsonProperty("_location")
private T location;
}
还可以在服务类中使用 validate 注释。
@Validated
@Service
public class DocService {
public HTMDoc create(@Valid HTMDoc doc) {
return repository.save(doc);
}
}
添加回答
举报