我受过培训以发展我的学习项目,但遇到一个例外。 因此,我编写了一个客户端REST调用Web服务。
这是我的web.xml:
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>AirQuality</display-name>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
<servlet>
<servlet-name>servlet sensor</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>airQuality.CUAirQualitySensor</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>servlet sensor</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>
这是我的示例界面:
package airQuality.CUAirQualitySensor;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
@Path("/AQSensor")
@Produces(MediaType.TEXT_PLAIN)
public interface CUManagerSensor {
int getId();
@GET
@Path("/getQuality")
String getQuality();
@GET
@Path("/getStateSensor")
String getSensorState(int idS);
String reduceEnergyConsumption(int id, String action);
}
这是我的客户代码:
package airQUserAgent;
import java.net.URI;
import javax.ws.rs.client.Client;
import javax.ws.rs.client.ClientBuilder;
import javax.ws.rs.client.WebTarget;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.UriBuilder;
import org.glassfish.jersey.client.ClientConfig;
public class UserImp {
int id;
public static void main(String[] args) {
ClientConfig config = new ClientConfig();
Client client = ClientBuilder.newClient(config);//Creating an Instance of a Client
WebTarget target = client.target(getBaseURI());// Creating a WebTarget using the URI of the targeted web resource:
String response = target.path("AQSensor").path("getQuality").request().accept(MediaType.TEXT_PLAIN).get(String.class);
System.out.println(response);
}
private static URI getBaseURI() {
return UriBuilder.fromUri("http://localhost:8083/AirQuality").build();
}
}
在执行此类时,出现以下异常:
Exception in thread "main" javax.ws.rs.NotFoundException: HTTP 404 Introuvable
at org.glassfish.jersey.client.JerseyInvocation.convertToException(JerseyInvocation.java:917)
at org.glassfish.jersey.client.JerseyInvocation.translate(JerseyInvocation.java:770)
at org.glassfish.jersey.client.JerseyInvocation.access$500(JerseyInvocation.java:90)
at org.glassfish.jersey.client.JerseyInvocation$2.call(JerseyInvocation.java:671)
at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
at org.glassfish.jersey.internal.Errors.process(Errors.java:228)
at org.glassfish.jersey.process.internal.RequestScope.runInScope(RequestScope.java:423)
at org.glassfish.jersey.client.JerseyInvocation.invoke(JerseyInvocation.java:667)
at org.glassfish.jersey.client.JerseyInvocation$Builder.method(JerseyInvocation.java:396)
at org.glassfish.jersey.client.JerseyInvocation$Builder.get(JerseyInvocation.java:296)
at airQUserAgent.UserImp.main(UserImp.java:25)
有人想让我知道吗?这个异常的解决方案是什么? 谢谢。
答案 0 :(得分:0)
这是CUManagerSensor的实现
public class CUManagerSensorImp implements CUManagerSensor {
int id;
@Override
public int getId() {
return 12;
}
@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;
}
@Override
public String reduceEnergyConsumption(int id, String action) {
return action;
}
@Override
public String getSensorState(int idS) {
return "On";
}