ใน Spring framework 6 (snapshot/RC) นั้น
มีการเพิ่มเติมและปลี่ยนแปลงหลาย ๆ อย่าง
หนึ่งในนั้นคือ HTTP interface
สำหรับการเรียกใช้งาน external service ผ่าน HTTP protocol
ซึ่งก่อนนี้จะมี

  • RestTemplate สำหรับ request-response model
  • WebClient สำหรับ WebFlux
  • ใช้ Spring Cloud OpenFeign และ Retrofit + OkHttp

โดยการเรียกใช้งาน external service เช่น REST API นั้น
จะให้เราพัฒนาเชิง declarative
ด้วยการสร้าง interface และ annotation ต่าง ๆ

ตัวอย่างเรียกใช้งาน Get user by id จาก JsonPlaceholder

จะเขียน interface ได้ดังนี้

ต่อมาทำการสร้าง Bean สำหรับสร้าง HTTP Client
ด้วย WebClient จาก Spring WebFlux

ทำการเรียกใช้งานแบบง่าย ๆ

จะเห็นได้ว่า เป็นแนวทางเดียวกับ Spring Cloud OpenFegint และ Retrofit นั่นเอง

ดู VDO เพิ่มเติมได้