本文介绍了使用Jersey客户端的JAX RS 2获得“找不到HTTP 404但相同的URL在浏览器上工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对JAX RS和Jersey 2还是陌生的.我的简单休息资源服务器端程序"Hello World"运行良好,并且可以通过任何浏览器和google chrome的Advance Rest Client应用程序访问.

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.

但是当我编写了JAX RS Jersey 2客户端时,它给了我HTTP 404未找到,下面是详细信息:

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:

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

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

我的服务器端Jar文件列表

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

在浏览器中工作正常.

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

    }
 }

客户端Jars列表:

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 

但是当我运行此主要方法时,得到如下所示的404感知:

    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.

推荐答案

您应该进一步研究Jersey客户端的API文档( https://jersey.java.net/documentation/latest/user-guide.html#client ).乍一看,您似乎在定义WebTarget时犯了一个错误,即您需要先定义target(Web服务端点),然后再定义resource(要与之交互的特定资源),所以您应该有这样的东西:

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

这也许就是为什么您可以在浏览器上看到结果而不在Java应用程序中看到结果的原因...

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.

这篇关于使用Jersey客户端的JAX RS 2获得“找不到HTTP 404但相同的URL在浏览器上工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-19 22:33