Java Jersey Rest:未找到提供程序类。(访问 URL 时出现 404 错误)

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6585211/
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-16 07:57:51  来源:igfitidea点击:

Java Jersey Rest : No provider classes found. (404 error when accessing URL)

javaapachetomcatrestjersey

提问by kensen john

Edit:I hadn't realized that all request were first going into "Apache" and then were redirected to Tomcat. I added a new redirection in the apache2.conffile. See the accepted answerfor details.

编辑:我没有意识到所有请求都首先进入“Apache”,然后被重定向到 Tomcat。我在apache2.conf文件中添加了一个新的重定向。有关详细信息,请参阅已接受的答案

I am having the exact same problem as this question. Jersey REST The ResourceConfig instance does not contain any root resource classesHowever the user never answered the question.

我遇到了与这个问题完全相同的问题。 Jersey REST ResourceConfig 实例不包含任何根资源类但是用户从未回答过这个问题。

I am using Tomcat, without maven. I followed this tutorial. http://www.ibm.com/developerworks/web/library/wa-aj-tomcat/index.html

我正在使用Tomcat,没有maven。我跟着这个教程。 http://www.ibm.com/developerworks/web/library/wa-aj-tomcat/index.html

I made changes to web.xml as per the article
i.e the new servlet and servlet mapping was created with the correct package name.

我按照文章对 web.xml 进行了更改,
即使用正确的包名称创建了新的 servlet 和 servlet 映射。

<servlet>
  <servlet-name>Jersey REST Service</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>sample.hello.resources</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> 

I have deployed the following jars to tomcat

我已将以下 jars 部署到 tomcat

asm.jar
jersey-server.jar
jersey-core.jar
jsr311.jar

The tomcat startup log has the following exceptions.

tomcat启动日志有以下异常。

    com.sun.jersey.api.core.PackagesResourceConfig init
    INFO: Scanning for root resource and provider classes in the packages:
      sample.hello.resources

   com.sun.jersey.api.core.ScanningResourceConfig logClasses
    INFO: Root resource classes found:
      class sample.hello.resources.HelloResource

   com.sun.jersey.api.core.ScanningResourceConfig init
    INFO: No provider classes found.

   com.sun.jersey.server.impl.application.WebApplicationImpl _initiate
    INFO: Initiating Jersey application, version 'Jersey: 1.8 06/24/2011 12:17 PM'

When I access the URL I get a 404. http://localhost:8080/Jersey/rest/hello

当我访问 URL 时,我得到一个 404。http ://localhost:8080/Jersey/rest/hello

The code:

编码:

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

@Path("/hello")
public class HelloResource {
    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public String sayHello() {
        return "Hello Jersey";
    }
}  

I do not see any other exceptions in the logs

我在日志中没有看到任何其他异常

采纳答案by kensen john

This issue was resolved as follows. My localhost has been setup with "Apache" webserver which redirects all requests to Tomcat. Since "Jersey" is using a new servlet, I had to create a separate redirect for this servlet specifically.

此问题已解决如下。我的本地主机已经设置了“Apache”网络服务器,它将所有请求重定向到 Tomcat。由于“Jersey”正在使用一个新的 servlet,我必须专门为这个 servlet 创建一个单独的重定向。

In Linux

在 Linux 中

/etc/apache2/apache2.conf

add:

添加:

JkMount /rest/* ajp13_worker

回答by Szocske

Verify you have the right URL:

验证您拥有正确的 URL:

The tomcat manager application is happy to direct you to the root URL of the servlet. (you are assuming this is "Jersey" with capital J.) http://localhost:8080/manager/html/list

tomcat 管理器应用程序很高兴将您定向到 servlet 的根 URL。(您假设这是带有大写字母 J 的“泽西”。) http://localhost:8080/manager/html/list

Then you can skip the "rest" part by setting the URL pattern in the servlet mapping of the web.xml to "/*" (yes, there was a bug in Jersey related to that once, but that's ancient history)

然后,您可以通过将 web.xml 的 servlet 映射中的 URL 模式设置为“/*”来跳过“其余”部分(是的,Jersey 中曾经有一个与此相关的错误,但那是古老的历史)

Then you can append "application.wadl" to get a description of available resources. Theoretically: http://localhost:8080/Jersey/application.wadl

然后您可以附加“application.wadl”以获取可用资源的描述。理论上: http://localhost:8080/Jersey/application.wadl

回答by Abidi

By default, Tomcat uses war file name or name of the top most directory of the exploded war as root context unless defined in catalina_home/conf/server.xml, catalina_home/conf/context.xml or in application's META-INF/context.xml file.

默认情况下,除非在 catalina_home/conf/server.xml、catalina_home/conf/context.xml 或应用程序的 META-INF/context.xml 中定义,否则 Tomcat 使用 war 文件名或爆炸后的最顶层目录的名称作为根上下文文件。

To access http://localhost:8080/Jersey/rest/hello, the context entry should be <Context path="/Jersey" docBase="myapp"/>and your resource should have @Path("/rest/hello").

要访问http://localhost:8080/Jersey/rest/hello,上下文条目应该是<Context path="/Jersey" docBase="myapp"/>并且您的资源应该有@Path("/rest/hello").