线程 "main" javax.ws.rs.InternalServerErrorException 中的异常:HTTP 500 Erreur Interne de Servlet

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

我有一个 java 客户端来调用 REST Web 服务。 如果我在 java class 中声明路径。一切都很好,即:像那样:

@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;
    }

但是如果我在接口中声明路径。像那样(接口代码):

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

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

这是接口实现:

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;
    }
}

我得到以下异常:

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(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)

我添加接口的时候是哪里出了问题?创建web服务rest时是否禁止添加接口?还是应该向 web.xml 文件添加一些说明? 提前谢谢你

JSR-339(Java API for RESTful Web Services 2.0)中,您可以直接阅读 3.6 注释继承:

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

因此可以使用注释,但只能在方法和方法参数中使用,而不是在接口本身中。这很可能是因为他们会尝试创建一个用它注释的类型的实例。

所以你的问题似乎是以下注释:

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

哪些应该保存在特定的 class 而不是界面中。 此外,同一部分中的规范最后指出:

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

因此使用注释继承需要您自担风险。