<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                我們后臺使用`spring` 為我們提供好的統一校驗的工具`spring-boot-starter-validation`對請求進行校驗。 ```xml <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-validation</artifactId> </dependency> ``` 這里通過注解封裝了幾種常用的校驗 - `@NotNull` 不能為null - `@NotEmpty` 不能為null、空字符串、空集合 - `@NotBlank` 不能為null、空字符串、純空格的字符串 - `@Min` 數字最小值不能小于x - `@Max` 數字最大值不能大于x - `@Email` 字符串為郵件格式 - `@Max` 數字最大值不能大于x - `@Size` 字符串長度最小為x、集合長度最小為x - `@Pattern` 正則表達式 我們以`SysUser`為例,看看怎么使用 ```java public class SysUser implements Serializable { private static final long serialVersionUID = 1L; /** * 用戶ID * */ @TableId private Long userId; /** * 用戶名 */ @NotBlank(message="用戶名不能為空") @Size(min = 2,max = 20,message = "用戶名長度要在2-20之間") private String username; /** * 密碼 */ @JsonProperty(access = JsonProperty.Access.WRITE_ONLY) private String password; /** * 郵箱 */ @NotBlank(message="郵箱不能為空") @Email(message="郵箱格式不正確") private String email; /** * 手機號 */ @Pattern(regexp="0?1[0-9]{10}",message = "請輸入正確的手機號") private String mobile; /** * 狀態 0:禁用 1:正常 */ private Integer status; /** * 用戶所在店鋪id */ private Long shopId; /** * 角色ID列表 */ @TableField(exist=false) private List<Long> roleIdList; /** * 創建時間 */ private Date createTime; } ``` 我們在Controller層使用該bean,并使用`@Valid`注解,使校驗的注解生效,如`SysUserController` : ```java @RestController @RequestMapping("/sys/user") public class SysUserController { /** * 保存用戶 */ @SysLog("保存用戶") @PostMapping @PreAuthorize("@pms.hasPermission('sys:user:save')") public ResponseEntity<String> save(@Valid @RequestBody SysUser user){ String username = user.getUsername(); SysUser dbUser = sysUserService.getOne(new LambdaQueryWrapper<SysUser>() .eq(SysUser::getUsername, username)); if (dbUser!=null) { return ResponseEntity.badRequest().body("該用戶已存在"); } user.setShopId(SecurityUtils.getSysUser().getShopId()); user.setPassword(passwordEncoder.encode(user.getPassword())); sysUserService.saveUserAndUserRole(user); return ResponseEntity.ok().build(); } } ``` 并且在`DefaultExceptionHandlerConfig` 攔截由`@Valid` 觸發的異常信息并返回: ```java @Controller @RestControllerAdvice public class DefaultExceptionHandlerConfig { @ExceptionHandler(BindException.class) public ResponseEntity<String> bindExceptionHandler(BindException e){ e.printStackTrace(); return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(e.getBindingResult().getFieldErrors().get(0).getDefaultMessage()); } @ExceptionHandler(MethodArgumentNotValidException.class) public ResponseEntity<String> methodArgumentNotValidExceptionHandler(MethodArgumentNotValidException e){ e.printStackTrace(); return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(e.getBindingResult().getFieldErrors().get(0).getDefaultMessage()); } } ```
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看