【问题标题】:jax-rs @Provider doesn't pick up correctly when jax-rs runs as Spring Boot app当 jax-rs 作为 Spring Boot 应用程序运行时,jax-rs @Provider 无法正确接收
【发布时间】:2021-11-11 10:50:51
【问题描述】:

我已将 JAX-RS 与 Spring Boot 应用程序集成。

我想使用 LocalDate 作为查询参数。

端点示例如下:

@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("book/publication/year")
override fun findInRange(
    @QueryParam("from") fromDate: LocalDate?,
    @QueryParam("to") toDate: LocalDate?
): Response

如果我使用 String 一切正常,那么 LocalDate 类型本身的问题。

我找到了一篇来自 Sebastian Daschner 的文章:https://blog.sebastian-daschner.com/entries/jaxrs-convert-params

我现在有这些课程:

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
    }
}

什么都没有改变。问题是如何让 Spring Boot 在 Spring Boot 中运行时拾取自定义的 ParameterConverterProvider?

【问题讨论】:

标签: spring-boot kotlin jax-rs


【解决方案1】:

由于我集成了 Spring Boot + JAX-RS,我需要注册 ResourceConfig 中的所有 JAX-RS 类或从它继承的类。

register(LocalDateParamConverterProvider::class.java)

这篇文章对找到它很有帮助:https://dzone.com/articles/using-jax-rs-with-spring-boot-instead-of-mvc

【讨论】:

    猜你喜欢
    • 2015-08-13
    • 2016-12-06
    • 2023-01-27
    • 1970-01-01
    • 2012-05-19
    • 1970-01-01
    • 2019-05-28
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多