In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-02-24 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/02 Report--
Most people do not understand the knowledge points of this article, "what is the method of SpringBoot integrated Validation parameter verification?", so the editor summarizes the following, detailed content, clear steps, and has a certain reference value. I hope you can get something after reading this article. Let's take a look at this "SpringBoot integrated Validation parameter verification method is what" article.
1. Dependence
SpringBoot already includes the validator package in the web initiator
Org.springframework.boot spring-boot-starter-web
Non-SpringBoot project, you need to customize the introduction of dependencies
Org.hibernate.validator hibernate-validator 6.1.5.Final org.glassfish jakarta.el 3.0.32, commonly used constraint description package com.smile.project.validator.utils Public class Common constraint description {/ * * @ Null: element is null * @ NotNull: element is not null * @ AssertTrue: element is true * @ AssertFalse: element is false * @ Min (value): the value of the number is greater than or equal to the specified minimum value * @ Max (value): the value of the number is less than or equal to the specified maximum value * @ DecimalMin (value) ): large value is greater than or equal to specified minimum value * @ DecimalMax (value): large value is less than or equal to specified maximum value * @ Size (max=) Min=): the size of the element is within the specified range * @ Digits (integer,fraction): the element is a number Its value must be within an acceptable range * @ Past: a past date * @ Future: a future date * @ Pattern (regex= Flag=): specified regular expression * @ URL: must be a URL * @ Email: must be in email format * @ NotBlank: string cannot be empty * @ NotEmpty: collection cannot be empty * @ Length: length must be within specified range * @ Valid: check entity class * /} 3, entity constraint example import com.smile.project.validator.utils.Gender Import lombok.Data;import javax.validation.constraints.Email;import javax.validation.constraints.NotNull;import javax.validation.constraints.Size; @ Datapublic class SysUser {private Long id; @ NotNull (message = "user name cannot be empty") @ Size (min = 3recovermax = 5 max message = "user name length is between {min}-{max}") private String username; @ NotNull (message = "nickname cannot be empty") private String name @ NotNull (message = "password cannot be empty") private String password; @ Email (message = "invalid mailbox format") private String email; private String gender;} 4, control layer example
Need to add @ Validated comment to class
If the parameter is an entity, you need to add the @ Valid annotation
Import com.smile.project.validator.entity.SysUser;import org.springframework.http.ResponseEntity;import org.springframework.validation.annotation.Validated;import org.springframework.web.bind.annotation.GetMapping;import org.springframework.web.bind.annotation.PostMapping;import org.springframework.web.bind.annotation.RestController;import javax.validation.Valid;import javax.validation.constraints.NotNull @ RestController@Validatedpublic class SysUserController {/ * method parameter is entity check * / @ PostMapping ("/ register") public ResponseEntity register (@ Valid SysUser sysUser) {return ResponseEntity.ok (sysUser) } / * method parameter verification * / @ GetMapping ("user") public ResponseEntity getUser (@ NotNull (message = "user name cannot be empty") String username) {SysUser sysUser = new SysUser (); sysUser.setName ("smile"); return ResponseEntity.ok (sysUser);}} 5, exception catch
Global exception capture, which catches the exception when the parameter verification is illegal and returns it to the front end
Import org.springframework.http.ResponseEntity;import org.springframework.validation.FieldError;import org.springframework.validation.ObjectError;import org.springframework.web.bind.MethodArgumentNotValidException;import org.springframework.web.bind.annotation.ControllerAdvice;import org.springframework.web.bind.annotation.ExceptionHandler;import org.springframework.web.bind.annotation.ResponseBody;import javax.validation.ConstraintViolation;import javax.validation.ConstraintViolationException;import java.util.HashMap;import java.util.List;import java.util.Set / * * Global exception capture * catch an exception when the flight parameter verification is illegal, and return it to the front end * / @ ControllerAdvicepublic class GlobHandler {/ * capture method parameter verification exception * / @ ExceptionHandler (ConstraintViolationException.class) @ ResponseBody public ResponseEntity constraintViolationExceptionHandler (ConstraintViolationException e) {Set [] groups () default {}; Class
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.