简体   繁体   English

嵌套对象上的 Javax 验证 - 不起作用

[英]Javax validation on nested objects - not working

In my Spring Boot project I have two DTO's which I'm trying to validate, LocationDto and BuildingDto.在我的 Spring Boot 项目中,我有两个 DTO 正在尝试验证,LocationDto 和 BuildingDto。 The LocationDto has a nested object of type BuildingDto. LocationDto 有一个 BuildingDto 类型的嵌套对象。

These are my DTO's:这些是我的 DTO:

LocationDto位置Dto

public class LocationDto {

  @NotNull(groups = { Existing.class })
  @Null(groups = { New.class })
  @Getter
  @Setter
  private Integer id;

  @NotNull(groups = { New.class, Existing.class })
  @Getter
  @Setter
  private String name;

  @NotNull(groups = { New.class, Existing.class, LocationGroup.class })
  @Getter
  @Setter
  private BuildingDto building;

  @NotNull(groups = { Existing.class })
  @Getter
  @Setter
  private Integer lockVersion;

}

BuildingDto建筑Dto

public class BuildingDto {

  @NotNull(groups = { Existing.class, LocationGroup.class })
  @Null(groups = { New.class })
  @Getter
  @Setter
  private Integer id;

  @NotNull(groups = { New.class, Existing.class })
  @Getter
  @Setter
  private String name;

  @NotNull(groups = { Existing.class })
  @Getter
  @Setter
  private List<LocationDto> locations;

  @NotNull(groups = { Existing.class })
  @Getter
  @Setter
  private Integer lockVersion;

}

Currently, I can validate in my LocationDto that the properties name and building are not null, but I can't validate the presence of the property id which is inside building .目前,我可以在我的LocationDto中验证属性namebuilding不为空,但我无法验证建筑物内部的属性 id 的存在

If I use the @Valid annotation on the building property, it would validate all of its fields, but for this case I only want to validate its id .如果我在building属性上使用@Valid注释,它将验证其所有字段,但对于这种情况,我只想验证其id

How could that be done using javax validation?如何使用 javax 验证来完成?

This is my controller:这是我的控制器:

@PostMapping
public LocationDto createLocation(@Validated({ New.class, LocationGroup.class }) @RequestBody LocationDto location) {
  // save entity here...
}

This is a correct request body: (should not throw validation errors)这是一个正确的请求正文:(不应抛出验证错误)

{
  "name": "Room 44",
  "building": {
    "id": 1
  }
}

This is an incorrect request body: (must throw validation errors because the building id is missing )这是一个不正确的请求正文:(必须抛出验证错误,因为缺少建筑物 ID

{
  "name": "Room 44",
  "building": { }
}

In my Spring Boot project I have two DTO's which I'm trying to validate, LocationDto and BuildingDto.在我的 Spring Boot 项目中,我有两个 DTO 正在尝试验证,LocationDto 和 BuildingDto。 The LocationDto has a nested object of type BuildingDto. LocationDto 有一个 BuildingDto 类型的嵌套对象。

These are my DTO's:这些是我的 DTO:

LocationDto位置Dto

public class LocationDto {

  @NotNull(groups = { Existing.class })
  @Null(groups = { New.class })
  @Getter
  @Setter
  private Integer id;

  @NotNull(groups = { New.class, Existing.class })
  @Getter
  @Setter
  private String name;

  @NotNull(groups = { New.class, Existing.class, LocationGroup.class })
  @Getter
  @Setter
  private BuildingDto building;

  @NotNull(groups = { Existing.class })
  @Getter
  @Setter
  private Integer lockVersion;

}

BuildingDto建筑Dto

public class BuildingDto {

  @NotNull(groups = { Existing.class, LocationGroup.class })
  @Null(groups = { New.class })
  @Getter
  @Setter
  private Integer id;

  @NotNull(groups = { New.class, Existing.class })
  @Getter
  @Setter
  private String name;

  @NotNull(groups = { Existing.class })
  @Getter
  @Setter
  private List<LocationDto> locations;

  @NotNull(groups = { Existing.class })
  @Getter
  @Setter
  private Integer lockVersion;

}

Currently, I can validate in my LocationDto that the properties name and building are not null, but I can't validate the presence of the property id which is inside building .目前,我可以在我的LocationDto中验证属性namebuilding不为空,但我无法验证建筑物内的属性 ID 是否存在

If I use the @Valid annotation on the building property, it would validate all of its fields, but for this case I only want to validate its id .如果我在building属性上使用@Valid注释,它将验证其所有字段,但对于这种情况,我只想验证其id

How could that be done using javax validation?如何使用 javax 验证来完成?

This is my controller:这是我的控制器:

@PostMapping
public LocationDto createLocation(@Validated({ New.class, LocationGroup.class }) @RequestBody LocationDto location) {
  // save entity here...
}

This is a correct request body: (should not throw validation errors)这是一个正确的请求正文:(不应抛出验证错误)

{
  "name": "Room 44",
  "building": {
    "id": 1
  }
}

This is an incorrect request body: (must throw validation errors because the building id is missing )这是一个不正确的请求正文:(必须抛出验证错误,因为缺少建筑物 ID

{
  "name": "Room 44",
  "building": { }
}

In my Spring Boot project I have two DTO's which I'm trying to validate, LocationDto and BuildingDto.在我的 Spring Boot 项目中,我有两个 DTO 正在尝试验证,LocationDto 和 BuildingDto。 The LocationDto has a nested object of type BuildingDto. LocationDto 有一个 BuildingDto 类型的嵌套对象。

These are my DTO's:这些是我的 DTO:

LocationDto位置Dto

public class LocationDto {

  @NotNull(groups = { Existing.class })
  @Null(groups = { New.class })
  @Getter
  @Setter
  private Integer id;

  @NotNull(groups = { New.class, Existing.class })
  @Getter
  @Setter
  private String name;

  @NotNull(groups = { New.class, Existing.class, LocationGroup.class })
  @Getter
  @Setter
  private BuildingDto building;

  @NotNull(groups = { Existing.class })
  @Getter
  @Setter
  private Integer lockVersion;

}

BuildingDto建筑Dto

public class BuildingDto {

  @NotNull(groups = { Existing.class, LocationGroup.class })
  @Null(groups = { New.class })
  @Getter
  @Setter
  private Integer id;

  @NotNull(groups = { New.class, Existing.class })
  @Getter
  @Setter
  private String name;

  @NotNull(groups = { Existing.class })
  @Getter
  @Setter
  private List<LocationDto> locations;

  @NotNull(groups = { Existing.class })
  @Getter
  @Setter
  private Integer lockVersion;

}

Currently, I can validate in my LocationDto that the properties name and building are not null, but I can't validate the presence of the property id which is inside building .目前,我可以在我的LocationDto中验证属性namebuilding不为空,但我无法验证建筑物内的属性 ID 是否存在

If I use the @Valid annotation on the building property, it would validate all of its fields, but for this case I only want to validate its id .如果我在building属性上使用@Valid注释,它将验证其所有字段,但对于这种情况,我只想验证其id

How could that be done using javax validation?如何使用 javax 验证来完成?

This is my controller:这是我的控制器:

@PostMapping
public LocationDto createLocation(@Validated({ New.class, LocationGroup.class }) @RequestBody LocationDto location) {
  // save entity here...
}

This is a correct request body: (should not throw validation errors)这是一个正确的请求正文:(不应抛出验证错误)

{
  "name": "Room 44",
  "building": {
    "id": 1
  }
}

This is an incorrect request body: (must throw validation errors because the building id is missing )这是一个不正确的请求正文:(必须抛出验证错误,因为缺少建筑物 ID

{
  "name": "Room 44",
  "building": { }
}

@Valid annotation must be added to cascade class attributes. @Valid 注释必须添加到级联类属性。

LocationDTO.class LocationDTO.class

public class LocationDto {

  @Valid
  private BuildingDto building;
   
  .........

}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM