Skip to content

NamNV2496/springboot_Annotation

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

10 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Aspect of annotation

AnnotationHandler

@Before: Chạy trước khi method được thực thi
@After: Chạy sau khi method trả về một kết quả
@AfterReturning: Chạy sau khi method trả về một kết quả, lọc lấy kết quả trả về
@AfterThrowing: Chạy khi method xảy ra exception
@Around: Gồm các tất cả các advice trên
@Aspect: Đánh dấu đây là 1 aspect

we will have 2 way:

Way 1: use @Around

    Available role:
    GET http://localhost:8080/get
        Role: ADMIN
        execute controller from ExampleController
        Input :
        [Ljava.lang.Object;@24b43f46
        Hello admin
        Result :null

    Unavailable role:
    GET http://localhost:8080/get2
        FAIl
        
    Available role:
    GET http://localhost:8080/get3
        Role: ADMIN
        execute controller from ExampleController
        Hello admin
        
    Available role:
    GET http://localhost:8080/set
        Role: ADMIN
        set function called
     
    Unavailable role:
    GET http://localhost:8080/set2
        FAIl

if we define @Around all annotation @Before, @After or @AfterReturning @AfterThrowing cannot handle

if we define 2 @Around. it will handle with priority

Way 2: use @Pointcut and @Before, @After

    Available role:
    GET http://localhost:8080/get
        Role: ADMIN
        before catchSpecifyAnnotation
        Input :
        [Ljava.lang.Object;@1171f831
        Hello admin
        Result :null
        after catchSpecifyAnnotation

    Unavailable role:
    GET http://localhost:8080/get2
        FAIl
        
    Available role:
    GET http://localhost:8080/get3
        Role: ADMIN
        before catchSpecifyAnnotation
        Hello admin
        after catchSpecifyAnnotation
        
    Available role:
    GET http://localhost:8080/set
        Role: ADMIN
        before executionAnnotation
        set function called
        after executionAnnotation
     
    Unavailable role:
    GET http://localhost:8080/set2
        FAIl

Validate data with annotation

add class to implement: ConstraintValidator<AnnotationConfig, String> with override

public boolean isValid(String s, ConstraintValidatorContext constraintValidatorContext);

img.png

and add the annotation @Constraint(validatedBy = AnnotationValidate.class)

img_1.png

abnormal case:

img_2.png

happy case: img_3.png

Releases

No releases published

Packages

 
 
 

Contributors

Languages