In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-04-02 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >
Share
Shulou(Shulou.com)06/01 Report--
This article mainly introduces "how to check the springboot parameter format". In the daily operation, I believe many people have doubts about how to check the springboot parameter format. The editor consulted all kinds of data and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "how to check the springboot parameter format". Next, please follow the editor to study!
Springboot parameter format check
@ Validated
Literal meaning check
@ RequestBody
Needless to say, this annotation means to receive parameters in json format
@ Validated
Literal verification, which requires @ NotBlank or @ NotNull annotations to take effect.
Enter the request body parameters.
Springboot parameter annotation check 1. Add dependency org.springframework.boot spring-boot-starter-validation package com.xl.annotation;import lombok.Data;import org.hibernate.validator.constraints.Length;import org.hibernate.validator.constraints.Range;import javax.validation.constraints.*;import java.math.BigDecimal;import java.util.Date @ Datapublic class User {@ NotNull (message = "ID cannot be empty") @ Range (min = 1, max = 100, message = "ID must be between 1 and 100") private Integer id; @ NotNull (message = "name cannot be empty") @ Length (min = 2, max = 6, message = "name must be between 2 and 6 digits") private String name @ NotNull (message = "balance cannot be empty") @ DecimalMax (value = "30.50", message = "balance cannot exceed 30.5") @ DecimalMin (value = "1.50", message = "balance cannot be less than 1.5") private BigDecimal amount; @ NotNull (message = "birthday cannot be empty") @ Past (message = "birthday must be a thing of the past") private Date birthday @ NotBlank (message = "mailbox cannot be empty") @ Email (message = "incorrect mailbox format") private String email @ NotBlank (message = "Mobile number cannot be empty") @ Pattern (regexp = "^ (13 [0-9])) | (14 [579]) | (15 ([0-3] | [5-9])) | (16 [6]) | (17 [0135678]) | (18 [0-9]) | (19 [89]) d {8}) $", message = "incorrect Mobile number format") private String phone } 2.controller layer package com.xl.annotation;import io.swagger.annotations.Api;import io.swagger.annotations.ApiOperation;import io.swagger.annotations.ApiParam;import org.springframework.validation.BindingResult;import org.springframework.validation.ObjectError;import org.springframework.validation.annotation.Validated;import org.springframework.web.bind.annotation.*;import javax.validation.ValidationException;import javax.validation.constraints.Max;import javax.validation.constraints.NotNull @ RestController@Validated@Api (value = "Mobile Verification", description = "Mobile Verification") public class MobileController {@ ApiOperation ("Mobile Verification") @ RequestMapping ("/ phone") public String mobilePattern (Phone phone) {return "chengg" } @ PostMapping ("/ getUser") @ ApiOperation ("Mobile Verification 12") public String getUserStr (@ NotNull (message = "name cannot be empty") @ RequestParam String name, @ Max (value = 99, message = "not older than 99") @ RequestParam Integer age) {return "name:" + name + ", age:" + age " } / * @ PostMapping ("/ getUser1") @ ApiOperation ("Mobile Verification c") public String getUser (@ RequestBody @ Validated User user, BindingResult bindingResult) {validData (bindingResult); return "name:" + user.getName () + ", age:" + user.getAge ();} * / private void validData (BindingResult bindingResult) {if (bindingResult.hasErrors ()) {StringBuffer sb = new StringBuffer () For (ObjectError error: bindingResult.getAllErrors ()) {sb.append (error.getDefaultMessage ());} throw new ValidationException (sb.toString ()) } @ PostMapping ("/ test") @ ApiOperation (value = "Test", notes = "") public String test (@ ApiParam (name = "test", value = "Parameter", required = true) @ Validated @ RequestBody User test, BindingResult bindingResult) {validData (bindingResult); if (bindingResult.hasErrors ()) {String errorMsg = bindingResult.getFieldError (). GetDefaultMessage (); return errorMsg } return "Parameter verification passed";}} 3. Customize a class package com.xl.annotation;import org.springframework.http.HttpStatus;import org.springframework.web.bind.annotation.ExceptionHandler;import org.springframework.web.bind.annotation.ResponseStatus;import org.springframework.web.bind.annotation.RestControllerAdvice;import javax.validation.ConstraintViolation;import javax.validation.ConstraintViolationException;import javax.validation.ValidationException;import java.util.Set that throws an exception / * * Custom validation throws an exception * / @ RestControllerAdvicepublic class GlobalExceptionHandler {@ ExceptionHandler (ValidationException.class) @ ResponseStatus (HttpStatus.BAD_REQUEST) public String handle (ValidationException exception) {if (exception instanceof ConstraintViolationException) {ConstraintViolationException exs = (ConstraintViolationException) exception; Set
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.