我正在 Kotlin 和 Quarkus 中创建一个应用程序。我现在正在创建一个通用的休息资源,它将由我的其他资源类扩展。我的想法是我将不得不编写更少的代码,因为泛型类的所有方法都将被其子类使用。现在我遇到的问题是传递一个通用的 PanacheRepository。
GenericResource 中的所有方法都使用 PanacheRepository 方法。但是,当我尝试通过构造函数将例如 UserRepository 传递给 GenericResource 时,它不起作用。
下面是我的通用资源:
class GenericResource(
val repository: PanacheRepository<Any>
) {
@GET
@Produces(MediaType.APPLICATION_JSON)
fun findAll() : FindAllResponse =
try {
FindAllSuccess(repository.listAll())
} catch (e: Exception) {
FindAllFailure(e)
}
@POST
@Transactional
fun add(
@Valid user: User
) : AddResponse =
try {
repository.persist(user)
AddSuccess(user)
} catch (e: Exception) {
AddFailure(e)
}
@GET
@Path("/{userId}")
fun findById(
@PathParam("userId")
userId : UUID
) : FindResponse =
try {
FindSuccess(
repository.find("id", userId).firstResult()
)
} catch (e: NotFoundException) {
FindFailure(e)
}
@PATCH
@Transactional
@Path("/{userId}")
fun update(
@PathParam("userId")
userId : UUID,
user: User
) : UpdateResponse =
try {
user.id?.let { user.fullName?.let { it1 ->
user.email?.let { it2 ->
repository.update("fullName = ?1, email = ?2 where id = ?3",
it1, it2, it)
}
} }
UpdateSuccess(user)
} catch (e: Exception) {
UpdateFailure(e)
}
@DELETE
@Transactional
@Path("/{userId}")
fun delete(
@PathParam("userId")
userId : UUID
) : DeleteResponse =
try {
repository.delete("id", userId)
DeleteSuccess("User deleted.")
} catch (e: Exception) {
DeleteFailure(e)
}
}
这将是我的 UserResource:
@Path("/users")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
class UserRes(user: User) : GenericResource(user) {
}
我还有一个单独的 UserRepository 是这样的:
@ApplicationScoped
class UserRepository : PanacheRepository<User>
有人能把我推向正确的方向吗?对 Kotlin 还是很陌生 :)