Java ResourceConfig实例不包含任何根资源类

Java ResourceConfig实例不包含任何根资源类,java,jersey,jax-rs,Java,Jersey,Jax Rs,虽然这是一个古老的问题,但我仍然找不到解决这个问题的答案。 如果你发现我的陈述有任何错误,请更正 我有一个Java Face应用程序,并使用REST提供Web服务。我不认为脸和我的问题有任何关系。 web.xml是: <servlet> <servlet-name>NDREST</servlet-name> <servlet-class> com.sun.jersey.spi.container.servlet.Se

虽然这是一个古老的问题,但我仍然找不到解决这个问题的答案。 如果你发现我的陈述有任何错误,请更正

我有一个Java Face应用程序,并使用REST提供Web服务。我不认为脸和我的问题有任何关系。 web.xml是:

<servlet>
    <servlet-name>NDREST</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.bi.nd.webservice</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
    <servlet-name>NDREST</servlet-name>
    <url-pattern>/nd/*</url-pattern>
</servlet-mapping>
我的类有一个@GET的事实足以将自己标识为一个资源类。
更不用说其他的复杂性了,我在Eclipse中用Ant编译了我的源代码,在catalina.out文件中得到了这个错误:

May 24, 2011 8:48:46 AM com.sun.jersey.api.core.PackagesResourceConfig init
INFO: Scanning for root resource and provider classes in the packages:
     com.bi.nd.webservice
May 24, 2011 8:48:46 AM com.sun.jersey.server.impl.application.WebApplicationImpl _initiate
INFO: Initiating Jersey application, version 'Jersey: 1.7 05/20/2011 11:04 AM'
May 24, 2011 8:48:46 AM com.sun.jersey.server.impl.application.RootResourceUriRules <init>
SEVERE: The ResourceConfig instance does not contain any root resource classes.

但问题远未结束。我尝试访问URL
http://localhost:8080/nd/hello
我仍然没有找到404。未找到提供程序类是一条重要消息吗?

我找到了Jersey无法找到根资源的另一个原因。错误信息是相同的,所以我认为在这里记录根本原因是一个很好的理由,以防其他人偶然发现它

我有一个根资源类,上面有以下注释:

@Singleton
@Path("/helloWorld")
@Service(...) // my own custom annotation
public class MyRootResource {
...
}
这将导致Jersey中的根资源扫描失败

修复方法是更改注释的顺序:

@Service(...) // my own custom annotation
@Singleton
@Path("/helloWorld")
public class MyRootResource {
...
}
这很有效。

我发现“找不到提供程序类”消息很可怕,但并不重要

“ResourceConfig实例不包含任何根资源类”是一个更大的问题


我发现resource类需要用@Path注释,provider类(如果您有一个——我们使用一个来将异常映射到HTTP响应代码)需要用@provider注释,并且两者都需要在正确的包中,Jersey才能找到它们。

您可能需要使用这样的url

http://localhost:8080/<web_context_root>/nd/hello
http://localhost:8080//nd/hello

首先,请注意,在类上使用@GET不足以将其标识为根资源类;该类上必须有@Path。你的有,所以这不是问题所在

我和你有同样的问题:它在Eclipse中工作,但当我构建它供外部使用时就不工作了

我使用的是嵌入式Jetty,其main()如下所示:

public static void main(String argv[])
{
    try
    {
        // from http://wiki.eclipse.org/Jetty/Tutorial/Embedding_Jetty
        Server server = new Server(8080);
        ServletContextHandler contextHandler = new ServletContextHandler(ServletContextHandler.SESSIONS);
        contextHandler.setContextPath("/");
        server.setHandler(contextHandler);

        // http://stackoverflow.com/questions/9670363/how-do-i-programmatically-configure-jersey-to-use-jackson-for-json-deserializa
        final PackagesResourceConfig prc = new PackagesResourceConfig("com.ultimatefoodfight.server.api");
        final Map<String, Object> prcProperties = prc.getProperties();
        prcProperties.put(JSONConfiguration.FEATURE_POJO_MAPPING, true);

        // from http://stackoverflow.com/questions/7421574/embedded-jetty-with-jersey-or-resteasy
        contextHandler.addServlet(new ServletHolder(new ServletContainer(prc)), "/*");

        server.start();
        server.join();
    }
    catch(Exception e)
    {
        System.out.println(e);
    }
}
然而,当我在服务器上启动应用程序时,我只得到了它的前两行,并且没有找到根资源类

事实证明,当你制作一个jar时,有不同的方法来构造它;特别是,如果您只是通过列出类的路径来实现,那么 只是那些条目。但是,如果用通配符将jar指向目录,那么也会得到所有的中间路径。有关提示我的示例,请参阅此消息:


PackageResourceConfig类依赖于有一个带有包名的目录,以便在其中查找类。我回到Eclipse,在“Export…>Jar”对话框的底部找到了一个“adddirectory entries”选项。我打开它,再次导出Jar,然后扫描找到了我的资源类。您需要在构建环境中找到一些类似的选项。

我收到了相同的错误消息,并通过修改web.xml文件解决了它。确保其中包含以下内容:

<init-param>
   <param-name>com.sun.jersey.config.property.packages</param-name>
   <param-value>your.package.name.here</param-value>
</init-param>

com.sun.jersey.config.property.packages
你的.package.name.在这里

我也花了5到6个小时来解决这个问题,最终解决了这个问题。这也许对你也有用

我一直在使用的源代码可以在上找到。使用ApacheTomcat 6.0.20、asm-all-3.3.1.jar、jersey-bundle-1.17.1.jar和jsr311-api-1.1.1.jar,我得到了与OP相同的结果,即

INFO: Root resource classes found:
  class com.mypackage.MyClass
13/03/2013 4:32:30 PM com.sun.jersey.api.core.ScanningResourceConfig init
INFO: No provider classes found.
我在不同的部署文件中有以下设置:

context root = rivets
<url-pattern>/rest</url-pattern>
@Path("/hello")
我最终通过将url模式更改为:

<url-pattern>/rest/*</url-pattern>
/rest/*

虽然我不确定当我开始引入更复杂的资源时,这将如何维持下去。不管怎样,希望这能帮助别人。

我也有同样的问题。通过在web.xml中固定init param标记下的param值解决了这个问题。 默认情况下,Eclipse在web.xml中将项目名称设置为display name。不应复制到参数值,而应复制到Java包

  <init-param>
    <param-name>com.sun.jersey.config.property.packages</param-name>
    <param-value>com.test.demo</param-value>
    </init-param> 

com.sun.jersey.config.property.packages
com.test.demo

希望这有帮助。

要解决此问题,请验证您的类MapperIn,需要指定路径:

import javax.ws.rs.GET;
import javax.ws.rs.OPTIONS;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import com.lot.account.api.doc.GetMemberAccountsIn;
import com.lot.common.doc.CommonApi;
import com.lot.common.doc.FlowData;
import com.lot.security.authorization.RequestData;


@Path("/account/member/")
public class MapperIn {

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    @Path("/{memberId}")
    public GetAccountIn prepareGetAccount(@PathParam("memberId") String memberId) {

        // create apiInput
        GetAccountIn apiInput = new GetAccountIn ();

        // map values from url
            apiInput.setMemberId(memberId);


        return apiInput;
    }
//...
}

所以我读了一遍答案,仍然有同样的问题。我决定如下

将“jettyweb.xml”添加到“web-INF”文件夹中。因此: src/main/webapp/WEB-INF/jetty-WEB.xml

jetty-web.xml的内容是:

<?xml version="1.0"  encoding="ISO-8859-1"?>
<!DOCTYPE Configure PUBLIC "-" "http://www.eclipse.org/jetty/configure.dtd">
<Configure class="org.eclipse.jetty.webapp.WebAppContext">
    <Set name="contextPath">/YourEndpoint</Set>
</Configure>

希望这对其他人有所帮助。

您能告诉我们您解决了什么问题吗?我也有同样的问题!我一直得到一个404,尽管我已经用一颗漂亮的牙齿完成了所有的事情comb@user759646面对同样的问题,我在weblogicHey Chris中运行它!非常感谢你的帖子。我讨厌jersey,因为它有时用来检测我的根资源,有时却没有。我不知道问题是什么,你的帖子指出了确切的问题并帮助我解决了它。再次非常感谢!那对我不起作用。。。即使在我的web.xml中添加了这个,错误仍然存在,或者我们可以更改server.xml文件中的web\u Context\u根。这是因为我忘记在web.xml resteasy.servlet.mapping.prefix/rest中定义端点。我使用@RequestMapping(value=“/api”),然后通过@Path(“/api”)进行更改
<url-pattern>/rest/*</url-pattern>
  <init-param>
    <param-name>com.sun.jersey.config.property.packages</param-name>
    <param-value>com.test.demo</param-value>
    </init-param> 
import javax.ws.rs.GET;
import javax.ws.rs.OPTIONS;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;

import com.lot.account.api.doc.GetMemberAccountsIn;
import com.lot.common.doc.CommonApi;
import com.lot.common.doc.FlowData;
import com.lot.security.authorization.RequestData;


@Path("/account/member/")
public class MapperIn {

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    @Path("/{memberId}")
    public GetAccountIn prepareGetAccount(@PathParam("memberId") String memberId) {

        // create apiInput
        GetAccountIn apiInput = new GetAccountIn ();

        // map values from url
            apiInput.setMemberId(memberId);


        return apiInput;
    }
//...
}
<?xml version="1.0"  encoding="ISO-8859-1"?>
<!DOCTYPE Configure PUBLIC "-" "http://www.eclipse.org/jetty/configure.dtd">
<Configure class="org.eclipse.jetty.webapp.WebAppContext">
    <Set name="contextPath">/YourEndpoint</Set>
</Configure>
http://localhost:8080/YourEndpoint/webresources/someMethod