spingboot integrates JSR303 checksum spring to achieve group check

1. JSR303 verification

1. Dependence

		<!-- JSR303依赖 -->
		<dependency>
			<groupId>org.springframework.boot</groupId>
			<artifactId>spring-boot-starter-validation</artifactId>
		</dependency>

2. Global exception handling

    @ResponseBody
    @ExceptionHandler(value = MethodArgumentNotValidException.class)
    public ResponseResult handleValidException(MethodArgumentNotValidException e) {
        log.error("数据校验出现问题{},异常类型{}", e.getMessage(), e.getClass());
        BindingResult bindingResult = e.getBindingResult();
        Map<String, String> errorMap = new HashMap<>();
        bindingResult.getFieldErrors().forEach(fieldError -> {
            errorMap.put(fieldError.getField(), fieldError.getDefaultMessage());
        });
        return ResultUtils.failure(errorMap.toString());
    }
}

3. The use judgment field cannot be empty


    @NotBlank(message = "订单号不能为空")
    @ApiModelProperty(value = "试用订单id" , name = "goodsUsageId")
    private String goodsUsageId;

4. Use @Valid annotation directly

    @PostMapping("/addEvaluationRecord")
    @ApiOperation(value = "提交测评报告")
    public ResponseResult addEvaluationRecord(HttpServletRequest httpServletRequest, @Valid  @RequestBody EvaluationRecordAddModel evaluationRecordAddModel){
        String accountId = (String) httpServletRequest.getAttribute("accountId");
        if (StringUtils.isEmpty(accountId)) return ResultUtils.failure("用户id不存在");
        evaluationTemplateService.addEvaluationRecord(Integer.valueOf(accountId),evaluationRecordAddModel);
        return null;
    }

2. Group check

1. Create and modify the group interface UpdateGroup

package com.xihongshi.common.valid;

/**
 * 修改分组校验
 * @author mgq
 * @since 2021-03-29 11:24
 */
public interface UpdateGroup {
}

2. Add to the attribute of the class, groups = {UpdateGroup.class}

    @NotNull(message = "修改时,id不能为空",groups = {UpdateGroup.class})
    private Integer id;

3. Add @Validated(UpdateGroup.class) annotation to the controller interface

Note: @Validated is provided by spring, which can realize the grouping function of validation

    /**
     * 修改
     */
    @PostMapping("/update")
    @ApiOperation(value = "修改收货地址信息")
    public ResponseResult update(HttpServletRequest httpServletRequest,@Validated(UpdateGroup.class) @RequestBody AccountAddressModel accountAddressModel){
        AccountAddress accountAddress = new AccountAddress();
        BeanUtils.copyProperties(accountAddressModel, accountAddress);
        String accountId = (String) httpServletRequest.getAttribute("accountId");
        QueryWrapper<AccountAddress> qw = new QueryWrapper<>();
        qw.eq("user_id",accountId);
        qw.eq("id",accountAddressModel.getId());
        accountAddress.setUserId(Integer.valueOf(accountId));
        boolean update = accountAddressService.update(accountAddress, qw);
        if (!update) {
            return ResultUtils.failure("修改数据失败");
        }
        return ResultUtils.success();
    }

 

Guess you like

Origin blog.csdn.net/qq_39564710/article/details/115235026