JAX RS 2 using Jersey client getting " HTTP 404 Not found " but same URL is working on browser

24,305

Solution 1

You should further study the API documentation for Jersey client (https://jersey.java.net/documentation/latest/user-guide.html#client). In a first glance you seem to be making a mistake on defining the WebTarget, namely you need to define the target (web service endpoint) and then the resource (specific resource you want to interact with), so you should have something like this:

Client client = ClientBuilder.newClient();
WebTarget target = client
    .target("http://localhost:8080/ProJrsRi_HelloRest2/rest").path("hello");

Probably this is the reason why you can see the results on browser but not in your Java application...

You can also find some very good examples of code in the documentation.

HTH.

Solution 2

Perhaps there is a server-side issue: I used the JdkHttpServerFactory:

ResourceConfig rc = new ResourceConfig(TestResource.class);
HttpServer server = JdkHttpServerFactory.createHttpServer(
  new URI("http://0.0.0.0:8080/"), rc);
System.out.println("Drücken Sie Enter, um den Server zu beenden!");
System.in.read();
server.stop(0);

First, I used http://localhost:8080/ as the URI for the server. The browser showed the resource without problems, the Jersey Client always returned Not Found, although I could determine the URI used by the Jersey Client in Debug mode and paste it into the browser (which worked).

I then used Live HTTP Headers to check the working request in the browser and found the following host header:

Host: localhost:8080

Then I used TCPmon to check my jersey request. The host header looked the following way:

Host: 127.0.0.1:8080

The problem was that the server is bound to localhost and the client issues requests to 127.0.0.1, even if you use localhost in the WebTarget.

Therefore I changed the server URI from

http://localhost:8080/

to

http://0.0.0.0:8080/

to bind the server to all addresses.

Hope that helps. Maybe your issue is similar!

Share:
24,305
Admin
Author by

Admin

Updated on June 17, 2020

Comments

  • Admin
    Admin almost 4 years

    I am new to JAX RS and Jersey 2. My simple rest resource server side program "Hello World" is working fine and I am able to access it by any browser and Advance Rest Client application of google chrome.

    But when I have written a JAX RS Jersey 2 client it is giving me HTTP 404 Not found, Below are the details:

    Below is rest resource server side program:

    Web.xml:

    http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" id="WebApp_ID" version="3.0"> ProJrsRi_HelloRest2

    <servlet>
        <servlet-name>jersey</servlet-name>
        <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
        <init-param>
            <param-name>com.sun.jersey.config.property.packages</param-name>
            <param-value>com</param-value>
        </init-param>
    </servlet>
    <servlet-mapping>
        <servlet-name>jersey</servlet-name>
        <url-pattern>/rest/*</url-pattern>
    </servlet-mapping>
    


    Resource Class

    package     com;
    import     javax.ws.rs.GET;
    import     javax.ws.rs.Path;
    
    
    @Path("hello")
    public class HelloRest {
    
        @GET()
        public  String sayHello(){
        return "Welcome to the world of REST";
        }
    }
    

    My Server side Jar files list

    asm-3.1.jar
    jackson-core-asl-1.9.2.jar
    jackson-jaxrs-1.9.2.jar
    jackson-mapper-asl-1.9.2.jar
    jackson-xc-1.9.2.jar
    jersey-client-1.18.jar
    jersey-core-1.18.jar
    jersey-json-1.18.jar
    jersey-server-1.18.jar
    jersey-servlet-1.18.jar
    jettison-1.1.jar
    

    It is working fine in browser.

    Here is Client Side:

     package userCoreJava;
    
     import java.net.URI;
     import java.net.URISyntaxException;
    
     import javax.ws.rs.client.Client;
     import javax.ws.rs.client.ClientBuilder;
     import javax.ws.rs.client.Invocation.Builder;
     import javax.ws.rs.client.WebTarget;
     import javax.ws.rs.core.Response;
    
    
    
     public class UsingJersey {
        public static void main(String[] args) {
            Client client = ClientBuilder.newClient();
            WebTarget target=null;
            try {
                target = client.target(new URI("http://localhost:8080/ProJrsRi_HelloRest2/rest/hello"));
            } catch (URISyntaxException e) {
                e.printStackTrace();
            }
            Builder builder =   target.request();
            //Response response  = builder.get();
            String result  = builder.get(String.class);
            System.out.println(target.getUri().toString());
            System.out.println("Result=" + result);
    
        }
     }
    

    Client side Jars list:

    asm-all-repackaged-2.2.0-b21.jar
    cglib-2.2.0-b21.jar
    guava-14.0.1.jar
    hk2-api-2.2.0-b21.jar
    hk2-locator-2.2.0-b21.jar
    hk2-utils-2.2.0-b21.jar
    javax.annotation-api-1.2.jar
    javax.inject-2.2.0-b21.jar
    javax.servlet-api-3.0.1.jar
    javax.ws.rs-api-2.0.jar
    jaxb-api-2.2.7.jar
    jersey-client.jar
    jersey-common.jar
    jersey-container-servlet-core.jar
    jersey-container-servlet.jar
    jersey-server.jar
    org.osgi.core-4.2.0.jar
    osgi-resource-locator-1.0.1.jar
    persistence-api-1.0.jar
    validation-api-1.1.0.Final.jar 
    

    But when I run this main method getting 404 exeception as follow:

        Exception in thread "main" javax.ws.rs.NotFoundException: HTTP 404 Not found
        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:422)
        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 userCoreJava.UsingJersey.main(UsingJersey.java:25)
    

    Please Help me to fix this issue, I stuck in it.

    Thanks in advance !!!