Reputation: 1778
I am trying to use Jackson to serialize the same DTO object in 2 different ways, depending on the @JsonView
.
I want to use 2 different names for the same field. In one case I want to name the json property myField
(just like the class field name), in the other I want it to be named myInternalApiField
.
As a result I would like to see outcomes similar to the presented below:
Usage 1 (External API View):
{
"myField": "value1",
"myOtherField": "otherValue"
}
Usage 2 (Internal API View):
{
"myInternalApiField": "value1",
"myOtherField": "otherValue"
}
In my implementation in Java to achieve that I used the combination of custom getters, setters and @JsonView
annotation as below:
public class CustomDTO {
@JsonView(Views.ExternalApiView)
private String myField;
// Other fields here
@JsonView(Views.InternalApiView)
public String getMyInternalApiField() { return myField; }
@JsonView(Views.InternalApiView)
public void setMyInternalApiField(String value) { this.myField = value; }
@JsonView(Views.ExternalApiView)
public String getMyField() { return myField; }
@JsonView(Views.ExternalApiView)
public void setMyField(String value) { this.myField = value }
}
However I don't know how to properly achieve the same result in Kotlin.
I was thinking about using something like:
data class CustomDTO(
@get:[JsonView(Views.ExternalApiView) JsonProperty("myField")]
@get:[JsonView(Views.InternalApiView) JsonProperty("myInternalApiField")]
@set:[JsonView(Views.InternalApiView) JsonProperty("myField")]
@set:[JsonView(Views.InternalApiView) JsonProperty("myInternalApiField")]
var myField: String,
val myOtherField: String,
val myDifferentField: String
)
But this is not allowed in Kotlin.
Do you have any suggestions how to utilize the @JsonView
in Kotlin in the similar way as I did it in Java?
Upvotes: 1
Views: 3166
Reputation: 5637
How about something like:
data class CustomDTO(
@JsonView(ExternalApiView::class)
var myField: String,
val myOtherField: String,
val myDifferentField: String
) {
val myExternalField: String
@JsonView(InternalApiView::class)
get() {
return myField
}
}
It looks like there are ways that don't require creating computed properties in the DTO, like:
But these have their own complexity, even if that complexity isn't in the DTO class. I'm not sure these are much more appealing to me but you could see if they appeal to you.
Upvotes: 1