我正在嘗試爲應用程序/ x-www-form-urlencoded和字符串有效負載以及其他應用程序/ json格式與json主體創建2個服務的資源。將ByteArrayOutputStream轉換爲Kotlin中的json
我有這樣的代碼:
@POST @Path("/test")
fun test(@Context request: ContainerRequest): Response {
val baos = ByteArrayOutputStream()
request.entityStream.use { it.copyTo(baos) }
val ipnRawData = baos.toString()
var map : Map<String,Any>
map = when (request.headers.getFirst("Content-Type")) {
"application/json" -> objectMapper.convertValue(ipnRawData,Map::class.java) as Map<String,Any>
"application/x-www-form-urlencoded" -> LinkedHashMap()
else -> throw UnsupportedOperationException()
}
//....handle the map
return Response.status(200).build()
}
但是,當我嘗試使用JSON選項運行它,和身體:{"name" :"test"}
),我得到一個錯誤:
"java.lang.IllegalArgumentException: Can not construct instance of java.util.LinkedHashMap: no String-argument constructor/factory method to deserialize from String value ('{ "name" :"test"}')"
感謝您的幫助,Yoel
感謝您的詳細解答和有用的提示! – Joel