Wicia
Wicia

Reputation: 565

NoSuchMethodException: Could not find a suitable constructor

I have created simple REST endpoint API interface using JAX-RS annotations:

package pl.webservice.cards;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

@Path("/cards")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public interface CardsServiceApi {

    @GET
    @Path("/message")
    public String getMessage();
}

And its implementation:

package pl.webservice.cards;

public class CardsService implements CardsServiceApi{

    @Override
    public String getMessage() {
        return "Hello World!";
    }
}

After sending request in POSTMAN I receive following response:

java.lang.NoSuchMethodException: Could not find a suitable constructor in pl.webservice.cards.CardsServiceApi class.

What is interesting when I "merge" both classes in non-interface class everything works fine. Why?

Upvotes: 2

Views: 5271

Answers (1)

Claus Radloff
Claus Radloff

Reputation: 363

I think, the problem is, that the REST-Annotations are in an interface. The server tries to instantiate the interface, which is not possible. If you move the annotations to the implementation class, erverything should work.

Upvotes: 1

Related Questions