Kotlin SpringMVC HandlerInterceptor – Spring Boot

https://grokonez.com/spring-framework/spring-boot/kotlin-spring-boot/kotlin-springmvc-handlerinterceptor-spring-boot

Kotlin SpringMVC HandlerInterceptor – Spring Boot

In the tutorial, JavaSampleApproach will show you how to create a Kotlin SpringMVC HandlerInterceptor that provides a mechanism to intercept requests {preProcessing, postProcessing and afterCompletion} with SpringBoot.

I. Technologies

– Java 1.8
– Maven 3.6.1
– Spring Tool Suite – Version 3.9.0.RELEASE
– Spring Boot – 1.5.9.RELEASE
– Kotlin 1.1.61

II. Goal

We use SpringToolSuite to create a Kotlin SpringBoot project as below structure:

Kotlin SpringMVC HandlerInterceptor - project structure

Use HandlerInterceptor to intercept requests:


@Component
class LogInterceptor: HandlerInterceptor{
    
    val log = LoggerFactory.getLogger(LogInterceptor::class.java);
    
    override fun preHandle(request: HttpServletRequest, response: HttpServletResponse, dataObject: Any) : Boolean{
        log.info("1. from PreHandle method.")
        return true
    }
    
    override fun postHandle(request: HttpServletRequest, response: HttpServletResponse, dataObject: Any, model: ModelAndView?){
        log.info("3. from PostHandle method.")
    }
    
    override fun afterCompletion(request: HttpServletRequest, response: HttpServletResponse, dataObject: Any, e: Exception?) {
        log.info("4. from AfterCompletion method - Request Completed!")
    }
}

With a simple RestAPI:


@GetMapping("/hello")
fun hello(): String{
    log.info("2. Actual Excuting - Welcome to @GetMapping: /hello!")
    return "Hello World!"
}

We make a request http://localhost:8080/hello, see logs:

More at:

https://grokonez.com/spring-framework/spring-boot/kotlin-spring-boot/kotlin-springmvc-handlerinterceptor-spring-boot

Kotlin SpringMVC HandlerInterceptor – Spring Boot

Bình luận


White
{{ comment.user.name }}
Bỏ hay Hay
{{comment.like_count}}
Male avatar
{{ comment_error }}
Hủy
   

Hiển thị thử

Chỉnh sửa

Male avatar

loveprogramming

545 bài viết.
98 người follow
Kipalog
{{userFollowed ? 'Following' : 'Follow'}}
Cùng một tác giả
Male avatar
1 0
Tutorial Link: (Link) (Ảnh) Django is a Pythonbased free and opensource web framework that follows the modeltemplateview architectural pattern. A...
loveprogramming viết 7 tháng trước
1 0
Male avatar
1 0
https://loizenai.com/angular11nodejspostgresqlcrudexample/ Angular 11 Node.js PostgreSQL Crud Example (Ảnh) Tutorial: “Angular 11 Node.js Postg...
loveprogramming viết 6 tháng trước
1 0
Male avatar
1 0
Angular Spring Boot jwt Authentication Example Github https://loizenai.com/angularspringbootjwt/ (Ảnh) Tutorial: ” Angular Spring Boot jwt Authe...
loveprogramming viết 6 tháng trước
1 0
Bài viết liên quan
Male avatar
0 0
https://grokonez.com/frontend/angular/angular6/kotlinspringbootangular6crudhttpclientmysqlexamplespringdatajparestapisexample Kotlin Spring Boot +...
loveprogramming viết 2 tháng trước
0 0
{{like_count}}

kipalog

{{ comment_count }}

bình luận

{{liked ? "Đã kipalog" : "Kipalog"}}


Male avatar
{{userFollowed ? 'Following' : 'Follow'}}
545 bài viết.
98 người follow

 Đầu mục bài viết

Vẫn còn nữa! x

Kipalog vẫn còn rất nhiều bài viết hay và chủ đề thú vị chờ bạn khám phá!