postgresql 运行具有postgres DB集成的Quarkus微服务(基于Java)时,出现javax.enterprise.inject.UnsatisfiedResolutionException

0ejtzxu1  于 2023-04-05  发布在  PostgreSQL
关注(0)|答案(1)|浏览(138)

我是Quarkus框架的新手,我被下面的错误卡住了。任何建议都会对问题在哪里非常有帮助。

错误:[error]: Build step io.quarkus.arc.deployment.ArcProcessor#validate threw an exception: javax.enterprise.inject.spi.DeploymentException: javax.enterprise.inject.UnsatisfiedResolutionException: Unsatisfied dependency for type org.abhishek.dao.PersonRepository and qualifiers [@Default] - java member: org.abhishek.resource.PersonResource#repository - declared on CLASS bean [types=[org.abhishek.resource.PersonResource, java.lang.Object], qualifiers=[@Default, @Any], target=org.abhishek.resource.PersonResource]
实体类:

@Entity
@Table(name = "persons")
public class PersonEntity {

    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    private Long id;

    @Column(name = "name")
    private String name;

    @Column(name = "age")
    private int age;

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }
}

仓库接口:

public interface PersonRepository extends PanacheRepository<PersonEntity> {
}

资源类

@ApplicationScoped
@Path("/persons")
public class PersonResource {

    @Inject
    PersonRepository repository;

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public List<PersonEntity> list() {
        return repository.listAll();
    }

    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    public void add(PersonEntity person) {
        repository.persist(person);
    }
}

我正在尝试运行我的应用程序并连接到我在端口5432上本地运行的Postgres DB。
应用程序应该能够连接到数据库并创建persons表,我将能够命中配置的API。但得到上述错误,我无法解决。

fhg3lkii

fhg3lkii1#

通过添加实现PanacheRepository接口的repository类,然后将其注入到repository类中,解决了我的问题。

@ApplicationScoped
public class PersonRepository implements PanacheRepository<Person> {

   //can also add any custom logic methods based on the requirements

}

注入Resource/controller类:

public class PersonResource {
    @Inject
    PersonRepository repository;
.........
.......

}

相关问题