user384729
user384729

Reputation: 403

"Could not find a suitable constructor" when extracted interface from my resource class with Jersey 2

How can I make Jersey understand that it should use a concrete class instead of the interface for a resource?

I had a working app with a Status resource. Then I extracted an interface IStatus, and moved all JAX-RS annotations there. Now, I get:

org.glassfish.hk2.api.MultiException A MultiException has 1 exceptions.  They are:1. java.lang.NoSuchMethodException: Could not find a suitable constructor in resource.IStatus class

I know that this works with RestEasy. Is there any way of making it work with Jersey?

Upvotes: 12

Views: 15271

Answers (3)

94621
94621

Reputation: 89

In my case, I was missing an @Component annotation like in the example below:

@Component // <-- this was missing
@Path("/somepath")
public class MyComponent {

    private OtherComponent otherComponent;

    public MyComponent(OtherComponent otherComponent) {
        this.otherComponent = otherComponent;
    }
}

Upvotes: 1

Robocide
Robocide

Reputation: 6751

To whoever reaches this thread and the above wasn't the issue, in my scenario it was this:

Please pay special attention to constructors annotated with @Inject. It is a common mistake to import com.google.Inject instead of javax.inject.Inject.

From here: https://github.com/eclipse-ee4j/jersey/issues/2390

That solved the issue!

Upvotes: 8

Paul Samsotha
Paul Samsotha

Reputation: 209052

Put the class level @Path on the implementation instead of the interface. Jersey is trying to instantiate the interface, which it can't.

Upvotes: 7

Related Questions