Reputation: 51
I've been playing with server side Kotlin, Spring MVC and Jackson.
I built a simple application using http://start.spring.io/, but I might have made a mistake in the JsonView
annotation.
This:
@RestController
class MyRestController {
@RequestMapping("/user")
@JsonView(User::class)
fun getUser() : User = User("Fred",50)
}
data class User(val name: String, val age: Int)
...when called with curl
ph@sleek ~ $ curl -X GET http://localhost:8080/user; echo
{}
ph@sleek ~ $
...the result is {}
when I expected {"name":"Fred","age":50}
. Is there something I did wrong?
Upvotes: 2
Views: 1212
Reputation: 121
if you do want to use @JsonView
(which is necessary in many scenarios), I was running into the same empty object problem until I added the jackson-module-kotlin
dependency to my project.
compile 'com.fasterxml.jackson.module:jackson-module-kotlin'
See here: https://stackoverflow.com/a/48019143/5258628
Upvotes: 1
Reputation: 51
Much simpler than I thought. After finding that Jackson was able to serialise the object just fine, I started making adjustments, and found the default was better than configuration:
class MyRestController {
@RequestMapping("/user")
fun getUser() : User = User("Fred",50)
}
Perfect:
ph@sleek ~ $ curl -X GET http://localhost:8080/user; echo
{"name":"Fred","age":50}
Upvotes: 1