Guest User

Untitled

a guest
Jul 20th, 2018
82
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.08 KB | None | 0 0
  1. data class PurchaseOrder(val buyer: String, val seller: String,
  2. val poNumber: String, val date: String,
  3. val vendorReference: String)
  4.  
  5. install(ContentNegotiation) {
  6. gson {
  7. setDateFormat(DateFormat.LONG)
  8. setPrettyPrinting()
  9. }
  10.  
  11.  
  12. post("/purchaseOrder"){
  13. val po = call.receive<PurchaseOrder>()
  14. println("purchase order: ${po.toString()}")
  15. call.respondText("post received", contentType =
  16. ContentType.Text.Plain)
  17.  
  18. {
  19. "PurchaseOrder" : {
  20. "buyer": "buyer a",
  21. "seller": "seller A",
  22. "poNumber": "PO1234",
  23. "date": "27-Jun-2018",
  24. "vendorReference": "Ref1234"
  25. }
  26. }
  27.  
  28. purchase order: PurchaseOrder(buyer=null, seller=null, poNumber=null,
  29. date=null, vendorReference=null)
  30.  
  31. val channel = call.request.receiveChannel()
  32. val ba = ByteArray(channel.availableForRead)
  33. channel.readFully(ba)
  34. val s = ba.toString(Charset.defaultCharset())
  35.  
  36. println(s) // prints JSON
  37.  
  38. val gson = Gson()
  39. val po = gson.fromJson(s, PurchaseOrder::class.java)
  40.  
  41. println("buyer = ${po.buyer}" //prints null
Add Comment
Please, Sign In to add comment