I'm tryin to expose some data class as JSON objects but something ain't working.
I have the following data classes:
data class Link(
@JsonProperty("rel")
@JsonView(View.Bind::class)
val rel: String,
@JsonProperty("method")
@JsonView(View.Bind::class)
val method: HttpMethod,
@JsonProperty("href")
@JsonView(View.Bind::class)
val href: String)
data class MetaData(val status: HttpStatus) {
@JsonView(View.Bind::class)
@JsonProperty("status_code")
fun getStatusCode(): Int {
return status.value()
}
@JsonView(View.Bind::class)
@JsonProperty("status_desc")
fun getStatusDesc(): String {
return status.name
}
}
data class Payload(
@JsonView(View.Bind::class)
@JsonProperty("payload")
val payload: Any,
@JsonProperty("_meta")
@JsonView(View.Bind::class)
val metaData: MetaData,
@JsonProperty("_links")
@JsonView(View.Bind::class)
val links: List<Link>)
And for some reason, when the Payload class is a JAVA class everything works fine, but when it is a kotlin class only the payload element gets into the JSON.
For dependency i'm using:
<dependency>
<groupId>org.jetbrains.kotlin</groupId>
<artifactId>kotlin-stdlib</artifactId>
<version>1.0.1-1</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.module</groupId>
<artifactId>jackson-module-kotlin</artifactId>
<version>2.7.1-2</version>
</dependency>
If i change the "_meta" and "_links" to "meta" and "links" the "links" elements are rendered.
I had to use the following combination of dependencies
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.7.3</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.7.3</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.7.3</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.module</groupId>
<artifactId>jackson-module-kotlin</artifactId>
<version>2.7.3</version>
</dependency>
The update form jackson-module-kotlin
from 2.7.1-2 to 2.7.3 had little to no diference in the problem, but i was obligated to include the jackson-core
, jackson-annotations
, and jackson-databind
.
Aparently the jackson jar included in the jackson-module-kotlin
had some issues spring-webmvc
that prevented me from registering the kotlin module as shown below
<mvc:annotation-driven>
<mvc:message-converters register-defaults="true">
<bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<property name="objectMapper" ref="kotlinMapper" />
</bean>
</mvc:message-converters>
</mvc:annotation-driven>
<bean id="kotlinMapper"
class="org.springframework.http.converter.json.Jackson2ObjectMapperFactoryBean">
<property name="featuresToEnable">
<array>
<util:constant static-field="com.fasterxml.jackson.databind.SerializationFeature.INDENT_OUTPUT" />
<util:constant static-field="com.fasterxml.jackson.databind.SerializationFeature.WRITE_EMPTY_JSON_ARRAYS" />
</array>
</property>
<property name="modulesToInstall" value="com.fasterxml.jackson.module.kotlin.KotlinModule" />
</bean>
In the end those _
in the @JsonProperty
worked just fine.