首页 > 解决方案 > 有什么方法可以在 init 块或其他函数中执行 jax-rs 方法?

问题描述

目前,我使用一种架构通过 jax 使用 @get、@post 等方法创建 cruds

@Path("/users")
@Produces(APPLICATION_JSON)
@Consumes(APPLICATION_JSON)
class UsersService @Inject constructor(
    val users: Users,
    userDTOMapper: UserDTOMapper
) : CrudService<User, UserDTO, UserDTO>(
    repository = users,
    mapper = userDTOMapper
)

但是,我想删除这种继承并使用聚合以获得更好的灵活性。可以有这样的东西

@Path("/users")
@Produces(APPLICATION_JSON)
@Consumes(APPLICATION_JSON)
class UsersService @Inject constructor(
    val users: Users,
    userDTOMapper: UserDTOMapper
)  {
    init {
        withReadOnlyOperations()

//        @GET
//        fun anotherTestMethod() : String{
//            return "Here JAX-RS can't find the method ;/"
//        }
    }
}

fun withReadOnlyOperations() {
    @GET
    fun readOnlyOperationTest() : String{
        return "hey"
    }
}

有什么方法可以使用其他函数,或者在我手动定义的上下文中初始化路由?

我知道你可以使用接口,但是我仍然会生成一个我必须为使用这个接口的每个实体实现的 boiderplate 代码,已经有了继承/聚合模型,我已经有了定义好的方法并准备好使用。

标签: kotlinservletsjax-rsinit

解决方案


推荐阅读