I. use @ Valid form to verify
Add @ Min and other annotations to entity classes
1 @Entity 2 public class Girl { 3 4 @Id 5 @GeneratedValue 6 private Integer id; 7 8 private String cupSize; 9 @Min(value = 18,message = "No entry for minors!") 10 private Integer age; 11 ... 12 }
Add @ Valid annotation to the specified access method parameter and use the BindingResult bindingResult object to get the return result
1 @PostMapping(value = "/girls") 2 public Girl addgirl(@Valid Girl girl, BindingResult bindingResult){ 3 if (bindingResult.hasErrors()){ 4 System.out.println(bindingResult.getFieldError().getDefaultMessage()); 5 return null; 6 } 7 girl.setCupSize(girl.getCupSize()); 8 girl.setAge(girl.getAge()); 9 return girlRepository.save(girl); 10 }
2, Using AOP to process requests
Use AOP to process request log uniformly
Add aop dependency in pom file,
<!-- aop rely on --> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-aop</artifactId> </dependency>
Create a new aspect class:
1 @Aspect 2 @Component 3 public class HttpAspect { 4 5 @Pointcut("execution(public * com.cenobitor.controller.GirlController.girlList(..))") 6 public void log(){ 7 } 8 9 @Before("log()") 10 public void doBefore(){ 11 System.out.println(11111111); 12 } 13 14 @After("log()") 15 public void doAfter(){ 16 System.out.println(22222222); 17 } 18 19 }
1 /* 2 * Replace sout as a log to show more detailed information 3 * */ 4 @Aspect 5 @Component 6 public class HttpAspect { 7 //import org.slf4j.Logger; 8 private final static Logger LOGGER = LoggerFactory.getLogger(HttpAspect.class); 9 //Set tangent points,Simplified code 10 @Pointcut("execution(public * com.cenobitor.controller.GirlController.girlList(..))") 11 public void log(){ 12 } 13 14 //Get request information 15 @Before("log()") 16 public void doBefore(JoinPoint joinPoint){ 17 18 ServletRequestAttributes attributes = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes(); 19 HttpServletRequest request = attributes.getRequest(); 20 21 //URL 22 LOGGER.info("url={}",request.getRequestURL()); 23 //IP 24 LOGGER.info("ip={}",request.getRemoteAddr()); 25 //METHOD 26 LOGGER.info("method={}",request.getMethod()); 27 //Class method 28 LOGGER.info("class_method={}",joinPoint.getSignature().getDeclaringTypeName()+"."+joinPoint.getSignature().getName()); 29 //parameter 30 LOGGER.info("args={}",joinPoint.getArgs()); 31 32 } 33 34 @After("log()") 35 public void doAfter(){ 36 LOGGER.info("222222222222"); 37 } 38 //Print return results 39 @AfterReturning(returning = "object",pointcut = "log()") 40 public void doAfterReturning(Object object){ 41 LOGGER.info("response={}",object.toString()); 42 } 43 }
3, Unit test
- Basic code:
1 @RestController 2 public class CustomerController { 3 @Autowired 4 private CustomerService customerService; 5 6 @GetMapping(value = "customer_findById") 7 public Customer findById(@RequestParam("id") Integer id){ 8 return customerService.findById(id); 9 } 10 }vv
1 @Service 2 @Transactional 3 public class CustomerServiceImpl implements CustomerService { 4 @Autowired 5 private CustomerRepository customerRepository; 6 7 @Override 8 public Customer findById(Integer id) { 9 return customerRepository.findOne(id); 10 } 11 }
- service layer test
1 @RunWith(SpringRunner.class) 2 @SpringBootTest 3 public class CustomerServiceTest { 4 @Autowired 5 private CustomerService customerService; 6 7 @Test 8 public void findByIdTest(){ 9 Customer customer = customerService.findById(1); 10 Assert.assertEquals("Zhang San",customer.getName()); 11 12 } 13 }
- API test (i.e. controller layer test):
1 @RunWith(SpringRunner.class) 2 @SpringBootTest 3 @AutoConfigureMockMvc 4 public class CustomerControllerTest { 5 @Autowired 6 private MockMvc mvc; 7 8 @Test 9 public void findById() throws Exception { 10 mvc.perform(MockMvcRequestBuilders.get("/customer_findById?id=1")) 11 .andExpect(MockMvcResultMatchers.status().isOk()) 12 .andExpect(MockMvcResultMatchers.content().string("abc"));//Return result 13 } 14 }
- Common commands:
- Package and unit test
mvn clean package
-
- Perform packaging and skip all unit tests
mvn clean package -Dmaven.test.skip=true