我是JAX RS和Jersey 2的新手。我的简单休息资源服务器端程序" Hello World"工作正常,我可以通过google chrome的任何浏览器和Advance Rest Client应用程序访问它。
但是当我写了一个JAX RS Jersey 2客户端时,它给了我HTTP 404 Not found,下面是详细信息:
以下是休息资源服务器端程序:
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" ID =" WebApp_ID"版本=" 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";
}
}
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);
}
}
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
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)
请帮我解决这个问题,我坚持了下来。
答案 0 :(得分:5)
您应该进一步研究Jersey客户端(https://jersey.java.net/documentation/latest/user-guide.html#client)的API文档。乍一看,您似乎在定义WebTarget
时出错,即您需要定义target
(Web服务端点),然后定义resource
(您想要的特定资源)与...互动,所以你应该有这样的东西:
Client client = ClientBuilder.newClient();
WebTarget target = client
.target("http://localhost:8080/ProJrsRi_HelloRest2/rest").path("hello");
这可能就是为什么你可以在浏览器上看到结果而不是在Java应用程序中看到结果......
您还可以在文档中找到一些非常好的代码示例。
HTH。
答案 1 :(得分:1)
也许存在服务器端问题: 我使用了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);
首先,我使用http://localhost:8080/
作为服务器的URI。浏览器显示资源没有问题,Jersey客户端总是返回Not Found,虽然我可以确定Jersey客户端在调试模式下使用的URI并将其粘贴到浏览器中(有效)。
然后我使用Live HTTP Headers在浏览器中检查工作请求,并找到以下主机头:
Host: localhost:8080
然后我使用TCPmon检查我的球衣请求。主机头看起来如下:
Host: 127.0.0.1:8080
问题是服务器绑定到localhost,客户端向127.0.0.1发出请求,即使您在WebTarget中使用localhost也是如此。
因此我从
更改了服务器URIhttp://localhost:8080/
到
http://0.0.0.0:8080/
将服务器绑定到所有地址。
希望有所帮助。也许你的问题很相似!