相关疑难解决方法(0)

如何使用jackson反序列化到Kotlin集合

示例代码我想要的:

data class D(val a: String, val b: Int)
val jsonStr = """[{"a": "value1", "b": 1}, {"a": "value2", "b":"2}]"""
// what I need
val listOfD: List<D> = jacksonObjectMapper().whatMethodAndParameter?
Run Code Online (Sandbox Code Playgroud)

json jackson kotlin

17
推荐指数
2
解决办法
2万
查看次数

Spring Boot REST服务:JSON反序列化不起作用

我正在使用spring-boot和Kotlin开发REST服务.(我应该提到这是我第一次使用它们.)我无法使用此代码让Jackson从POST请求中反序列化JSON:

@RequestMapping("cloudservice/login/{uuid}", method = arrayOf(RequestMethod.POST))
fun login(@PathVariable(value="uuid")uuid: String, @RequestBody user: CloudServiceUser ) : ResponseEntity<CloudServiceUser> {

    val cloudServiceFactory : Class<CloudServiceFactory> = cloudServiceRepository.cloudServiceExtensions[UUID.fromString(uuid)] ?: throw InvalidPathVariableException("Invalid UUID.")
    var token : String
    try {
        token = cloudServiceFactory.newInstance().authenticationService.login(user.userId, user.password)
    } catch (e:Exception ){
        throw CloudServiceException(e.message)
    }

    return ResponseEntity(CloudServiceUser(userId=user.userId, password = "", token = token),HttpStatus.OK)
}
Run Code Online (Sandbox Code Playgroud)

用户对象很简单:

data class CloudServiceUser(val userId: String, val password:String, val token:String)
Run Code Online (Sandbox Code Playgroud)

我收到了错误

org.springframework.http.converter.HttpMessageNotReadableException: Could not read document: Can not construct instance of com.irotsoma.cloudbackenc.cloudservice.CloudServiceUser: no suitable constructor found, can not deserialize …
Run Code Online (Sandbox Code Playgroud)

rest jackson kotlin spring-boot

6
推荐指数
1
解决办法
6719
查看次数

标签 统计

jackson ×2

kotlin ×2

json ×1

rest ×1

spring-boot ×1