Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
1.6k views
in Technique[技术] by (71.8m points)

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

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 !!!

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...