簡體   English   中英

RESTEasy - javax.ws.rs.NotFoundException:無法找到完整路徑的資源

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

我試圖在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();
    }

        ...
}

我想要的是在進入時返回一個JSON文件,例如: http://127.0.0.1:8888/api/matcheshttp://127.0.0.1:8888/api/matcheshttp://127.0.0.1:8888/api/matches

有誰知道我做錯了什么?

編輯:

如果我訪問http://127.0.0.1:8888/api/http://127.0.0.1:8888/api/* (其中*是您要編寫的內容),瀏覽器不會顯示任何內容。 但是,如果我訪問http://127.0.0.1:8888/oqiwn (其中oqiwn是隨機字符串),瀏覽器會顯示Error 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 Content-Type: text/html; charset=iso-8859-1不在第一個。

您使用名為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;
    }
}

首先,我認為您的MatchApplication類應該使用@ApplicationPath(“/ api”)進行注釋。 如果已經完成,我很抱歉。

然后,根據您的RESTEasy版本,它將自動掃描提供程序或資源的類,因此您暫時不需要在MatchApplication上實現任何內容。 只需擴展應用程序即可完成。

如果您可以更新您的Web應用程序以使用servlet 3.0,則無需在web.xml中添加任何類型的配置。

閱讀有關RESTEasy文檔的更多信息

這適用於我的所有服務。

 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資源類名稱的完全限定名稱。 如果您有多個類條目,請使用逗號分隔符。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM