user6850907
user6850907

Reputation:

quarkus-arc Unsatisfied dependency problem

This is my resource class: with repository injection.

@Path("/posts")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public class PostsResource {


    @Context
    UriInfo uriInfo;

    @Inject
    PostRepository posts;

    @GET
    public Response getAllPosts(
        @QueryParam("q") String q,
        @QueryParam("limit") @DefaultValue("10") int limit,
        @QueryParam("offset") @DefaultValue("0") int offset
    ) {
        return Response.ok(this.posts.findByKeyword(q, limit, offset)).build();
    }

    @GET
    @Path("/count")
    public Response getAllPosts(@QueryParam("q") String q) {
        return Response.ok(
            Count.builder().count(this.posts.countByKeyword(q))
        ).build();
    }

    @POST
    public Response savePost(PostForm post) {
        Post entity = Post.builder()
            .title(post.getTitle())
            .content(post.getContent())
            .build();
        Post saved = this.posts.save(entity);
        return Response.created(uriInfo.getBaseUriBuilder().path("/posts/{slug}").build(saved.getSlug())).build();
    }

}

and repository class

public class PostRepository extends AbstractRepository<Post, Long> {

    @Inject
    private EntityManager em;

    @Transactional
    public List<Post> findByKeyword(String keyword, long limit, long offset) {
        return this.stream()
                .filter(p -> Optional.ofNullable(keyword)
                        .map(k -> p.getTitle().contains(k) || p.getContent().contains(k)).orElse(true))
        
                .limit(limit).skip(offset).collect(toList());
    }

    @Transactional
    public long countByKeyword(String keyword) {
        return this.stream().filter(p -> Optional.ofNullable(keyword)
                .map(k -> p.getTitle().contains(k) || p.getContent().contains(k)).orElse(true)).count();
    }

    @Transactional
    public List<Post> findByCreatedBy(String username) {
        Objects.requireNonNull(username, "username can not be null");

        return this.stream().filter(p -> username.equals(p.getCreatedBy().getUsername()))
                .sorted(Post.DEFAULT_COMPARATOR).collect(toList());
    }

    @Transactional
    public Optional<Post> findBySlug(String slug) {
        Objects.requireNonNull(slug, "Slug can not be null");
        return this.stream().filter(p -> p.getSlug().equals(slug)).findFirst();
    }

    public List<Post> findAll() {
        return em.createNamedQuery("Post.findAll", Post.class).getResultList();

    }

    public Post findPostById(Long id) {

        Post post = em.find(Post.class, id);

        if (post == null) {
            throw new WebApplicationException("Post with id of " + id + " does not exist.", 404);
        }
        return post;
    }

    @Transactional
    public void updatePost(Post post) {

        Post postToUpdate = findPostById(post.getId());
        postToUpdate.setTitle(post.getTitle());
        postToUpdate.setContent(post.getContent());
    }

    @Transactional
    public void createPost(Post post) {

        em.persist(post);

    }

    @Transactional
    public void deletePost(Long postId) {

        Post p = findPostById(postId);
        em.remove(p);

    }

    @Override
    protected EntityManager entityManager() {
        return this.em;
    }

}

My pm.xml conatin these dependencies:

<dependencyManagement>
    <dependencies>
        <dependency>
            <groupId>io.quarkus</groupId>
            <artifactId>quarkus-bom</artifactId>
            <version>${quarkus.version}</version>
            <type>pom</type>
            <scope>import</scope>
        </dependency>
    </dependencies>
</dependencyManagement>
<dependencies>
    <!-- <dependency>
        <groupId>io.quarkus</groupId>
        <artifactId>quarkus-arc</artifactId>
        <scope>provided</scope>
    </dependency> -->
    <dependency>
        <groupId>io.quarkus</groupId>
        <artifactId>quarkus-resteasy</artifactId>
    </dependency>
    <dependency>
        <groupId>io.quarkus</groupId>
        <artifactId>quarkus-resteasy-jsonb</artifactId>
    </dependency>
    <dependency>
        <groupId>io.rest-assured</groupId>
        <artifactId>rest-assured</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>io.quarkus</groupId>
        <artifactId>quarkus-rest-client</artifactId>
    </dependency>
    <dependency>
        <groupId>io.jsonwebtoken</groupId>
        <artifactId>jjwt-api</artifactId>
        <version>0.10.7</version>
    </dependency>
    <dependency>
        <groupId>io.jsonwebtoken</groupId>
        <artifactId>jjwt-impl</artifactId>
        <version>0.10.7</version>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>io.jsonwebtoken</groupId>
        <artifactId>jjwt-jackson</artifactId>
        <version>0.10.7</version>
        <scope>runtime</scope>
    </dependency>
    <dependency>
        <groupId>com.nimbusds</groupId>
        <artifactId>nimbus-jose-jwt</artifactId>
        <version>${nimbus-jose.version}</version>
    </dependency>
    <dependency>
        <groupId>io.quarkus</groupId>
        <artifactId>quarkus-junit5</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.junit.jupiter</groupId>
        <artifactId>junit-jupiter-engine</artifactId>
    </dependency>
<!--    <dependency>
        <groupId>org.junit.platform</groupId>
        <artifactId>junit-platform-launcher</artifactId>
    </dependency> -->
    <dependency>
        <groupId>org.mockito</groupId>
        <artifactId>mockito-junit-jupiter</artifactId>
        <version>${mockito.version}</version>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.assertj</groupId>
        <artifactId>assertj-core</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>io.quarkus</groupId>
        <artifactId>quarkus-jdbc-postgresql</artifactId>
    </dependency>
    <!-- https://mvnrepository.com/artifact/io.quarkus/quarkus-jdbc-h2 -->
    <dependency>
        <groupId>io.quarkus</groupId>
        <artifactId>quarkus-jdbc-h2</artifactId>
    </dependency>
    <!-- https://mvnrepository.com/artifact/io.quarkus/quarkus-jdbc-h2 -->
    <dependency>
        <groupId>io.quarkus</groupId>
        <artifactId>quarkus-test-h2</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>io.quarkus</groupId>
        <artifactId>quarkus-hibernate-orm-panache</artifactId>
    </dependency>
     <dependency>
     <groupId>io.quarkus</groupId>
     <artifactId>quarkus-hibernate-orm</artifactId>
   </dependency>
    <dependency>
        <groupId>io.quarkus</groupId>
        <artifactId>quarkus-security</artifactId>
    </dependency>
    <dependency>
        <groupId>io.quarkus</groupId>
        <artifactId>quarkus-smallrye-openapi</artifactId>
    </dependency>
    <dependency>
        <groupId>io.quarkus</groupId>
        <artifactId>quarkus-smallrye-metrics</artifactId>
    </dependency>
    <dependency>
        <groupId>io.quarkus</groupId>
        <artifactId>quarkus-smallrye-health</artifactId>
    </dependency>
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>1.18.4</version>
    </dependency>
    <dependency>
        <groupId>io.quarkus</groupId>
        <artifactId>quarkus-spring-web</artifactId>
    </dependency>
    <!-- <dependency>
        <groupId>io.quarkus</groupId>
        <artifactId>quarkus-undertow</artifactId>
    </dependency> -->
    <dependency>
        <groupId>io.quarkus</groupId>
        <artifactId>quarkus-spring-di</artifactId>
    </dependency>
    <!-- <dependency>
        <groupId>io.vertx</groupId>
        <artifactId>vertx-web-client</artifactId>
    </dependency> -->
</dependencies>  

I cannot build this beacause the build return :

Unsatisfied dependency for type com.ciwara.kalanSowApp.repository.PostRepository and qualifiers [@Default]

       - java member: com.ciwara.kalanSowApp.rest.post.PostsResource#posts

       - declared on CLASS bean [types=[com.ciwara.kalanSowApp.rest.post.PostsResource], qualifiers=[@Default, @Any], target=com.ciwara.kalanSowApp.rest.post.PostsResource]

Upvotes: 7

Views: 28001

Answers (2)

PANTELIS
PANTELIS

Reputation: 1

Above the Injected client also add @RestClient and to client class @RegisterRestClient, @ApplicationScoped. Like this

@Path("/posts")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
@RegisterRestClient
@ApplicationScoped
public class PostsResource {}

//Where you inject.
@Inject
@RestClient
PostRepository posts;

Upvotes: 0

Turing85
Turing85

Reputation: 20195

In order to make PostRepository injectable, it must be a bean. The most common way to declare a class as bean is to add a Bean-Scope annotation to the class.

In the given class, adding @ApplicationScoped seems sensible.

Upvotes: 15

Related Questions