if (bindingResult.hasErrors()) { return Result.failure(bindingResult.getAllErrors() .get(0) .getDefaultMessage()); }
时间: 2024-04-11 10:31:03 浏览: 7
这段代码是一个表单校验的逻辑。当使用Spring框架进行表单校验时,如果存在错误,bindingResult对象会保存这些错误信息。通过调用bindingResult.hasErrors()方法可以判断是否存在错误。如果存在错误,可以通过bindingResult.getAllErrors()方法获取所有错误信息列表,并通过getDefaultMessage()方法获取第一个错误信息的默认消息。然后将错误消息封装到Result对象中,返回给前端或其他调用方。
相关问题
@PostMapping("/v1/straddle") public Result<String> paging(@RequestBody @Valid final SealPagingDTO sealPagingDTO, final BindingResult bindingResult) { if (log.isInfoEnabled()) { log.info("开始调用骑缝章服务.applyNo:[{}],param:[{}]", sealPagingDTO.getApplyNo(), sealPagingDTO); } if (bindingResult.hasErrors()) { return Result.failure(bindingResult.getAllErrors() .get(0) .getDefaultMessage()); } // 开启耗时统计. final StopWatch sw = new StopWatch(); sw.start(); // 骑缝章. final SealResultDTO<SealPagingResultDTO> resultDTO = this.sealPagingService.sealPaging(sealPagingDTO); sw.stop(); if (log.isInfoEnabled()) { log.info("调用签章骑缝章服务调用结束.applyNo:[{}],result:[{}],耗时为:[{}]毫秒", sealPagingDTO.getApplyNo(), resultDTO, sw.getTotalTimeMillis()); } return Optional.of(resultDTO) .filter(res -> SealConstant.HTTP_SUCCESS_CODE.equals(res.getRespCode())) .map(res -> Result.success(res.getDatas())) .orElseGet(() -> Result.failure(resultDTO.getRespMsg())); } 第一return返回的是String类型,第二个return成功返回的是T类型,怎么修改Result<String>
要修改第一个返回类型为String的问题,可以将方法的返回类型改为Result<String>,并在return语句中使用Result.success()方法来封装返回结果。具体修改如下:
```java
@PostMapping("/v1/straddle")
public Result<String> paging(@RequestBody @Valid final SealPagingDTO sealPagingDTO,
final BindingResult bindingResult) {
if (log.isInfoEnabled()) {
log.info("开始调用骑缝章服务.applyNo:[{}],param:[{}]", sealPagingDTO.getApplyNo(), sealPagingDTO);
}
if (bindingResult.hasErrors()) {
return Result.failure(bindingResult.getAllErrors()
.get(0)
.getDefaultMessage());
}
// 开启耗时统计.
final StopWatch sw = new StopWatch();
sw.start();
// 骑缝章.
final SealResultDTO<SealPagingResultDTO> resultDTO = this.sealPagingService.sealPaging(sealPagingDTO);
sw.stop();
if (log.isInfoEnabled()) {
log.info("调用签章骑缝章服务调用结束.applyNo:[{}],result:[{}],耗时为:[{}]毫秒",
sealPagingDTO.getApplyNo(),
resultDTO,
sw.getTotalTimeMillis());
}
return Optional.of(resultDTO)
.filter(res -> SealConstant.HTTP_SUCCESS_CODE.equals(res.getRespCode()))
.map(res -> Result.success(res.getDatas()))
.orElseGet(() -> Result.failure(resultDTO.getRespMsg()));
}
```
这样就将第一个return语句的返回类型修改为Result<String>了。
if (bindingResult.hasErrors()) { for (ObjectError error : bindingResult.getAllErrors()) { return new PayResult<ZhenghangParamDTO>(PayResultConstant.PARAM_VALID_FAILED.getCode(), error.getDefaultMessage(), null); } } if (StringUtils.isBlank(orgCode)){ return new PayResult<ZhenghangParamDTO>(PayResultConstant.PARAM_VALID_FAILED.getCode(), "公司编码不能为空", null); } if (epayBankParamDTO.getCo_list() == null || epayBankParamDTO.getCo_list().isEmpty()){ return new PayResult<ZhenghangParamDTO>(PayResultConstant.PARAM_VALID_FAILED.getCode(), "关联订单信息不能为空", null); } if (!isIntAndGreaterThanZero(epayBankParamDTO.getBill_acct())){ return new PayResult(PayResultConstant.PARAM_VALID_FAILED.getCode(),"交易金额必须大于0,必须为整数", null); } if (StringUtils.isBlank(epayBankParamDTO.getCanInputName())){ epayBankParamDTO.setCanInputName("1"); } if (StringUtils.isBlank(epayBankParamDTO.getOrder_type())){ epayBankParamDTO.setOrder_type("901"); } if (StringUtils.isBlank(epayBankParamDTO.getCurr_type())){ epayBankParamDTO.setCurr_type("156"); }
这段代码可以进行如下优化:
```java
if (bindingResult.hasErrors()) {
ObjectError error = bindingResult.getAllErrors().get(0);
return new PayResult<>(PayResultConstant.PARAM_VALID_FAILED.getCode(), error.getDefaultMessage(), null);
}
if (StringUtils.isBlank(orgCode)) {
return new PayResult<>(PayResultConstant.PARAM_VALID_FAILED.getCode(), "公司编码不能为空", null);
}
if (epayBankParamDTO.getCo_list() == null || epayBankParamDTO.getCo_list().isEmpty()) {
return new PayResult<>(PayResultConstant.PARAM_VALID_FAILED.getCode(), "关联订单信息不能为空", null);
}
if (!isIntAndGreaterThanZero(epayBankParamDTO.getBill_acct())) {
return new PayResult<>(PayResultConstant.PARAM_VALID_FAILED.getCode(), "交易金额必须大于0,必须为整数", null);
}
epayBankParamDTO.setCanInputName(StringUtils.defaultIfBlank(epayBankParamDTO.getCanInputName(), "1"));
epayBankParamDTO.setOrder_type(StringUtils.defaultIfBlank(epayBankParamDTO.getOrder_type(), "901"));
epayBankParamDTO.setCurr_type(StringUtils.defaultIfBlank(epayBankParamDTO.getCurr_type(), "156"));
```
这个优化做了以下几个改进:
1. 在第一个 `if` 语句块里,我们只需要获取第一个错误信息,而不需要遍历所有的错误信息,这样可以减少不必要的操作。
2. 对于后面三个 `if` 语句块,我们可以使用 `StringUtils.defaultIfBlank()` 方法来简化代码,这个方法会在字符串为空时返回一个默认值,这样可以避免使用 `if` 语句来判断是否为空。