javarestservletsjersey-clientsca

Exception in thread "main" javax.ws.rs.InternalServerErrorException: HTTP 500 Erreur Interne de Servlet


I have a java client to call a REST web service. If I declare the Path in the java class. All is good, ie: like that:

@Path("/aqsensor")
@Produces(MediaType.TEXT_PLAIN)

public class CUManagerSensorImp {
@GET
@Path("/getQuality")
public String getQuality() {
        String quality;
        double average = getAverage();
        if (isBetween(average, 0, 39))
            quality = "Bonne qualité de l'air";
        else if (isBetween(average, 40, 79))
            quality = "Moyenne qualité de l'air";
        else
            quality = "Mauvaise qualité de l'air";

        return quality;
    }

    private double getAverage() {
        int v = (int) (Math.random() * 125);
        return v;

    }

    private boolean isBetween(double average, int min, int max) {

        if (average >= min && average <= max)
            return true;
        else
            return false;
    }

but if I declare the Path in an interface. like that (interface code):

@Path("/aqsensor")
@Produces(MediaType.TEXT_PLAIN)

public interface CUManagerSensor {
    @GET
    @Path("/getQuality")
    String getQuality();

and this the interface implementation :

public class CUManagerSensorImp implements CUManagerSensor{
@Override
public String getQuality() {
        String quality;
        double average = getAverage();
        if (isBetween(average, 0, 39))
            quality = "Bonne qualité de l'air";
        else if (isBetween(average, 40, 79))
            quality = "Moyenne qualité de l'air";
        else
            quality = "Mauvaise qualité de l'air";

        return quality;
    }

    private double getAverage() {
        int v = (int) (Math.random() * 125);
        return v;

    }

    private boolean isBetween(double average, int min, int max) {

        if (average >= min && average <= max)
            return true;
        else
            return false;
    }
}

I get the following exception:

Exception in thread "main" javax.ws.rs.InternalServerErrorException: HTTP 500 Erreur Interne de Servlet
    at org.glassfish.jersey.client.JerseyInvocation.convertToException(JerseyInvocation.java:1074)
    at org.glassfish.jersey.client.JerseyInvocation.translate(JerseyInvocation.java:859)
    at org.glassfish.jersey.client.JerseyInvocation.lambda$invoke$1(JerseyInvocation.java:743)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:292)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:274)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:205)
    at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:390)
    at org.glassfish.jersey.client.JerseyInvocation.invoke(JerseyInvocation.java:741)
    at org.glassfish.jersey.client.JerseyInvocation$Builder.method(JerseyInvocation.java:404)
    at org.glassfish.jersey.client.JerseyInvocation$Builder.get(JerseyInvocation.java:300)
    at airQUserAgent.UserImp.main(UserImp.java:25)

Where is the problem when I add an interface? Is it forbidden to add interfaces when creating web service rest? Or should you add some instructions to the web.xml file? And thank you in advance


Solution

  • In JSR-339 (Java API for RESTful Web Services 2.0) you can read right at the start of section 3.6 Annotation Inheritance:

    JAX-RS annotations may be used on the methods and method parameters of a super-class or an implemented interface.

    So it's possible to use annotations but only in methods and method parameters, not in the interface itself This is most probably because they'll try to create an instance of the type that is annotated with it.

    So the problem in your case seems to the be following annotations:

    @Path("/aqsensor")
    @Produces(MediaType.TEXT_PLAIN)
    

    Which should kept in the specific class and not the interface. Also the specification in the same section states at the end:

    For consistency with other Java EE specifications, it is recommended to always repeat annotations instead of relying on annotation inheritance.

    So use annotation inheritance at your own risk.