RESTEasy - javax.ws.rs.NotFoundException:找不到完整路径的资源

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19882126/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-12 21:00:43  来源:igfitidea点击:

RESTEasy - javax.ws.rs.NotFoundException: Could not find resource for full path

javagwtjax-rsresteasy

提问by Kaer

I've tried to implement a REST service with RESTEasy in a GWT project, but when I get into the respective URI the application returns:

我尝试在 GWT 项目中使用 RESTEasy 实现 REST 服务,但是当我进入相应的 URI 时,应用程序返回:

Grave: failed to execute
javax.ws.rs.NotFoundException: Could not find resource for full path: http://127.0.0.1:8888/api/matches
    at org.jboss.resteasy.core.registry.ClassNode.match(ClassNode.java:73)
    at org.jboss.resteasy.core.registry.RootClassNode.match(RootClassNode.java:48)
    at org.jboss.resteasy.core.ResourceMethodRegistry.getResourceInvoker(ResourceMethodRegistry.java:444)
    at org.jboss.resteasy.core.SynchronousDispatcher.getInvoker(SynchronousDispatcher.java:234)
    at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:171)
    at org.jboss.resteasy.plugins.server.servlet.ServletContainerDispatcher.service(ServletContainerDispatcher.java:220)
    at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:56)
    at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:51)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)

My web.xml is:

我的 web.xml 是:

<?xml version="1.0" encoding="UTF-8" standalone="no"?><web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" version="2.5" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee                http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

  <!-- All REST resources will be prefixed by /api -->
  <context-param>
    <param-name>resteasy.servlet.mapping.prefix</param-name>
    <param-value>/api</param-value>
  </context-param>

  <!-- Servlets -->
  <servlet>
    <servlet-name>Resteasy</servlet-name>
    <servlet-class>org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher</servlet-class>
    <init-param>
        <param-name>javax.ws.rs.Application</param-name>
        <param-value>eii.api.MatchApplication</param-value>
    </init-param>
  </servlet>

  <!-- Servlet mappings -->
  <!-- All calls to /api/xxx will be sent to the reasy servlet -->
  <servlet-mapping>
    <servlet-name>Resteasy</servlet-name>
    <url-pattern>/api/*</url-pattern>
  </servlet-mapping>

</web-app>

The implementation of Application:

应用的实现:

public class MatchApplication extends Application {
    private Set<Object> singletons = new HashSet<Object>();
    private Set<Class<?>> classes = new HashSet<Class<?>>();

    public MatchApplication() {
        singletons.add(new MatchServiceImpl());
    }

    @Override
    public Set<Class<?>> getClasses() {
        return classes;
    }

    @Override       
    public Set<Object> getSingletons() {
        return singletons;
    }
}

And here's the class that provide the REST service:

这是提供 REST 服务的类:

 /* Imports */
    ...
@Path("/matches")
public class MatchResource {
    private static MatchResource _instance = null;
    private MatchRepository repository;

    public MatchResource() {
        repository = new MapMatchRepository(); 
    }

    public static MatchResource getInstance() {
        if (_instance == null)
            _instance = new MatchResource();
        return _instance;
    }

    @GET
    @Path("/{id}")
    @Produces("application/json")
    public Match getMatch(@PathParam("id") int id) {
        return repository.getMatch(id);
    }

    @GET
    @Produces("application/json")
    public Matches getMatchesCurrentRound() {
        return repository.getMatchesCurrentRound();
    }

        ...
}

What I want is to return a JSON file when getting into, for example: http://127.0.0.1:8888/api/matches

我想要的是在进入时返回一个 JSON 文件,例如: http://127.0.0.1:8888/api/matches

Does anyone know what I'm doing wrong?

有谁知道我做错了什么?

Edit:

编辑:

If I access to http://127.0.0.1:8888/api/or http://127.0.0.1:8888/api/*(where * is whatever you want to write), the browser shows nothing. However, if I access to http://127.0.0.1:8888/oqiwn(where oqiwnis a random string) the browser shows a Error 404.

如果我访问http://127.0.0.1:8888/api/or http://127.0.0.1:8888/api/*(其中 * 是您想写的任何内容),浏览器将不显示任何内容。但是,如果我访问http://127.0.0.1:8888/oqiwn(其中oqiwn是随机字符串),浏览器会显示一个Error 404.

Also, I tried the RESTClient addon and these are the answers that returns:

另外,我尝试了 RESTClient 插件,这些是返回的答案:

With http://127.0.0.1:8888/api/or http://127.0.0.1:8888/api/*

http://127.0.0.1:8888/api/http://127.0.0.1:8888/api/*

Status Code: 404 Not Found
Cache-Control: no-cache
Content-Length: 0
Date: Sun, 10 Nov 2013 22:59:57 GMT
Expires: Fri, 01 Jan 1990 00:00:00 GMT
Server: Development/1.0

And with http://127.0.0.1:8888/oqiwn

http://127.0.0.1:8888/oqiwn

Status Code: 404 Not Found
Cache-Control: no-cache
Content-Length: 83
Content-Type: text/html; charset=iso-8859-1
Date: Sun, 10 Nov 2013 22:59:05 GMT
Expires: Fri, 01 Jan 1990 00:00:00 GMT
Server: Development/1.0

Note that Content-Type: text/html; charset=iso-8859-1is not in the first one.

请注意,这Content-Type: text/html; charset=iso-8859-1不在第一个中。

采纳答案by gregwhitaker

You added your resource using a method named getMatches(), which Resteasy knows nothing about. You need to override the getSingletons()method of Applicationand return your root resources from there as shown below.

您使用名为 的方法添加了资源getMatches(),Resteasy 对此一无所知。您需要覆盖getSingletons()方法Application并从那里返回您的根资源,如下所示。

Documentation Here

文档在这里

Example:

例子:

public class MatchApplication extends Application {
    private Set<Object> singletons = new HashSet<Object>();
    private Set<Class<?>> classes = new HashSet<Class<?>>();

    public MatchApplication() {
        singletons.add(new MatchServiceImpl());
    }

    @Override
    public Set<Class<?>> getClasses() {
        return classes;
    }

    @Override
    public Set<Object> getSingletons() {
        return singletons;
    }
}

回答by fasfsfgs

First, I think your MatchApplication class should be annotated with @ApplicationPath("/api"). I'm sorry if that's already done.

首先,我认为您的 MatchApplication 类应该用@ApplicationPath("/api")进行注释。如果这已经完成,我很抱歉。

Then, depending on your RESTEasy version, it will scan automatically for classes that are providers or resources, so you don't need to give to implement anything on your MatchApplication for now. Just extend Application and you are done.

然后,根据您的 RESTEasy 版本,它会自动扫描作为提供者或资源的类,因此您现在不需要在 MatchApplication 上实现任何内容。只需扩展应用程序即可完成。

If you can update your web-app to use servlet 3.0, you don't need to put any kind of configuration into your web.xml.

如果您可以更新您的 web 应用程序以使用 servlet 3.0,则不需要将任何类型的配置放入您的 web.xml。

Read more on the RESTEasy documentation.

阅读有关RESTEasy 文档的更多信息

回答by vjOnstack

This works for all my services.

这适用于我的所有服务。

This is a runtime exception indicating a resource requested by a client was not found on the server.
Add below entry into your web.xml :

<context-param>
        <param-name>resteasy.resources</param-name>
        <param-value>com.org.abc.xyz.MainClassName</param-value>
</context-param>

You can specify fully qualified name of your JAX-RS resource class name you want to register. If you have multiple classes entries, use comma delimiter.

您可以指定要注册的 JAX-RS 资源类名称的完全限定名称。如果您有多个类条目,请使用逗号分隔符。