SlideShare a Scribd company logo
(Shengyou Fan)
GDG Taichung
2021/04/17
Cloud Functions
Kotlin Serverless API
- Ch.3 JSON API -
Photo by Jean-Philippe Delberghe on Unsplash
—
• JSON Response
• Data Class
• JSON Serialization
—
?query=param
①
②
③
⑤
Client
FaaS
Local Development
Kotlin Functions
④
HTTP Request
—
GET http://localhost:8080/getfeed?limit={n}
Accept: application/json
GET http://localhost:8080/findfeed?id={id}
Accept: application/json
1. n
2. JSON
1. id
2. JSON
class Handler: HttpFunction {
@Throws(IOException::class)
override fun service(req: HttpRequest, res: HttpResponse) {
if ("GET" != request.method.toUpperCase()) {
with(response) {
setStatusCode(HttpURLConnection.HTTP_BAD_METHOD)
writer.write("Bad method")
}
return
}
}
}
HTTP Method
—
class Handler: HttpFunction {
@Throws(IOException::class)
override fun service(req: HttpRequest, res: HttpResponse) {
with(res) {
setStatusCode(HttpURLConnection.HTTP_OK)
setContentType("application/json")
writer.write("...JSON STING...")
}
}
}
JSON
—
• HTTP Status Code
• Content-Type
•
HTTP Response
—
{
"data": [
{
"id": ...,
"title": "...",
"author": "...",
"content": "...",
"coverUrl": "...",
"permalink": "...",
"publishedAt": "..."
},
// ...
]
}
1. n
2. JSON
3. data
Data Class
—
// Data Class
data class News(
val id: String,
val title: String,
val author: String,
val content: String,
val coverUrl: String?
val permalink: String,
val publishedAt: String,
)
// JSON
{
"id": 1,
"title": "...",
"author": "...",
"content": "...",
"coverUrl": "...",
"permalink": "...",
"publishedAt": “..."
}
• JSON Data Class
// build.gradle
plugins {
id("...kotlin.plugin.serialization") version "..."
}
dependencies {
implementation("...:kotlinx-serialization-json:$ver")
}
• Gradle Plugin
Serialization
—
•
// News
@Serializable
data class News(
// ...
)
Annotation
—
val news = (1..10).map { id ->
News(
// ...
)
}
—
Json.encodeToString(
mapOf("data" to news)
)
JSON
—
JSON
• List Map data
• Map JSON
class Handler: HttpFunction {
@Throws(IOException::class)
override fun service(req: HttpRequest, res: HttpResponse) {
val limit = request.queryParameters["limit"]?
.first()?
.toIntOrNull() ?: 10
}
}
Query
—
• HttpRequest Query
•
Java Faker
—
• Java Faker
// build.gradle
dependencies {
implementation("com.github.javafaker:javafaker:$ver")
}
val faker = Faker.instance(Locale.TRADITIONAL_CHINESE)
News(
id = ...,
title = faker.lorem().sentence(),
author = faker.name().fullName(),
content = faker.lorem().paragraph(),
coverUrl = "https://${faker.internet().url()}",
permalink = "https://${faker.internet().url()}",
publishedAt = ...,
)
—
• Faker

More Related Content

What's hot (20)

PDF
運用 Exposed 管理及操作資料庫
Shengyou Fan
 
PDF
用 OPENRNDR 將 Chatbot 訊息視覺化
Shengyou Fan
 
PDF
Composer 經典食譜
Shengyou Fan
 
PDF
以 Laravel 經驗開發 Hyperf 應用
Shengyou Fan
 
PDF
Kotlin 讀書會 #1
Shengyou Fan
 
PDF
IVS CTO Night And Day 2018 Winter - [re:Cap] Serverless & Mobile
Amazon Web Services Japan
 
PDF
Retrofit
Amin Cheloh
 
PDF
Kotlin 讀書會第三梯次第一章
Shengyou Fan
 
PDF
From Java to Kotlin - The first month in practice
StefanTomm
 
PDF
Serverless Ballerina
Ballerina
 
PDF
Nordic APIs - Automatic Testing of (RESTful) API Documentation
Rouven Weßling
 
KEY
Background Jobs with Resque
homanj
 
PDF
Android Libs - Retrofit
Daniel Costa Gimenes
 
PDF
Building an API with Django and Django REST Framework
Christopher Foresman
 
PDF
[PHP 也有 Day] 垃圾留言守城記 - 用 Laravel 阻擋 SPAM 留言的奮鬥史
Shengyou Fan
 
PDF
Altitude NY 2018: Leveraging Log Streaming to Build the Best Dashboards, Ever
Fastly
 
PDF
Workshop Consul .- Service Discovery & Failure Detection
Vincent Composieux
 
PDF
Cachopo - Scalable Stateful Services - Madrid Elixir Meetup
Abel Muíño
 
PDF
Fast C++ Web Servers
Troy Miles
 
KEY
GPerf Using Jesque
ctoestreich
 
運用 Exposed 管理及操作資料庫
Shengyou Fan
 
用 OPENRNDR 將 Chatbot 訊息視覺化
Shengyou Fan
 
Composer 經典食譜
Shengyou Fan
 
以 Laravel 經驗開發 Hyperf 應用
Shengyou Fan
 
Kotlin 讀書會 #1
Shengyou Fan
 
IVS CTO Night And Day 2018 Winter - [re:Cap] Serverless & Mobile
Amazon Web Services Japan
 
Retrofit
Amin Cheloh
 
Kotlin 讀書會第三梯次第一章
Shengyou Fan
 
From Java to Kotlin - The first month in practice
StefanTomm
 
Serverless Ballerina
Ballerina
 
Nordic APIs - Automatic Testing of (RESTful) API Documentation
Rouven Weßling
 
Background Jobs with Resque
homanj
 
Android Libs - Retrofit
Daniel Costa Gimenes
 
Building an API with Django and Django REST Framework
Christopher Foresman
 
[PHP 也有 Day] 垃圾留言守城記 - 用 Laravel 阻擋 SPAM 留言的奮鬥史
Shengyou Fan
 
Altitude NY 2018: Leveraging Log Streaming to Build the Best Dashboards, Ever
Fastly
 
Workshop Consul .- Service Discovery & Failure Detection
Vincent Composieux
 
Cachopo - Scalable Stateful Services - Madrid Elixir Meetup
Abel Muíño
 
Fast C++ Web Servers
Troy Miles
 
GPerf Using Jesque
ctoestreich
 

More from Shengyou Fan (20)

PDF
[JCConf 2024] Kotlin/Wasm:為 Kotlin 多平台帶來更多可能性
Shengyou Fan
 
PDF
[GDG Kaohsiung DevFest 2023] 以 Compose 及 Kotlin Multiplatform 打造多平台應用程式
Shengyou Fan
 
PDF
[JCConf 2023] 從 Kotlin Multiplatform 到 Compose Multiplatform:在多平台間輕鬆共用業務邏輯與 U...
Shengyou Fan
 
PDF
[Kotlin 讀書會第五梯次] 深入淺出 Kotlin 第一章導讀
Shengyou Fan
 
PDF
[WebConf Taiwan 2023] 一份 Zend Engine 外帶!透過 Micro 讓一次打包、多處運行變得可能
Shengyou Fan
 
PDF
How I make a podcast website using serverless technology in 2023
Shengyou Fan
 
PDF
[Effective Kotlin 讀書會] 第八章 Efficient collection processing 導讀
Shengyou Fan
 
PDF
[MOPCON 2022] 以 Kotlin Multiplatform 制霸全平台
Shengyou Fan
 
PDF
[JCConf 2022] Compose for Desktop - 開發桌面軟體的新選擇
Shengyou Fan
 
PDF
Using the Exposed SQL Framework to Manage Your Database
Shengyou Fan
 
PDF
[COSCUP 2022] 讓黑畫面再次偉大 - 用 PHP 寫 CLI 工具
Shengyou Fan
 
PDF
[COSCUP 2022] Kotlin Collection 遊樂園
Shengyou Fan
 
PDF
初探 Kotlin Multiplatform
Shengyou Fan
 
PDF
簡化 JVM 上雲 - 透過 Azure Spring Cloud 提升開發、發佈及服務監控效率
Shengyou Fan
 
PDF
[PHP 也有 Day #64] PHP 升級指南
Shengyou Fan
 
PDF
以 Kotlin Multiplatform Mobile (KMM) 開發跨平台行動應用
Shengyou Fan
 
PDF
老派浪漫:用 Kotlin 寫 Command Line 工具
Shengyou Fan
 
PDF
[Kotlin Serverless 工作坊] 單元 2 - 簡介 Kotlin Serverless
Shengyou Fan
 
PDF
[Kotlin Serverless 工作坊] 單元 1 - 開發環境建置
Shengyou Fan
 
PDF
用 Kotlin 打造讀書會小幫手
Shengyou Fan
 
[JCConf 2024] Kotlin/Wasm:為 Kotlin 多平台帶來更多可能性
Shengyou Fan
 
[GDG Kaohsiung DevFest 2023] 以 Compose 及 Kotlin Multiplatform 打造多平台應用程式
Shengyou Fan
 
[JCConf 2023] 從 Kotlin Multiplatform 到 Compose Multiplatform:在多平台間輕鬆共用業務邏輯與 U...
Shengyou Fan
 
[Kotlin 讀書會第五梯次] 深入淺出 Kotlin 第一章導讀
Shengyou Fan
 
[WebConf Taiwan 2023] 一份 Zend Engine 外帶!透過 Micro 讓一次打包、多處運行變得可能
Shengyou Fan
 
How I make a podcast website using serverless technology in 2023
Shengyou Fan
 
[Effective Kotlin 讀書會] 第八章 Efficient collection processing 導讀
Shengyou Fan
 
[MOPCON 2022] 以 Kotlin Multiplatform 制霸全平台
Shengyou Fan
 
[JCConf 2022] Compose for Desktop - 開發桌面軟體的新選擇
Shengyou Fan
 
Using the Exposed SQL Framework to Manage Your Database
Shengyou Fan
 
[COSCUP 2022] 讓黑畫面再次偉大 - 用 PHP 寫 CLI 工具
Shengyou Fan
 
[COSCUP 2022] Kotlin Collection 遊樂園
Shengyou Fan
 
初探 Kotlin Multiplatform
Shengyou Fan
 
簡化 JVM 上雲 - 透過 Azure Spring Cloud 提升開發、發佈及服務監控效率
Shengyou Fan
 
[PHP 也有 Day #64] PHP 升級指南
Shengyou Fan
 
以 Kotlin Multiplatform Mobile (KMM) 開發跨平台行動應用
Shengyou Fan
 
老派浪漫:用 Kotlin 寫 Command Line 工具
Shengyou Fan
 
[Kotlin Serverless 工作坊] 單元 2 - 簡介 Kotlin Serverless
Shengyou Fan
 
[Kotlin Serverless 工作坊] 單元 1 - 開發環境建置
Shengyou Fan
 
用 Kotlin 打造讀書會小幫手
Shengyou Fan
 
Ad

Recently uploaded (20)

PPTX
Engineering the Java Web Application (MVC)
abhishekoza1981
 
PPTX
Writing Better Code - Helping Developers make Decisions.pptx
Lorraine Steyn
 
PDF
iTop VPN With Crack Lifetime Activation Key-CODE
utfefguu
 
PPTX
A Complete Guide to Salesforce SMS Integrations Build Scalable Messaging With...
360 SMS APP
 
PDF
Streamline Contractor Lifecycle- TECH EHS Solution
TECH EHS Solution
 
PDF
Revenue streams of the Wazirx clone script.pdf
aaronjeffray
 
PPTX
Feb 2021 Cohesity first pitch presentation.pptx
enginsayin1
 
PDF
유니티에서 Burst Compiler+ThreadedJobs+SIMD 적용사례
Seongdae Kim
 
PDF
Beyond Binaries: Understanding Diversity and Allyship in a Global Workplace -...
Imma Valls Bernaus
 
PDF
GetOnCRM Speeds Up Agentforce 3 Deployment for Enterprise AI Wins.pdf
GetOnCRM Solutions
 
PDF
Salesforce CRM Services.VALiNTRY360
VALiNTRY360
 
PDF
Alarm in Android-Scheduling Timed Tasks Using AlarmManager in Android.pdf
Nabin Dhakal
 
PPTX
Comprehensive Guide: Shoviv Exchange to Office 365 Migration Tool 2025
Shoviv Software
 
PPTX
Agentic Automation Journey Session 1/5: Context Grounding and Autopilot for E...
klpathrudu
 
PPTX
Human Resources Information System (HRIS)
Amity University, Patna
 
PPTX
3uTools Full Crack Free Version Download [Latest] 2025
muhammadgurbazkhan
 
PPTX
MailsDaddy Outlook OST to PST converter.pptx
abhishekdutt366
 
PPTX
An Introduction to ZAP by Checkmarx - Official Version
Simon Bennetts
 
PPTX
Why Businesses Are Switching to Open Source Alternatives to Crystal Reports.pptx
Varsha Nayak
 
PPTX
Equipment Management Software BIS Safety UK.pptx
BIS Safety Software
 
Engineering the Java Web Application (MVC)
abhishekoza1981
 
Writing Better Code - Helping Developers make Decisions.pptx
Lorraine Steyn
 
iTop VPN With Crack Lifetime Activation Key-CODE
utfefguu
 
A Complete Guide to Salesforce SMS Integrations Build Scalable Messaging With...
360 SMS APP
 
Streamline Contractor Lifecycle- TECH EHS Solution
TECH EHS Solution
 
Revenue streams of the Wazirx clone script.pdf
aaronjeffray
 
Feb 2021 Cohesity first pitch presentation.pptx
enginsayin1
 
유니티에서 Burst Compiler+ThreadedJobs+SIMD 적용사례
Seongdae Kim
 
Beyond Binaries: Understanding Diversity and Allyship in a Global Workplace -...
Imma Valls Bernaus
 
GetOnCRM Speeds Up Agentforce 3 Deployment for Enterprise AI Wins.pdf
GetOnCRM Solutions
 
Salesforce CRM Services.VALiNTRY360
VALiNTRY360
 
Alarm in Android-Scheduling Timed Tasks Using AlarmManager in Android.pdf
Nabin Dhakal
 
Comprehensive Guide: Shoviv Exchange to Office 365 Migration Tool 2025
Shoviv Software
 
Agentic Automation Journey Session 1/5: Context Grounding and Autopilot for E...
klpathrudu
 
Human Resources Information System (HRIS)
Amity University, Patna
 
3uTools Full Crack Free Version Download [Latest] 2025
muhammadgurbazkhan
 
MailsDaddy Outlook OST to PST converter.pptx
abhishekdutt366
 
An Introduction to ZAP by Checkmarx - Official Version
Simon Bennetts
 
Why Businesses Are Switching to Open Source Alternatives to Crystal Reports.pptx
Varsha Nayak
 
Equipment Management Software BIS Safety UK.pptx
BIS Safety Software
 
Ad

[Kotlin Serverless 工作坊] 單元 3 - 實作 JSON API