简体   繁体   中英

Client REST throwing javax.ws.rs.NotFoundException

I am trained to develop my end of study project, but I meet an exception. Therefore, I have written a client REST invoke a web service.

Here is my 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>

Here is my sample interface:

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

Here is my client code:

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

}

On executing this class, I am getting the following exception:

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)

Can someone please let me know if I am missing something? Waht's the solution of this exception? Thanks.

Here is the implementation of 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";
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM