gpt4 book ai didi

java - 即使请求正文中存在错误,BindingResult 也不会显示错误

转载 作者:行者123 更新时间:2023-12-02 01:58:29 28 4
gpt4 key购买 nike

我要验证传入请求正文中的字段验证错误,但 BindingResult 中没有捕获任何错误。

我尝试在 DTO 中使用 @Valid 注释和约束注释,但它不起作用。

这是我的 Controller 类

管理 Controller

@Slf4j
@RestController
@RequestMapping("/admin/staff")
@CrossOrigin("*")
public class AdminController {

@Autowired
private AdminService adminService;

@PreAuthorize("hasAnyRole('ROLE_SUPER_ADMIN')")
@PutMapping("/{id}")
public ResponseEntity<GenericResponse<UserDetailsDTO>> updateUser(
@PathVariable("id") String userId, @Valid @RequestBody
GenericRequest<StaffUpdateRequestDTO> request, BindingResult bindingResult
) {
exceptionDTO.setMessageId(request.getHeader().getMsgId());
if(bindingResult.hasErrors()){
throw ValidationError.builder().bindingResult(bindingResult).build();
}
if(!userId.equalsIgnoreCase(request.getTxnRequest().getUserDetails().getUserId())){
throw ValidationError.builder().bindingResult(bindingResult).build();
}
GenericResponse<UserDetailsDTO> response = new GenericResponse<>();
response.setHeader(adminUtil.createResponseHeader());
response.getHeader().setMsgId(request.getHeader().getMsgId());
response.getHeader().setChannelId(request.getHeader().getChannelId());
response.setTxnResponse(adminService.saveUser(request.getTxnRequest()));
return ResponseEntity.status(HttpStatus.OK).body(response);
}

}

这是 DTO

通用请求

@Data
public class GenericRequest<T> {

@NotNull(message = "Validation failure")
private HeaderDTO header;

@NotNull(message = "Validation failure")
private T txnRequest;
}

标题DTO

@Data
public class HeaderDTO {

@NotNull
@NotEmpty
private String msgId;

@NotNull
@NotEmpty
private String timestamp;

@NotNull
@NotEmpty
private String channelId;
}

StaffUpdateRequestDTO

@Data
public class StaffUpdateRequestDTO {

@NotNull(message = "Validation failure")
private UserDetailsDTO userDetails;

@NotNull(message = "Validation failure")
@NotEmpty(message = "Validation Failure")
private String approvedBy;

}

用户详细信息DTO

@Data
public class UserDetailsDTO {

@NotEmpty(message = "Validation failure")
@NotNull(message = "Validation failure")
private String userId;
@NotEmpty(message = "Validation failure")
@NotNull(message = "Validation failure")
private String userName;
@NotNull(message = "Validation failure")
@NotEmpty(message = "Validation failure")
private String emailId;
@NotEmpty(message = "Validation failure")
@NotNull(message = "Validation failure")
private String countryCode;
@NotNull(message = "Validation failure")
private List<RoleDetailsDTO> roles;
}

角色详细信息DTO

@Data
public class RoleDetailsDTO {

@NotEmpty(message = "Validation failure")
@NotNull(message = "Validation failure")
private String roleId;
@NotEmpty(message = "Validation failure")
@NotNull(message = "Validation failure")
private String roleName;
}

即使我不从 UserDetailsDTO 发送某些字段,BindingResult 错误检查也会在以下行中失败:

if(bindingResult.hasErrors()){
throw ValidationError.builder().bindingResult(bindingResult).build();
}

发送请求正文的示例:

{
"header":{
"msgId":"87612983612",
"timestamp":"2019-08-05T12:12:12.122",
"channelId":"BDSHK"
},
"txnRequest":{
"userDetails":{
"userId": "deepakkumarc",
"userName": "Deepak Kumar Choudhary",
"roles": [
{
"roleId": "SUPER_ADMIN",
"roleName": "SUPER ADMIN"
},
{
"roleId":"LI_PRODUCT_ADMIN",
"roleName":"LIFE INSURANCE PRODUCT ADMIN"
},
{
"roleId":"GI_PRODUCT_ADMIN",
"roleName":"GENERAL INSURANCE PRODUCT ADMIN"
}
]
},
"approvedBy":"deepakkumarc"
}
}

如上所示,我没有发送 emailId 和 CountryCode,但这通过了验证。期望的是,如果任何验证失败,则会收到验证错误。

如何对此请求正文实现验证?

最佳答案

我终于明白出了什么问题。我们需要将 @Valid 放在字段级别,如下所示:

@Data
public class StaffUpdateRequestDTO {

@NotNull(message = "Validation failure")
@Valid
private UserDetailsDTO userDetails;

@NotNull(message = "Validation failure")
@NotEmpty(message = "Validation Failure")
private String approvedBy;

}

关于java - 即使请求正文中存在错误,BindingResult 也不会显示错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57387044/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com