我有一个 Java 客户端来调用 REST Web 服务。如果我在 java 类中声明路径。一切都很好,即:像这样:
@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$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)
添加接口时问题出在哪里?创建web service rest时是否禁止添加接口?还是应该在 web.xml 文件中添加一些说明?并提前谢谢你