I'm building a simple Spring Boot service with Kotlin. I have an endpoint
@GetMapping("/summary")
fun getLoginReport(params: SummaryRequest): ResponseEntity<LoginSummaryResponse> {
// not important
}
The SummaryRequest
class is defined like
data class SummaryRequest(
val groupBy: List<SummaryGroupBy>
)
SummaryGroupBy
is an enum class. If I make a request passing a single groupBy parameter (/summary?groupBy=region
), the data class object is built successfully. But if I pass several groupBy (/summary/groupBy=region,date
), I get the following error:
[Field error in object 'SummaryRequest' on field 'groupBy': rejected value [region,date]; codes [typeMismatch.SummaryRequest.groupBy,typeMismatch.groupBy,typeMismatch.java.util.List,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [SummaryRequest.groupBy,groupBy]; arguments []; default message [groupBy]]; default message [Failed to convert value of type 'java.lang.String[]' to required type 'java.util.List'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [api.domain.SummaryGroupBy] for value 'region,date'; nested exception is java.lang.IllegalArgumentException: No enum constant api.domain.SummaryGroupBy.REGION,DATE]]
But, the funny part is it works if I change SummaryRequest
to a regular class (like below). So, I'm puzzled about why it doesn't work with data classes.
class SummaryRequest {
var groupBy: List<SummaryGroupBy> = ArrayList
}
I have jackson-module-kotlin
as a dependency.
Why Jackson cannot deserialize a list in a data class?
Is there a workaround?