找不到提供程序类:运行Jersey REST服务时

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了找不到提供程序类:运行Jersey REST服务时相关的知识,希望对你有一定的参考价值。

我是Java的新手,我已经按照教程创建了一个JAX-RS服务,我所做的一切都是根据教程完成的,但无法让它工作。我附上了屏幕截图和日志。

VEB.HML

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" id="WebApp_ID" version="3.1">
  <display-name>com.duo.test</display-name>
 <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>com.duo.test</param-value>
      </init-param>
      <load-on-startup>1</load-on-startup>
  </servlet>
  <servlet-mapping>
      <servlet-name>Jersey Rest Service</servlet-name>
      <url-pattern>/api/*</url-pattern>
  </servlet-mapping>  
</web-app>

我的课:

package com.duo.test.status;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;

@Path("/v1/status/*")
public class V1_status {
    @GET
    @Produces(MediaType.TEXT_html)
    public String returnTitle()
    {
    return "<p> Java Web Serbice</p>";  
    }

}

日志:

Sep 23, 2014 12:22:56 PM org.apache.catalina.core.AprLifecycleListener init
INFO: The APR based Apache Tomcat Native library which allows optimal performance in production environments was not found on the java.library.path: C:Program FilesJavajre7in;C:WINDOWSSunJavain;C:WINDOWSsystem32;C:WINDOWS;C:Program Files (x86)InteliCLS Client;C:Program FilesInteliCLS Client;C:WINDOWSsystem32;C:WINDOWS;C:WINDOWSSystem32Wbem;C:WINDOWSSystem32WindowsPowerShellv1.0;C:Program FilesIntelIntel(R) Management Engine ComponentsDAL;C:Program FilesIntelIntel(R) Management Engine ComponentsIPT;C:Program Files (x86)IntelIntel(R) Management Engine ComponentsDAL;C:Program Files (x86)IntelIntel(R) Management Engine ComponentsIPT;C:Program Files (x86)IntelOpenCL SDK2.0inx86;C:Program Files (x86)IntelOpenCL SDK2.0inx64;C:Program FilesMicrosoftWeb Platform Installer;D:
dk;C:ant;C:Program FilesJavajdk1.7.0_07in;D:SoftwaresEcclipseEcclipsesdkuild-tools18.1.0;C:Program Files (x86)Windows Kits8.0Windows Performance Toolkit;C:Program Files (x86)Windows Kits8.1Windows Performance Toolkit;C:Program Files (x86)mysqlMySQL Server 5.6in;C:Program Files (x86)GtkSharp2.12in;C:Takipilib;C:Takipilib32;C:Program Files (x86)Microsoft SDKsTypeScript1.0;C:Program Files (x86)
odejs;JAVA_HOME%in;C:apache-ant-1.9.4in;C:adt-bundlesdkplatform-tools;C:adt-bundlesdk	ools;;C:Program Files (x86)Javajdk1.6.0_39in;C:Userssajeetharan.DUOSL.000AppDataLocalandroidandroid-sdk	ools;C:Userssajeetharan.DUOSL.000AppDataLocalAndroidandroid-sdkplatform-tools;C:Program FilesMicrosoft SQL Server110ToolsBinn;;C:Program Files (x86)InteliCLS Client;C:Program FilesInteliCLS Client;C:WINDOWSsystem32;C:WINDOWS;C:WINDOWSSystem32Wbem;C:WINDOWSSystem32WindowsPowerShellv1.0;C:Program FilesIntelIntel(R) Management Engine ComponentsDAL;C:Program FilesIntelIntel(R) Management Engine ComponentsIPT;C:Program Files (x86)IntelIntel(R) Management Engine ComponentsDAL;C:Program Files (x86)IntelIntel(R) Management Engine ComponentsIPT;C:Program Files (x86)IntelOpenCL SDK2.0inx86;C:Program Files (x86)IntelOpenCL SDK2.0inx64;C:Program FilesMicrosoftWeb Platform Installer;D:
dk;C:ant;C:Program FilesJavajdk1.7.0_07in;D:SoftwaresEcclipseEcclipsesdkuild-tools18.1.0;C:Program Files (x86)Windows Kits8.0Windows Performance Toolkit;C:Program Files (x86)Windows Kits8.1Windows Performance Toolkit;C:Program Files (x86)MySQLMySQL Server 5.6in;C:Program Files (x86)GtkSharp2.12in;C:Takipilib;C:Takipilib32;C:Program Files (x86)Microsoft SDKsTypeScript1.0;C:Program Files (x86)
odejs;JAVA_HOME%in;C:apache-ant-1.9.4in;C:adt-bundlesdkplatform-tools;C:adt-bundlesdk	ools;;C:Program Files (x86)Javajdk1.6.0_39in;C:Userssajeetharan.DUOSL.000AppDataLocalAndroidandroid-sdk	ools;C:Userssajeetharan.DUOSL.000AppDataLocalAndroidandroid-sdkplatform-tools;C:Program FilesMicrosoft SQL Server110ToolsBinn\tools;C:apache-ant-1.9.4in;C:adt-bundlesdk	ools;C:adt-bundlesdkplatform-tools;c:userssajeetharan.DUOSL.000AppDataRoaming
pm;.
Sep 23, 2014 12:22:57 PM org.apache.tomcat.util.digester.SetPropertiesRule begin
WARNING: [SetPropertiesRule]{Server/Service/Engine/Host/Context} Setting property 'source' to 'org.eclipse.jst.jee.server:com.duo.test' did not find a matching property.
Sep 23, 2014 12:22:57 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["http-nio-8080"]
Sep 23, 2014 12:22:57 PM org.apache.tomcat.util.net.NioselectorPool getSharedSelector
INFO: Using a shared selector for servlet write/read
Sep 23, 2014 12:22:57 PM org.apache.coyote.AbstractProtocol init
INFO: Initializing ProtocolHandler ["ajp-nio-8009"]
Sep 23, 2014 12:22:57 PM org.apache.tomcat.util.net.NioSelectorPool getSharedSelector
INFO: Using a shared selector for servlet write/read
Sep 23, 2014 12:22:57 PM org.apache.catalina.startup.Catalina load
INFO: Initialization processed in 2349 ms
Sep 23, 2014 12:22:58 PM org.apache.catalina.core.StandardService startInternal
INFO: Starting service Catalina
Sep 23, 2014 12:22:58 PM org.apache.catalina.core.StandardEngine startInternal
INFO: Starting Servlet Engine: Apache Tomcat/8.0.12
Sep 23, 2014 12:23:01 PM com.sun.jersey.api.core.PackagesResourceConfig init
INFO: Scanning for root resource and provider classes in the packages:
  com.duo.test
Sep 23, 2014 12:23:01 PM com.sun.jersey.api.core.ScanningResourceConfig logClasses
INFO: Root resource classes found:
  class com.duo.test.status.V1_status
Sep 23, 2014 12:23:01 PM com.sun.jersey.api.core.ScanningResourceConfig init
INFO: No provider classes found.
Sep 23, 2014 12:23:01 PM com.sun.jersey.server.impl.application.WebApplicationImpl _initiate
INFO: Initiating Jersey application, version 'Jersey: 1.17 01/17/2013 03:31 PM'
[2014-09-23 12:23:03 TAKIPI] User code detected.
Sep 23, 2014 12:23:04 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["http-nio-8080"]
Sep 23, 2014 12:23:04 PM org.apache.coyote.AbstractProtocol start
INFO: Starting ProtocolHandler ["ajp-nio-8009"]
Sep 23, 2014 12:23:04 PM org.apache.catalina.startup.Catalina start
INFO: Server startup in 6653 ms

OUTPUT:

答案

其中一个问题,我发现有错误

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

在Rest服务类中使用Lambda表达式(Java 8)。如果在下面的web.xml文件中指定的包中使用了Lambdas,则Jersey无法扫描注释:

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

解决此问题的方法是删除指定包中任何类的Lambda。

注意:这可能只是Jersey 1.x的一个问题,我还没有尝试过使用Jersey 2.x.删除Lambda帮助我暂时解决了这个问题。

另一答案

URL映射是错误的,它应该是

<servlet-mapping>
      <servlet-name>Jersey Rest Service</servlet-name>
      <url-pattern>/v1/*</url-pattern>
</servlet-mapping>

根据您的配置,API URL应该是qazxsw poi

要么

在Service类中更改您的URI映射

http://localhost:8080/v1/status/
另一答案

似乎当我遇到这个时,它将球衣从1.0.2升级到1.1x并且日志会说

package com.duo.test.status;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;

@Path("/api/*")
public class V1_status {
    @GET
    @Produces(MediaType.TEXT_HTML)
    @Path("/v1/status/")
    public String returnTitle()
    {
    return "<p> Java Web Serbice</p>";  
    }

}

然后一切都将成为404,尽管它说成功部署了webapp,但根本无法实现。

原来我的

INFO: Root resource classes found:
  class org.x.y.MyClass
No provider classes found.

<servlet-mapping> <servlet-name>Webapp Human name</servlet-name> <url-pattern>/</url-pattern> </servlet-mapping> 不再适合新版球衣,它需要

web.xml

ref:<url-pattern>/*</url-pattern>

以上是关于找不到提供程序类:运行Jersey REST服务时的主要内容,如果未能解决你的问题,请参考以下文章

Jersey Rest服务类型

使用 oAuth 2.0 保护基于 Jersey 的 REST 服务

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

严重:找不到媒体类型 = 应用程序/json、类型 = 类 com.jersey.jaxb.Todo、通用类型 = 类 com.jersey.jaxb.Todo 的 MessageBodyWriter

运行程序时找不到主类

Spring 4 vs Jersey 用于 REST Web 服务