Validation validation is integrated with SpringBoot.

The original message was as follows:

^A-\d{12}-\d{4}${A-\d{12}-\d{4}${A-\d{4}${A-\d{12}-\d{4}$

Anyway, I don’t really believe this kind of response, and my gut tells me this fan must be using it wrong. But since fans have doubts or need to write a special demo to verify. Just write.

The integration process is simple, with the following dependencies introduced directly into the original project’s POM file:

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-validation</artifactId>
</dependency>
Copy the code

Once introduced, you can use @VALID and so on in your project for validation.

Here is a simple example to verify that a submitted argument is Valid based on @valid. Start by creating an entity class, LoginForm (using Lombok) :

@Data public class LoginForm {

@notblank (message = "Email cannot be empty ") @email private String Email; @notblank (message = "password cannot be blank ") @length (min = 6,message =" password must contain at least 6 characters ") private String password; @notblank @pattern (regexp = "^A-\ d{12}-\ d{4}$",message = "error ") private String other;Copy the code

} the last other field is used to verify the regular expression that the fan is asking about.

Then, after creating a Controller class:

@Slf4j @RestController public class LoginController {

@PostMapping("/login") public void login(@Valid LoginForm loginForm, BindingResult bindingResult) { log.info("loginForm:{}", loginForm); if (bindingResult.hasErrors()) { for (ObjectError error : bindingResult.getAllErrors()) { log.info("error:{}", error.getDefaultMessage()); }} else {log.info(" Parameter verification successful! ") ); }}Copy the code

} If the parameter verification fails, error logs are displayed. If the verification succeeds, “Parameter verification succeeded!” is displayed. . Since it’s a Post request, let’s write a unit test for the service:

@Slf4j @SpringBootTest @RunWith(SpringRunner.class) @AutoConfigureMockMvc @WebAppConfiguration public class TestLoginController {

@Autowired private WebApplicationContext webApplicationContext; @Autowired private MockMvc mockMvc; @Before public void setUp() { mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build(); } @Test public void testLogin() throws Exception { MvcResult mvcResult = mockMvc.perform(MockMvcRequestBuilders.post("/login") .param("email", "[email protected]") .param("password", "123456") .param("other", "A-123456789012-1234") .accept(MediaType.APPLICATION_JSON)) .andDo(MockMvcResultHandlers.print()) .andReturn(); int status = mvcResult.getResponse().getStatus(); String content = mvcresult.getresponse ().getContentAsString(); Log.info (" return result content={}", content); Assert.assertEquals(200, status); }Copy the code

} Execute the print test and check the log. It is found that the verification passes and there is no error. At this point, the fans raised doubts are not tenable. The question the fan encountered was probably what detail caused the validation to fail, and the lesson he learned was that validation validation parameters were flawed.

This problem is more likely to occur when no one is talking to each other. This is why technical people should have their own circle, gather together, discuss and see the real knowledge. Finally, welcome to visit my wechat public number: Program New Vision, also welcome to put forward more questions, we make progress together.