0

How to use Retrofit2 with Spring boot, Kotlin

Intro

Trong bài viết sẽ giới thiệu về cách sử dụng Retrofit trong spring boot kotlin.

Requirement

Spring boot Kotlin setup

Các bạn có thể khởi tạo bằng 2 cách:

  • Thêm dependency vào build.gradle.kts
implementation("com.squareup.retrofit2:retrofit:2.9.0")
implementation("com.squareup.retrofit2:converter-gson:2.9.0")

Code implement

Kotlin

  • Tạo controller với path / để check server đã chạy, nếu nhận được như hình trong trình duyệt là đã chạy.

  • Tạo DAO

  • API interface: Tạo 2 request GET

  • Controller: Thêm 2 routes để xử lý request đến external api resource thông qua service

Golang

  • Model và các hàm trả về thông tin cho handler

  • Handler xử lý request

Kết quả

References


All rights reserved

Viblo
Hãy đăng ký một tài khoản Viblo để nhận được nhiều bài viết thú vị hơn.
Đăng kí