Issue
I have JAX-RS integrated with the Spring Boot app.
I want to use LocalDate as a query parameter.
The example of endpoint is below:
@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("book/publication/year")
override fun findInRange(
@QueryParam("from") fromDate: LocalDate?,
@QueryParam("to") toDate: LocalDate?
): Response
If I use String everything works fine, so the issue with LocalDate type itself.
I found an article from Sebastian Daschner: https://blog.sebastian-daschner.com/entries/jaxrs-convert-params
I have now these classes:
LocalDateConverter.kt:
class LocalDateConverter : ParamConverter<LocalDate?> {
override fun fromString(value: String?): LocalDate? {
return if (value == null) null else LocalDate.parse(value)
}
override fun toString(value: LocalDate?): String? {
return value?.toString()
}
}
LocalDateParamConverterProvider.kt:
@Provider
public class LocalDateParamConverterProvider : ParamConverterProvider {
override fun <T> getConverter(
rawType: Class<T>,
genericType: Type,
annotations: Array<Annotation>
): ParamConverter<T>? {
return if (rawType == LocalDate::class.java) LocalDateConverter() as ParamConverter<T> else null
}
}
Nothing has changed. The question is how to make Spring Boot pick up custom ParameterConverterProvider when run in Spring Boot?
Solution
As I have Spring Boot + JAX-RS integrated, I need to register for all the JAX-RS classes in ResourceConfig
or classes inherited from it.
register(LocalDateParamConverterProvider::class.java)
This article helped a lot to find it out: https://dzone.com/articles/using-jax-rs-with-spring-boot-instead-of-mvc
Answered By - Dmytro Chasovskyi
Answer Checked By - Marilyn (JavaFixing Volunteer)