Spring-boot Two--web environment

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.