RESTEasy - javax.ws.rs.NotFoundException: 路径未找到资源

5

我尝试在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)

我的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>

应用程序的实现:
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;
    }
}

这里是提供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();
    }

        ...
}

我想要的是,在进入例如:http://127.0.0.1:8888/api/matches 时返回一个JSON文件。是否有人知道我做错了什么?
编辑: 如果我访问 http://127.0.0.1:8888/api/ 或 http://127.0.0.1:8888/api/* (其中*是您想要写的任何内容),浏览器将不显示任何内容。但是,如果我访问 http://127.0.0.1:8888/oqiwn(其中oqiwn是一个随机字符串),浏览器会显示一个错误404。
此外,我尝试了RESTClient插件并返回以下答案: 使用 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

使用 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

请注意,第一个中没有 Content-Type: text/html; charset=iso-8859-1

1
来自James Lyell的评论:如果您将@Path更改为@Path("/api/matches"),它是否有效? - Duncan Jones
3个回答

2
你使用了一个名为getMatches()的方法添加了你的资源,但是Resteasy对此一无所知。你需要重写ApplicationgetSingletons()方法,并从那里返回你的根资源,如下所示。 文档在此处 示例:
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;
    }
}

我已经将 getMatches() 更改为 getSingletons(),并且将 matches 更改为 singletons。但是它仍然无法正常工作。我需要在 MatchResource 类中进行编辑吗?(例如,repository.getMatchesCurrentRound() 返回直接从另一个 API REST 获取的比赛列表。这样做对吗?) - Kaer
你是否仍然遇到HTTP 404 - NotFoundException错误?另外,在你上面的问题中,我看到你编辑了MatchApplication类,但你仍然在将资源添加到matches中。那只是一个打字错误吗? - gregwhitaker
哎呀,不好意思,那是个打错字。至于第一个问题,我之前已经问过你了。 - Kaer

0

这适用于我所有的服务。

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>

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


0

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

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

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

请阅读RESTEasy文档以获取更多信息。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接