【问题标题】:Kotlin: Map collection to json array with JacksonKotlin:使用 Jackson 将集合映射到 json 数组
【发布时间】:2019-12-10 06:53:13
【问题描述】:

Kotlin 新手,我正在尝试使用 kotlin spring webserver 模板设置一个 web 服务器

这里我想将 kotlin 集合转换为 JSON 对象数组

名为test的示例端点

@PostMapping(value = [ "/test"],produces = [ "application/json" ],headers = [ "Content-Type=application/json" ])
    fun test(@RequestBody request:IOUDATA): ResponseEntity<String>{

      //need this as JSON array of object
      val numbersMap = mapOf("key1" to 1, "key2" to 2, "key3" to 3, "key4" to 1)

      return ResponseEntity.status(HttpStatus.CREATED).body("${numbersMap}");

}

预期结果

 {
   result:[
     {"key1":1},{"key2":2}...
    ]
   }

【问题讨论】:

    标签: json spring-mvc kotlin jackson


    【解决方案1】:

    这里的问题是,您的控制器被声明为返回 String,而您不让 Jackson 创建它,而是依赖默认的 toString 实现(由 Kotlin 的字符串插值机制调用)。

    为了让 Jackson 序列化对象,您有两种不同的方式:

    1. 让控制器返回一个ResponseEntity&lt;String&gt; 并手动将您的对象序列化为一个字符串:

      @RestController
      class MyController(private val objectMapper: ObjectMapper) {
      
          @PostMapping(value = ["/test"], produces = ["application/json"], headers = ["Content-Type=application/json"])
          fun test(@RequestBody request: MyInputData): ResponseEntity<String> {
      
              val numbersMap = mapOf(
                  "result" to listOf(
                      mapOf("key1" to 1),
                      mapOf("key2" to 2),
                      mapOf("key3" to 3),
                      mapOf("key4" to 1)
                  )
              )
              val mySerialisedString = objectMapper.writeValueAsString(numbersMap)
      
              return ResponseEntity.status(HttpStatus.CREATED).body(mySerialisedString) // Jackson will serialize it automatically
      
          }
      }
      

      请注意,在这种情况下,您需要获取 ObjectMapper 的实例(由 Spring 自动装配)。

    2. 创建您自己的结果对象,Jackson 将自动对其进行序列化(在后台由 Spring 调用):

      @RestController
      class MyController {
      
          @PostMapping(value = ["/test"], produces = ["application/json"], headers = ["Content-Type=application/json"])
          fun test(@RequestBody request: MyInputData): ResponseEntity<MyOutputData> {
      
              val numbersMap = listOf(mapOf("key1" to 1), mapOf("key2" to 2), mapOf("key3" to 3), mapOf("key4" to 1))
      
              val myOutputData = MyOutputData(numbersMap)
              return ResponseEntity.status(HttpStatus.CREATED).body(myOutputData) // Jackson will serialize it automatically
      
          }
      }
      
      class MyOutputData(val result: List<Map<String, Int>>)
      

    两种情况在调用时都返回以下 JSON 字符串:

    {
        "result": [
        {
            "key1": 1
        },
        {
            "key2": 2
        },
        {
            "key3": 3
        },
        {
            "key4": 1
        }
        ]
    }
    

    【讨论】:

      猜你喜欢
      • 2019-08-10
      • 2023-03-30
      • 1970-01-01
      • 2013-07-27
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-04-12
      • 1970-01-01
      相关资源
      最近更新 更多