示例代码我想要的:
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) 我正在使用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)