package com.wh.rest
import io.vertx.core.AbstractVerticle
import io.vertx.core.http.HttpServerResponse
import io.vertx.ext.web.Router
import io.vertx.ext.web.RoutingContext
import io.vertx.ext.web.handler.BodyHandler
import io.vertx.kotlin.core.json.array
import io.vertx.kotlin.core.json.json
import io.vertx.kotlin.core.json.obj
/**
* @Author hw83770
* @Date 16:09 2019/8/8
*
*/
class SimpleREST : AbstractVerticle() {
override fun start() {
initData()
val router = Router.router(vertx)
router.route().handler(BodyHandler.create())
router.get("/products/:productID").handler { handleGetProduct(it) }
router.put("/products/:productID").handler { handleAddProduct(it) }
router.get("/products").handler { handleListProducts(it) }
vertx.createHttpServer().requestHandler(router).listen(8080) { res ->
if (res.failed()) {
res.cause().printStackTrace()
} else {
System.out.println("Server listening at: http://localhost:8080/")
}
}
}
var products = mutableMapOf<String, Any?>()
fun addProduct(product: io.vertx.core.json.JsonObject) {
products[product.getString("id")] = product
}
private fun initData() {
addProduct(json {
obj(
"id" to "prod3568",
"name" to "Egg Whisk",
"price" to 3.99,
"weight" to 150
)
})
addProduct(json {
obj(
"id" to "prod7340",
"name" to "Tea Cosy",
"price" to 5.99,
"weight" to 100
)
})
addProduct(json {
obj(
"id" to "prod8643",
"name" to "Spatula",
"price" to 1.0,
"weight" to 80
)
})
}
fun handleAddProduct(ctx: RoutingContext) {
val productID = ctx.request().getParam("productID")
val response = ctx.response()
if (productID == null) {
sendError(400, response)
} else {
val product = ctx.bodyAsJson
if (product == null) {
sendError(400, response)
} else {
products[productID] = product
response.end()
}
}
}
fun handleListProducts(ctx: RoutingContext) {
val arr = json {
array()
}
for (v in products.values) {
arr.add(v)
}
ctx.response().putHeader("content-type", "application/json").end(arr.toString())
}
fun handleGetProduct(ctx: RoutingContext) {
val productID: String? = ctx.request().getParam("productID")
val response = ctx.response()
if (null == productID) {
sendError(400, response)
} else {
val product = products[productID]
if (product == null) {
sendError(404, response)
} else {
response.putHeader("content-type", "application/json").end(product.toString())
}
}
}
fun sendError(statusCode: Int, response: HttpServerResponse) {
response.setStatusCode(statusCode).end()
}
}
build.gradle
buildscript {
repositories {
maven {
url 'https://www.artifactrepository.citigroup.net:443/artifactory/maven-icg-dev'
credentials {
username 'ocean-devops'
password 'APBQW78wqY4oewwXFBtTfcN17ZG'
}
}
// mavenCentral()
}
dependencies {
classpath 'org.jetbrains.kotlin:kotlin-gradle-plugin:1.3.0'
// classpath 'gradle.plugin.io.vertx:vertx-gradle-plugin:0.8.0'
}
}
// new version of build script
//plugins {
// id "io.vertx.vertx-plugin" version "0.8.0"
// id "org.jetbrains.kotlin.jvm" version "1.3.0"
//}
apply plugin: 'kotlin'
apply plugin: 'java'
apply from: 'http://oceanfw.hub.nam.nsroot.net/file/oceanfw/build/build-common.gradle'
version = '0.0.1-SNAPSHOT'
group = 'com.wh'
ext {
vertx_version = '3.7.0'
slf4j_version = '1.7.22'
}
dependencies {
compile 'org.jetbrains.kotlin:kotlin-stdlib-jdk8'
compile 'org.jetbrains.kotlin:kotlin-reflect:1.3.0'
compile "org.slf4j:slf4j-jdk14:${slf4j_version}"
compile "io.vertx:vertx-core:${vertx_version}"
compile "io.vertx:vertx-web:${vertx_version}"
compile "io.vertx:vertx-unit:${vertx_version}"
compile "io.vertx:vertx-auth-shiro:${vertx_version}"
compile "io.vertx:vertx-web-templ-thymeleaf:${vertx_version}"
compile "io.vertx:vertx-lang-kotlin:${vertx_version}"
}
compileKotlin {
kotlinOptions.jvmTarget = '1.8'
}
compileTestKotlin {
kotlinOptions.jvmTarget = '1.8'
}
//vertx {
// mainVerticle = "com.wh.vertx_startup.MainVerticle"
// vertxVersion = "3.8.0"
//}
tasks.withType(org.jetbrains.kotlin.gradle.tasks.KotlinCompile).all {
kotlinOptions {
jvmTarget = "1.8"
}
}
网友评论