如何在 Spring/Spring Boot 中做参数校验?你需要了解的都在这里! (2)

我们试一下所有参数输入正确的情况。

@RunWith(SpringRunner.class) @SpringBootTest @AutoConfigureMockMvc public class PersonControllerTest { @Autowired private MockMvc mockMvc; @Autowired private ObjectMapper objectMapper; @Test public void should_get_person_correctly() throws Exception { Person person = new Person(); person.setName("SnailClimb"); person.setSex("Man"); person.setClassId("82938390"); person.setEmail("Snailclimb@qq.com"); mockMvc.perform(post("/api/person") .contentType(MediaType.APPLICATION_JSON_UTF8) .content(objectMapper.writeValueAsString(person))) .andExpect(MockMvcResultMatchers.jsonPath("name").value("SnailClimb")) .andExpect(MockMvcResultMatchers.jsonPath("classId").value("82938390")) .andExpect(MockMvcResultMatchers.jsonPath("sex").value("Man")) .andExpect(MockMvcResultMatchers.jsonPath("email").value("Snailclimb@qq.com")); } }

验证出现参数不合法的情况抛出异常并且可以正确被捕获。

@Test public void should_check_person_value() throws Exception { Person person = new Person(); person.setSex("Man22"); person.setClassId("82938390"); person.setEmail("SnailClimb"); mockMvc.perform(post("/api/person") .contentType(MediaType.APPLICATION_JSON_UTF8) .content(objectMapper.writeValueAsString(person))) .andExpect(MockMvcResultMatchers.jsonPath("sex").value("sex 值不在可选范围")) .andExpect(MockMvcResultMatchers.jsonPath("name").value("name 不能为空")) .andExpect(MockMvcResultMatchers.jsonPath("email").value("email 格式不正确")); }

使用 Postman 验证结果如下:

Postman 验证结果

验证请求参数(Path Variables 和 Request Parameters)

Controller:

一定一定不要忘记在类上加上 Validated 注解了,这个参数可以告诉 Spring 去校验方法参数。

@RestController @RequestMapping("/api") @Validated public class PersonController { @GetMapping("/person/{id}") public ResponseEntity<Integer> getPersonByID(@Valid @PathVariable("id") @Max(value = 5,message = "超过 id 的范围了") Integer id) { return ResponseEntity.ok().body(id); } @PutMapping("/person") public ResponseEntity<String> getPersonByName(@Valid @RequestParam("name") @Size(max = 6,message = "超过 name 的范围了") String name) { return ResponseEntity.ok().body(name); } }

ExceptionHandler:

@ExceptionHandler(ConstraintViolationException.class) ResponseEntity<String> handleConstraintViolationException(ConstraintViolationException e) { return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(e.getMessage()); }

通过测试验证:

@Test public void should_check_param_value() throws Exception { mockMvc.perform(get("/api/person/6") .contentType(MediaType.APPLICATION_JSON_UTF8)) .andExpect(status().isBadRequest()) .andExpect(content().string("getPersonByID.id: 超过 id 的范围了")); } @Test public void should_check_param_value2() throws Exception { mockMvc.perform(put("/api/person") .param("name","snailclimbsnailclimb") .contentType(MediaType.APPLICATION_JSON_UTF8)) .andExpect(status().isBadRequest()) .andExpect(content().string("getPersonByName.name: 超过 name 的范围了")); } 验证 Service 中的方法

我们还可以验证任何Spring组件的输入,而不是验证控制器级别的输入,我们可以使用@Validated和@Valid注释的组合来实现这一需求。

一定一定不要忘记在类上加上 Validated 注解了,这个参数可以告诉 Spring 去校验方法参数。

@Service @Validated public class PersonService { public void validatePerson(@Valid Person person){ // do something } }

通过测试验证:

@RunWith(SpringRunner.class) @SpringBootTest @AutoConfigureMockMvc public class PersonServiceTest { @Autowired private PersonService service; @Test(expected = ConstraintViolationException.class) public void should_throw_exception_when_person_is_not_valid() { Person person = new Person(); person.setSex("Man22"); person.setClassId("82938390"); person.setEmail("SnailClimb"); service.validatePerson(person); } } Validator 编程方式手动进行参数验证

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zwypxy.html