当前位置: 首页 > 知识库问答 >
问题:

使用Panache存储库方法的Quarkus Reactive Restease

楚修为
2023-03-14

我正在使用静态编程语言在Quarkus中创建一个应用程序。现在我也在使用Reactive Restease创建一个通用资源。现在Quarkus的留档在使用Panache Hibernate时没有使用存储库方法。我的问题是,这仍然是可能的吗?每当我尝试它时,它似乎都不起作用。我的IDE中出现了这个错误:

下列函数都不能用提供的参数调用。坚持(T!)定义在io . quar kus . hibernate . reactive . panache . panache repository persist(T!,vararg T!)在io . quar kus . hibernate . reactive . panache . panache repository persist中定义(流

这是我的代码:

package com.fortuneapp.backend.application.rest

import com.fortuneapp.backend.application.domain.core.models.entities.Fruit
import io.quarkus.hibernate.reactive.panache.Panache
import io.quarkus.hibernate.reactive.panache.PanacheRepository
import io.quarkus.hibernate.reactive.panache.PanacheRepositoryBase
import io.smallrye.mutiny.Uni
import java.net.URI
import java.util.*
import javax.transaction.Transactional
import javax.validation.Valid
import javax.ws.rs.*
import javax.ws.rs.core.MediaType
import javax.ws.rs.core.Response

@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.APPLICATION_JSON)
open class GenericResource<T>(
    private var repository: PanacheRepository<T>,
    ) {

    @GET
    fun getAll(): Uni<MutableList<T>> = repository.listAll()

    @POST
    @Transactional
    fun add(@Valid resource: T): Uni<Response> = Panache.withTransaction<T>(repository.persist(resource))
        .onItem().transform { id ->
            Response.created(
                URI.create(
                    "/fruits/$id"
                )
            ).build()
        }

    @GET
    @Path("/{id}")
    fun get(@PathParam("id") id: UUID): Uni<T> = repository.findById(id)

    @POST
    @Transactional
    @Path("/{id}")
    fun update(
        @PathParam("id")
        id: UUID,
        @Valid
        resource: T
    ): Uni<T>? = repository.persist(resource)

    @DELETE
    @Transactional
    @Path("/{id}")
    fun delete(@PathParam("id") id: UUID): Uni<Long>? = repository.deleteById(id)

}

private fun <Entity, Id> PanacheRepositoryBase<Entity, Id>.deleteById(id: UUID): Uni<Long>? =
    delete("id", id)

private fun <Entity, Id> PanacheRepositoryBase<Entity, Id>.findById(id: UUID): Uni<Entity> =
    find("id", id).firstResult()

对于@GET和其他动词,我可以使用存储库方法,但不能使用@POST。我还没有实现update方法。

非常感谢一位名为geoand的用户,他帮助我解决了之前关于这个主题的问题。

以下是我目前遇到的错误:

[ERROR] Failed to execute goal org.jetbrains.kotlin:kotlin-maven-plugin:1.5.21:compile (compile) on project fortuneapp-backend: Compilation failure
[ERROR] /Users/ricardo/Documents/code/quarkus/fortuneapp-backend/src/main/kotlin/com/fortuneapp/backend/application/rest/GenericResource.kt:[26,77] Type mismatch: inferred type is Uni<T!>! but Supplier<Uni<TypeVariable(T)!>!>! was expected
[ERROR] 
[ERROR] -> [Help 1]
org.apache.maven.lifecycle.LifecycleExecutionException: Failed to execute goal org.jetbrains.kotlin:kotlin-maven-plugin:1.5.21:compile (compile) on project fortuneapp-backend: Compilation failure
/Users/ricardo/Documents/code/quarkus/fortuneapp-backend/src/main/kotlin/com/fortuneapp/backend/application/rest/GenericResource.kt:[26,77] Type mismatch: inferred type is Uni<T!>! but Supplier<Uni<TypeVariable(T)!>!>! was expected

    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:215)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:156)
    at org.apache.maven.lifecycle.internal.MojoExecutor.execute (MojoExecutor.java:148)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:117)
    at org.apache.maven.lifecycle.internal.LifecycleModuleBuilder.buildProject (LifecycleModuleBuilder.java:81)
    at org.apache.maven.lifecycle.internal.builder.singlethreaded.SingleThreadedBuilder.build (SingleThreadedBuilder.java:56)
    at org.apache.maven.lifecycle.internal.LifecycleStarter.execute (LifecycleStarter.java:128)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:305)
    at org.apache.maven.DefaultMaven.doExecute (DefaultMaven.java:192)
    at org.apache.maven.DefaultMaven.execute (DefaultMaven.java:105)
    at org.apache.maven.cli.MavenCli.execute (MavenCli.java:957)
    at org.apache.maven.cli.MavenCli.doMain (MavenCli.java:289)
    at org.apache.maven.cli.MavenCli.main (MavenCli.java:193)
    at jdk.internal.reflect.NativeMethodAccessorImpl.invoke0 (Native Method)
    at jdk.internal.reflect.NativeMethodAccessorImpl.invoke (NativeMethodAccessorImpl.java:62)
    at jdk.internal.reflect.DelegatingMethodAccessorImpl.invoke (DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke (Method.java:566)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launchEnhanced (Launcher.java:282)
    at org.codehaus.plexus.classworlds.launcher.Launcher.launch (Launcher.java:225)
    at org.codehaus.plexus.classworlds.launcher.Launcher.mainWithExitCode (Launcher.java:406)
    at org.codehaus.plexus.classworlds.launcher.Launcher.main (Launcher.java:347)
Caused by: org.jetbrains.kotlin.maven.KotlinCompilationFailureException: Compilation failure
/Users/ricardo/Documents/code/quarkus/fortuneapp-backend/src/main/kotlin/com/fortuneapp/backend/application/rest/GenericResource.kt:[26,77] Type mismatch: inferred type is Uni<T!>! but Supplier<Uni<TypeVariable(T)!>!>! was expected

共有1个答案

孙书
2023-03-14

您需要将资源传递给persist方法repository.persist(资源)

 类似资料:
  • 使用laravel 7/livewire应用程序,我使用Repository制作crud,并获得了数据列表,在装载事件中,我分配了受保护的var$FacilityRepository,它在render方法中正常工作, 但在编辑方法中为空,我得到错误: 当用户单击“编辑链接”时 在模板中,编辑链接定义为: 为什么会出现错误以及如何修复? 修改#2: > 类设施扩展组件{...公共$FacilityR

  • 我的应用程序中有一个Book model类,它如下所示: 返回NULL。为什么?

  • 我正在尝试实现一个简单的REST服务,该服务基于具有Spring启动和Spring数据Rest的JPA存储库。(请参阅此教程)如果将以下代码与 gradle 一起使用,则运行良好: 为了让事情变得更简单,我使用Spring boot CLI(“Spring run”命令)尝试了相同的代码。 不幸的是,这似乎不起作用@RepositoryRestResource似乎无法像@RestControlle

  • 我正在使用100个实体(使用JHipster)设置一个新的Spring Boot API,我的问题是:鉴于我有一组存储库层方法,我希望我的所有存储库都能够调用这些方法。 我已经尝试制作所有接口来扩展('RepositoryQuery'是我默认的自定义接口名称后缀),然后使用特定于实体的类。请注意,所有的类扩展了一个泛型实现类,名为。 请注意,给定正则表达式中的“.*”代表我的持久实体集中的任何实体

  • Quarkus 1.8.3。最终的 直接调用访问PanacherRepository的方法可以按预期工作,但是当通过EventBus调用同一方法时,调用到达该方法并执行每一行,直到它到达任何存储库调用,然后在没有任何发生的指示的情况下无声地失败/退出。 根据日志,直接调用在Quarkus主线程中执行,事件总线调用在vert中执行。x-eventloop-thread-2。 还尝试了以下步骤的组合,

  • 下面的方法由控制器调用,其中是必需的参数,、是可选参数。我必须编写多个方法来根据这些参数获取记录。我想知道有没有一个更好的/有效的方法来重构这个方法,用if/else梯子?