简体   繁体   English

Jersey2客户端抛出javax.ws.rs.NotFoundException

[英]Jersey2 Client throwing javax.ws.rs.NotFoundException

I have written a sample REST service using Jersey2. 我使用Jersey2编写了一个示例REST服务。

Here is my web.xml: 这是我的web.xml:

<web-app>
  <display-name>jerseysample</display-name>
    <servlet>
        <servlet-name>Jersey REST Service</servlet-name>
        <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
        <init-param>
            <param-name>javax.ws.rs.Application</param-name>
            <param-value>com.adaequare.rest.config.JerseyResourceInitializer</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Jersey REST Service</servlet-name>
        <url-pattern>/rest/*</url-pattern>
    </servlet-mapping>
</web-app>

Here is my sample class: 这是我的示例类:

package com.adaequare.resource;

import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

@Path("/hello")
public class Hello {
    @GET
    @Produces(MediaType.TEXT_HTML)
    public String sayHtmlHello(){
        return "<html><title>Hello Jersey</title><body><h1>Hello Jersey</h1></body></html>";
    }

    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public String sayPlainTextHello() {
        return "Hello Jersey";
    }

    // This method is called if XML is request
    @GET
    @Produces(MediaType.TEXT_XML)
    public String sayXMLHello() {
        return "<?xml version=\"1.0\"?>" + "<hello> Hello Jersey" + "</hello>";
    }

}

I have deployed it to Tomcat and am able to access the following URL: 我已将其部署到Tomcat,并且能够访问以下URL:

http://localhost:8080/jerseysample/rest/hello

I tried writing a unit test this way: 我尝试用这种方式编写单元测试:

package com.adaequare.client;

public class MyResourceTest {
    public static final URI BASE_URI = UriBuilder.fromUri("http://localhost").port(8080).build();
    private HttpServer server;
    private WebTarget target;

    @Before
    public void setUp() throws Exception {

        ResourceConfig rc = new ResourceConfig(Hello.class);
        server = GrizzlyHttpServerFactory.createHttpServer(BASE_URI, rc);

        server.start();
        Client c = ClientBuilder.newClient();
        target = c.target(BASE_URI);
    }

    @After
    public void tearDown() throws Exception {
        server.shutdownNow();
    }


    @Test
    public void testGetIt() {
        String responseMsg = target.path("jerseysample").path("rest").path("hello").request().get(String.class);
        System.out.println("I am here");
        assertEquals("Got it!", responseMsg);
    }
}

This class also throws the exception. 这个类也抛出异常。

On executing this class, I am getting the following exception: 在执行此类时,我收到以下异常:

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: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 com.adaequare.client.TestClient.main(TestClient.java:14)

I am sure I am missing some configuration stuff. 我确信我错过了一些配置。 I have browsed to see the root cause of the issue but to no avail. 我曾经浏览过这个问题的根本原因,但无济于事。 Can someone please let me know if I am missing something? 如果我错过了什么,有人可以告诉我吗?

Your service is mapped to (and you are saying you can access it): http://localhost:8080/jerseysample/rest/hello but using your client you are calling http://localhost:8080/restserver/rest/hello which is different URL. 您的服务映射到(并且您说您可以访问它): http://localhost:8080/jerseysample/rest/hello但使用您的客户端调用http://localhost:8080/restserver/rest/hello是不同的URL。 What is the surprise? 有什么惊喜?

Try 尝试

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

As for the second test, try calling getUri() on your WebTarget to see what URL you are actually calling, it should help you see where is the problem. 至于第二个测试,尝试在WebTarget上调用getUri()来查看实际调用的URL,它应该可以帮助您查看问题所在。

After your update: 更新后:

Well first thing is, you haven't specified (in terms of content negotiation) what content your client accepts (you did this in your previous example, which you deleted). 首先,您没有指定(在内容协商方面)您的客户接受哪些内容(您在之前的示例中执行了此操作,您删除了该内容)。 But that should not be a problem since in that case server should send you any of implemented ones since by not specifying it you are stating you are supporting all kind of responses. 但这应该不是问题,因为在这种情况下服务器应该向您发送任何已实现的服务器,因为通过不指定它您说明您支持所有类型的响应。 But the problem probably is putting String.class into get() method. 但问题可能是将String.class放入get()方法。 There should go an entity you want Jersey to transform the response into. 应该让你希望泽西岛的实体将响应转化为。 If you want to get String I would do something like this: 如果你想获得String,我会做这样的事情:

Response response = target.path("jerseysample").path("rest").path("hello").
      request().get();
StringWriter responseCopy = new StringWriter();
IOUtils.copy((InputStream) response.getEntity(), responseCopy);

But you can't tell for sure which one of your three method is going to be called since it is on the same PATH, so you should also specify the content by passing it to request method. 但是你不能确定你的三个方法中哪一个会被调用,因为它在同一个PATH上,所以你也应该通过将它传递给request方法来指定内容。

Hope this helps anyone who can be facing the same problem. 希望这有助于任何可能面临同样问题的人。 In my case, I created my web service RESTful project with the Netbeans Wizard. 就我而言,我使用Netbeans向导创建了我的Web服务RESTful项目。 By any reason, I didn't know why, it missed the ApplicationConfig.java class which contains the annotation @javax.ws.rs.ApplicationPath("webresources"). 出于任何原因,我不知道为什么,它错过了包含注释@javax.ws.rs.ApplicationPath(“webresources”)的ApplicationConfig.java类。 I don't know why when I generated the client it showed me the correct path that I was expecting. 我不知道为什么当我生成客户端时它向我展示了我期待的正确路径。

So, the solution for me was to copy another ApplicationConfig.java from other project and add my facade to the resources. 所以,我的解决方案是从其他项目复制另一个ApplicationConfig.java并将我的外观添加到资源中。

if you don't config web.xml to lookup the rest classes you need use @ApplicationPath to indicate the classes that keep the Rest resources. 如果您没有配置web.xml来查找其余类,则需要使用@ApplicationPath来指示保留Rest资源的类。

@ApplicationPath("/rest")
public class AplicationRest extends Application
{
  @Override
  public Set<Class<?>> getClasses()
  {
      Set<Class<?>> resources = new java.util.HashSet<>();
      resources.add(com.acme.SomeRestService.class);
      return resources;
  }
}

There is an error in web.xml web.xml中存在错误

    <init-param>
        <param-name>javax.ws.rs.Application</param-name>
        <param-value>
            com.adaequare.rest.config.JerseyResourceInitializer
        </param-value>
    </init-param>

please try below 请尝试以下

    <init-param>
        <param-name>javax.ws.rs.Application</param-name>
        <param-value>
            com.adaequare.resource.config.JerseyResourceInitializer
        </param-value>
    </init-param>

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 客户端 REST 抛出 javax.ws.rs.NotFoundException - Client REST throwing javax.ws.rs.NotFoundException 为什么未选中 javax.ws.rs.NotFoundException ? - Why is javax.ws.rs.NotFoundException unchecked? 最新的CXF与Spring:警告:javax.ws.rs.NotFoundException - latest CXF with Spring: WARNING: javax.ws.rs.NotFoundException javax.ws.rs.NotFoundException:找不到完整路径的资源 - javax.ws.rs.NotFoundException: Could not find resource for full path javax.ws.rs.NotFoundException:找不到HTTP 404 angular 1.5 - javax.ws.rs.NotFoundException: HTTP 404 Not Found angular 1.5 javax.ws.rs.NotFoundException:找不到用于junit + jersy测试框架的HTTP 404 - javax.ws.rs.NotFoundException: HTTP 404 Not Found for junit+jersy test framework javax.ws.rs.NotFoundException:无法从http请求中提取参数 - javax.ws.rs.NotFoundException: Unable to extract parameter from http request javax.ws.rs.NotFoundException:无法使用 RESTEasy 和 Wildfly 8.1.0.Final 找到完整路径的资源 - javax.ws.rs.NotFoundException: Could not find resource for full path with RESTEasy and Wildfly 8.1.0.Final 请求后发生“javax.ws.rs.NotFoundException: HTTP 404 Not Found” - “javax.ws.rs.NotFoundException: HTTP 404 Not Found” occurring after request javax.ws.rs.NotFoundException:找不到完整路径的资源:具有RESTEasy,Eclipse Luna和Tomcat 7 - javax.ws.rs.NotFoundException: Could not find resource for full path: with RESTEasy, Eclipse Luna and Tomcat 7
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM