Reputation: 197
I'm experimenting with Micronaut and Kotlin.
I have a Hibernate JPA connection and it works pretty well, however, while creating a REST API for it, I wanted to exclude some properties from the listing serialization.
Therefore, here's what I did:
Domain class:
@Entity
@JsonView
class User {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
@NotNull
@JsonView(View1::class)
var id : Long? = null
@NotNull
@Column(unique = true, length = 64)
@JsonView(View1::class)
lateinit var email : String
@NotNull
@Column(length = 16)
@JsonView(View2::class)
lateinit var firstName : String
@NotNull
@Column(length = 16)
@JsonView(View2::class)
lateinit var lastName : String
@NotNull
@JsonView(View2::class)
lateinit var address : Address
}
@Embeddable
class Address {
@Column(length = 64)
lateinit var street : String
@Column(length = 32)
lateinit var city : String
@Column(length = 16)
lateinit var state : String
}
View1 and View2 are not interesting, as they are literally empty classes as per the documentation.
Now, in the controller, I do something like:
@Get("/")
@Produces("application/json")
@JsonView(View1::class)
fun list() : List<User> {
return userRepository.findAll()
}
Expecting some cooperation, however, this is what I got:
[
{
"id": 1,
"email": "[email protected]",
"firstName": "John",
"lastName": "Doe",
"address": {
"street": "23, Madison Street",
"city": "Brooklyn",
"state": "NY"
}
}
]
In the application.yml, json-view should be properly set as in:
jackson:
json-view:
enabled: true
Any clue?
Upvotes: 1
Views: 665
Reputation: 31
After having the same problem I eventually stumbled onto this answer which was using the ObjectMapper directly: Jackson's @JsonView annotation does not work.
Problem was that the jackson.mapper.default-view-inclusion
property has a default value of true. Explicitly setting it to false in application.yml solved the issue for me.
jackson:
serializationInclusion: ALWAYS
json-view:
enabled: true
mapper:
default-view-inclusion: false
Upvotes: 1