首页 > 解决方案 > 在 Get 方法中验证 DTO

问题描述

我想将 DTO 传递给 Get 方法:

@GetMapping
public void my_get(MyDto myDto){
    
}

上面的代码工作正常,但字段验证不起作用。

@Data
@AllArgsConstructor
@NoArgsConstructor
public class MyDto {
    @NotNull
    @NotBlank(message = ValidateConstant.NOT_BLANK_MESSAGE)
    String id;

    String uri;

    String type;

    String version;
}

我还添加@Valid了注释,但 Get 方法也返回 200 作为响应。

标签: javaspringspring-boot

解决方案


添加@Valid注释

public void my_get(@Valid MyDto myDto){

如果这没有帮助,@Validated请在班级级别添加。


推荐阅读