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 > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
This article shows you how to verify the access parameters of springboot. The content is concise and easy to understand, which will definitely brighten your eyes. I hope you can get something through the detailed introduction of this article.
Use environment jdk1.8 spring 4 and above 1, add jar package com.github.fashionbrot mars-validated 1.0.2 2, Enable the use of valid 2 ways @ SpringBootApplication@EnableValidatedConfig (fileName = "test") / / fileName the default Chinese jar package comes with it. If you need batch customization, create your own test.properties and put it under resources in your own project, public class DemoApplication {public static void main (String [] args) {SpringApplication.run (DemoApplication.class). Args) }} @ Component@Configuration@EnableValidatedConfig (fileName = "valid_zh_CN") / / read the valid_zh_CN under mars-validated resources by default, so do not write default read Chinese public class ValidConfig {} 3, customize to implement global exception handling
Intercepting ValidatedException exception classes
@ RestControllerAdvice@Slf4jpublic class GlobalExceptionHandler {@ ExceptionHandler (Exception.class) @ ResponseStatus (HttpStatus.OK) public RespVo exception (Exception e) {log.error ("exception error:", e); return RespVo.fail (RespCode.FAIL.getMsg ()) } / * * parameters verify global processing * @ param e * @ return * / @ ExceptionHandler (ValidatedException.class) @ ResponseStatus (HttpStatus.OK) public RespVo ValidationException (ValidatedException e) {if (log.isDebugEnabled ()) {log.debug ("filedName: {} errorMsg: {}", e.getFieldName (), e.getMsg ()) } return RespVo.fail (e.getMsg (), RespCode.PARAMETER_ERROR.getCode ());} 4. Start using @ Validated / / API to enable verification @ Controllerpublic class TestController {@ Autowired private ValidService validService @ RequestMapping ("/ test") @ ResponseBody @ Validated / / API enables verification public String test (String abc,@Custom (min = 1dmsg = "request parameter failed") String abc1) {return abc+ ":" + abc1 } / / group verification parameter @ RequestMapping ("/ test1") @ ResponseBody @ Validated (groups = {EditGroup.class}) public String test1 (@ Custom (min = 1je groups = {EditGroup.class,AddGroup.class}) String abc1) {return abc1 } / / group verifies bean @ RequestMapping ("/ test2") @ ResponseBody @ Validated (groups = AddGroup.class) public String test2 (GroupModel groupModel) {return groupModel.getAbc () }} 5. Note AnnotationSupported data types uses NotBlankString to verify whether the String string is empty NotNullString,Object,Integer,Long,Double,Short,Float,BigDecimal, BigInteger verifies that the object is empty NotEmptyString verification string cannot be empty AssertFalseBoolean,boolean,String can only verify for falseAssertTrueBoolean,boolean,String bank card CreditCardString verify credit card DefaultInteger,Double,Long,Short,Float,BigDecimal,String setting default value DigitsString verification is numeric EmailString verification is mailbox IdCardString verification is ID card Verify 18-year-old Lengthint,long,short,double,Integer,Long,Float,Double,Short,String verification length PatternString regular expression verification PhoneString verification mobile phone number is correct Sizeint,long,short,Integer,Long,Short verification size value NotEqualSizeString verification length 6, custom note (1) definition annotation @ Documented@Target ({ElementType.FIELD, ElementType.PARAMETER}) @ Retention (RetentionPolicy.RUNTIME) @ Constraint (validatedBy = {CustomConstraintValidator.class) CustomConstraintValidator2.class}) / / can correspond to multiple or one implementation class / / CustomConstraintValidator implementation class 1//CustomConstraintValidator2 implementation class 2public @ interface Custom {/ / com.sgr.valid.Custom.msg jar package under valid_zh_CN.properties corresponding msg String msg () default "com.sgr.valid.Custom.msg" Int min (); Class [] groups () default {};} (2) implement class CustomConstraintValidator like CustomConstraintValidator2public class CustomConstraintValidator implements ConstraintValidator {@ Override public boolean isValid (Custom custom, Object var1) {/ * custom method * / int min=custom.min (); / * valud * / System.out.println (var1); var1= "567" / * return true successful false verification failed * / return false;} / / parameters can be modified @ Override public Object modify (Custom annotation, Object var) {System.out.println ("CustomConstraintValidator:" + var); return var+ "1";}}
The above is how to verify the access parameters of springboot. Have you learned any knowledge or skills? If you want to learn more skills or enrich your knowledge reserve, you are welcome to follow the industry information channel.
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.