First, @Valid verification
Scenario: When adding departmental information, increase the number of people under the Department; Meeting a certain number of people can be added successfully, otherwise null is returned
Number of departments @min (value=18,message= "not meet 18 people, can not set up a new department!" ") Private Integer Count;//get/set method @postmapping (value="/savedept ") public depttemp savedept (@Valid depttemp temp,// The error message returns the object Bindingresult result) {if (Result.haserrors ()) {////If there is an error message, print and return System.out.println (Result.getfielderror (). Getdefaultmessage ()); return null;} Temp.setdname (Temp.getdname ()); Temp.setloc (Temp.getloc ()); Temp.setcount (Temp.getcount ()); return Deptrespository.save (temp);}
Because, we have processed the validation of different outdated information, if the validation does not pass the console will print "Custom authentication information, such as: Do not meet 18 people, can not set up a new department!" ”
Otherwise, this access path will return "500" error, run-time exception;
The database can be added if the correct conditions are met, otherwise it will be shut down.
Second, AOP Unified processing Log
AOP is a programming norm , which is independent of language and is a programming idea.
Scenario: Record requests for each visit
First step: Add dependencies
<dependency><groupId>org.springframework.boot</groupId><artifactId> Spring-boot-starter-aop</artifactid></dependency>
Step two: Define the Aspect class
@Aspect @component//The file is referenced to the spring container in the public class Httpaspect {@Before ("Execution (public * Com.test.controller.DeptController.getList (..)) ") public void log () {System.out.println ("Aspect:log"); }}
The log method acts on top of the GetList method and prints the journal before the method.
There will also be after methods, as in the same way.
Spring comes with a log frame, the underlying implementation is Logback private final static Logger Logger = Loggerfactory.getlogger (httpaspect.class); @Pointcut ("Execution (public * com.test.controller.DeptController.getList (..))") public void log () {} @Before ("Log ()") public void Dobefore () {logger.info ("Aspect:before:log");} @After ("log ()") public void Doafter () {logger.info ("Aspect:after:log");}
Spring-boot Two--web environment