如何使用 SpringMVC @Valid 验证 POST 中的字段而不是 PUT 中的空字段
How to use SpringMVC @Valid to validate fields in POST and only not null fields in PUT
我们正在使用 SpringMVC 创建一个 RESTful API 并且我们有一个 /products 端点,其中 POST 可用于创建新产品并使用 PUT 更新字段。我们还使用 javax.validation 来验证字段。
In POST 工作正常,但在 PUT 中用户只能传递一个字段,我不能使用@Valid,所以我需要复制所有使用 [=18= 注释进行的验证] PUT 的代码。
有人知道如何扩展@Valid 注释并创建类似@ValidPresents 或其他解决我问题的东西吗?
您可以使用带有 Spring org.springframework.validation.annotation.Validated
注释的验证组。
Product.java
class Product {
/* Marker interface for grouping validations to be applied at the time of creating a (new) product. */
interface ProductCreation{}
/* Marker interface for grouping validations to be applied at the time of updating a (existing) product. */
interface ProductUpdate{}
@NotNull(groups = { ProductCreation.class, ProductUpdate.class })
private String code;
@NotNull(groups = { ProductCreation.class, ProductUpdate.class })
private String name;
@NotNull(groups = { ProductCreation.class, ProductUpdate.class })
private BigDecimal price;
@NotNull(groups = { ProductUpdate.class })
private long quantity = 0;
}
ProductController.java
@RestController
@RequestMapping("/products")
class ProductController {
@RequestMapping(method = RequestMethod.POST)
public Product create(@Validated(Product.ProductCreation.class) @RequestBody Product product) { ... }
@RequestMapping(method = RequestMethod.PUT)
public Product update(@Validated(Product.ProductUpdate.class) @RequestBody Product product) { ... }
}
使用此代码后,Product.code
、Product.name
和 Product.price
将在创建和更新时进行验证。 Product.quantity
,但是,只会在更新时进行验证。
如果您实现接口 Validator 来自定义您的验证并通过反射检查任何类型的约束会怎样。
我们正在使用 SpringMVC 创建一个 RESTful API 并且我们有一个 /products 端点,其中 POST 可用于创建新产品并使用 PUT 更新字段。我们还使用 javax.validation 来验证字段。
In POST 工作正常,但在 PUT 中用户只能传递一个字段,我不能使用@Valid,所以我需要复制所有使用 [=18= 注释进行的验证] PUT 的代码。
有人知道如何扩展@Valid 注释并创建类似@ValidPresents 或其他解决我问题的东西吗?
您可以使用带有 Spring org.springframework.validation.annotation.Validated
注释的验证组。
Product.java
class Product {
/* Marker interface for grouping validations to be applied at the time of creating a (new) product. */
interface ProductCreation{}
/* Marker interface for grouping validations to be applied at the time of updating a (existing) product. */
interface ProductUpdate{}
@NotNull(groups = { ProductCreation.class, ProductUpdate.class })
private String code;
@NotNull(groups = { ProductCreation.class, ProductUpdate.class })
private String name;
@NotNull(groups = { ProductCreation.class, ProductUpdate.class })
private BigDecimal price;
@NotNull(groups = { ProductUpdate.class })
private long quantity = 0;
}
ProductController.java
@RestController
@RequestMapping("/products")
class ProductController {
@RequestMapping(method = RequestMethod.POST)
public Product create(@Validated(Product.ProductCreation.class) @RequestBody Product product) { ... }
@RequestMapping(method = RequestMethod.PUT)
public Product update(@Validated(Product.ProductUpdate.class) @RequestBody Product product) { ... }
}
使用此代码后,Product.code
、Product.name
和 Product.price
将在创建和更新时进行验证。 Product.quantity
,但是,只会在更新时进行验证。
如果您实现接口 Validator 来自定义您的验证并通过反射检查任何类型的约束会怎样。