SpringBoot 对@RequestBody定义对象的参数校

2022-05-09  本文已影响0人  一介书生独醉江湖

一、对象Field上加注解

# 在对象Field上面加上校验注解与返回消息
# 我这里使用@NotNull,    还有@NotEmpty 、@NotBlank等等;
# 如下:
@NotNull(message = "Id不能为空")
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.Data;

import javax.validation.constraints.NotNull;
import java.io.Serializable;

/**
 * @Author ds
 * @Date 2022-05-07
 */
@Data
@ApiModel(value = "RequestVo", description = "测试请求对象")
public class RequestVo implements Serializable {

    private static final long serialVersionUID = 1L;

    /**
     * Id
     */
    @NotNull(message = "Id不能为空")
    @ApiModelProperty(value="Id", example="1", notes="Id")
    private String id;

    /**
     * 名称
     */
    @ApiModelProperty(value="名称", example="1", notes="名称")
    private String name;

}

二、加入@Valid,定义BindingResult

# 在 @RequestBody注解的对象前,加入@Valid,并定义BindingResult,用来接收错误信息,以便返回给前端;
import com.github.xiaoymin.knife4j.annotations.ApiSort;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.validation.Valid;

/**
 * @Author ds
 * @Date 2022-05-06
 */
@ApiSort(1)
@Api(value = "test", tags = "test")
@RestController
@RequestMapping("/test")
public class TestController {

    @ApiOperation("测试swagger")
    @GetMapping(value = "")
    public String swagger(HttpServletRequest request, HttpServletResponse response) {
        return "测试swagger -> 成功";
    }

    @ApiOperation(value = "测试Valid", notes = "测试Valid", produces = "application/json")
    @PostMapping(value = "testValid", produces = {"application/json;charset=UTF-8"})
    public String testValid(@RequestBody @Valid RequestVo requestVo , BindingResult results) {
        if (results.hasErrors()){
            return results.getFieldError().getDefaultMessage();
        }
        return "success";
    }

}

三、BindingResult更多的方法

# 这里的BindingResult还有非常多的方法,自己可以根据需求翻一翻,下面举两个例子:
# getFieldErrorCount
# getFieldErrors
    @ApiOperation(value = "测试Valid", notes = "测试Valid", produces = "application/json")
    @PostMapping(value = "testValid", produces = {"application/json;charset=UTF-8"})
    public String testValid(@RequestBody @Valid RequestVo requestVo , BindingResult results) {
        if (results.hasErrors()){
            int fieldErrorCount = results.getFieldErrorCount();
            List<FieldError> fieldErrors = results.getFieldErrors();
            System.out.println("fieldErrorCount : " + fieldErrorCount);
            System.out.println("fieldErrors : " + fieldErrors.size());
            for(FieldError fieldError : fieldErrors){
                System.out.println("fieldError : " + fieldError.getDefaultMessage());
            }
            return results.getFieldError().getDefaultMessage();
        }
        return "success";
    }
# 打印:
fieldErrorCount : 2
fieldErrors : 2
fieldError : 名字不能为空
fieldError : Id不能为空
上一篇下一篇

猜你喜欢

热点阅读