java 为 servlet Jersey REST 服务错误分配异常

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

Allocate exception for servlet Jersey REST Service Error

javaweb-servicesrestservlets

提问by Ju East

I'm re-coding a webservice that I've already created so that I can create a "how-to" with git for the other members of my group who will be taking over the project.

我正在重新编码一个我已经创建的网络服务,以便我可以使用 git 为我的小组中将接管该项目的其他成员创建一个“操作方法”。

This time around, I started to get an error when trying to use my webservice and I can't seem to find the problem because the code looks exactly like what I've previously coded (a code that worked).

这一次,我在尝试使用我的网络服务时开始遇到错误,我似乎无法找到问题,因为代码看起来与我之前编写的代码(有效的代码)完全一样。

I am using apache tomcat and Jersey with JSON.

我正在使用带有 JSON 的 apache tomcat 和 Jersey。

Here is the error:

这是错误:

mai 14, 2015 6:08:02 PM org.apache.catalina.core.StandardWrapperValve invoke SEVERE: Allocate exception for servlet Jersey REST Service com.sun.jersey.api.container.ContainerException: The ResourceConfig instance does not contain any root resource classes. at com.sun.jersey.server.impl.application.RootResourceUriRules.<init>(RootResourceUriRules.java:99) at com.sun.jersey.server.impl.application.WebApplicationImpl._initiate(WebApplicationImpl.java:1359) at com.sun.jersey.server.impl.application.WebApplicationImpl.access$700(WebApplicationImpl.java:180) at com.sun.jersey.server.impl.application.WebApplicationImpl$13.f(WebApplicationImpl.java:799) at com.sun.jersey.server.impl.application.WebApplicationImpl$13.f(WebApplicationImpl.java:795) at com.sun.jersey.spi.inject.Errors.processWithErrors(Errors.java:193) at com.sun.jersey.server.impl.application.WebApplicationImpl.initiate(WebApplicationImpl.java:795) at com.sun.jersey.server.impl.application.WebApplicationImpl.initiate(WebApplicationImpl.java:790) at com.sun.jersey.spi.container.servlet.ServletContainer.initiate(ServletContainer.java:509) at com.sun.jersey.spi.container.servlet.ServletContainer$InternalWebComponent.initiate(ServletContainer.java:339) at com.sun.jersey.spi.container.servlet.WebComponent.load(WebComponent.java:605) at com.sun.jersey.spi.container.servlet.WebComponent.init(WebComponent.java:207) at com.sun.jersey.spi.container.servlet.ServletContainer.init(ServletContainer.java:394) at com.sun.jersey.spi.container.servlet.ServletContainer.init(ServletContainer.java:577) at javax.servlet.GenericServlet.init(GenericServlet.java:212) at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1213) at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:827) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:129) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293) at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:861) at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:606) at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489) at java.lang.Thread.run(Unknown Source)

mai 14, 2015 6:08:02 PM org.apache.catalina.core.StandardWrapperValve invoke SEVERE: Allocate exception for servlet Jersey REST Service com.sun.jersey.api.container.ContainerException: The ResourceConfig instance does not contain any root resource classes. at com.sun.jersey.server.impl.application.RootResourceUriRules.<init>(RootResourceUriRules.java:99) at com.sun.jersey.server.impl.application.WebApplicationImpl._initiate(WebApplicationImpl.java:1359) at com.sun.jersey.server.impl.application.WebApplicationImpl.access$700(WebApplicationImpl.java:180) at com.sun.jersey.server.impl.application.WebApplicationImpl$13.f(WebApplicationImpl.java:799) at com.sun.jersey.server.impl.application.WebApplicationImpl$13.f(WebApplicationImpl.java:795) at com.sun.jersey.spi.inject.Errors.processWithErrors(Errors.java:193) at com.sun.jersey.server.impl.application.WebApplicationImpl.initiate(WebApplicationImpl.java:795) at com.sun.jersey.server.impl.application.WebApplicationImpl.initiate(WebApplicationImpl.java:790) at com.sun.jersey.spi.container.servlet.ServletContainer.initiate(ServletContainer.java:509) at com.sun.jersey.spi.container.servlet.ServletContainer$InternalWebComponent.initiate(ServletContainer.java:339) at com.sun.jersey.spi.container.servlet.WebComponent.load(WebComponent.java:605) at com.sun.jersey.spi.container.servlet.WebComponent.init(WebComponent.java:207) at com.sun.jersey.spi.container.servlet.ServletContainer.init(ServletContainer.java:394) at com.sun.jersey.spi.container.servlet.ServletContainer.init(ServletContainer.java:577) at javax.servlet.GenericServlet.init(GenericServlet.java:212) at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1213) at org.apache.catalina.core.StandardWrapper.allocate(StandardWrapper.java:827) at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:129) at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191) at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127) at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:103) at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109) at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:293) at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:861) at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:606) at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:489) at java.lang.Thread.run(Unknown Source)

Here is my web.xml file:

这是我的 web.xml 文件:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://java.sun.com/xml/ns/javaee" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
<display-name>com.labtabdb.rest</display-name>
<welcome-file-list>
  <welcome-file>readme.html</welcome-file>   
  <welcome-file>index.html</welcome-file>
  <welcome-file>index.htm</welcome-file>
  <welcome-file>index.jsp</welcome-file>
  <welcome-file>default.html</welcome-file>
  <welcome-file>default.htm</welcome-file>
  <welcome-file>default.jsp</welcome-file>
</welcome-file-list>

<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.labtabdb.rest</param-value> 
   </init-param>
  <load-on-startup>1</load-on-startup> 
</servlet>

<servlet-mapping>
   <servlet-name>Jersey REST Service</servlet-name>
   <url-pattern>/nxp/*</url-pattern> 
</servlet-mapping>

</web-app>

Here is a class that uses the @Path annotation:

这是一个使用@Path 注释的类:

//url path for class it's methods/chemin url pour la classe et ses methodes
@Path ("/v1/inventory") 
public class V1__inventory
{


@GET //url ending in only /v1/inventory /chemin terminé par /v1/inventory
@Produces(MediaType.APPLICATION_JSON) 
public Response errorIfNoQueryParamSpecified() throws Exception
{
        return Response
                .status(400) //bad request
                .entity("Error: Please specify extension and parameter for this search.") 
                .build();
}

/**
 * Method that returns materials filter by material name
 * @param material name
 * @return Response
 * @throws Exception
 */

@Path("/{material_label}") 
@GET
@Produces(MediaType.APPLICATION_JSON)
public Response returnMaterial(@PathParam("material_label") String material) throws Exception
{

    material = URLDecoder.decode(material, "UTF-8"); //decode URL param
    String returnString = null;
    JSONArray json;

    try
    {   

        json = LabTab_MySQLQuerys 
                .getMaterialsByName(material);

        //if there are no results to the query
        if(json.length() == 0) 
            throw new CustomException("No results returned");
        else //otherwise return results
            returnString = json.toString();

    }
    catch(CustomException e)
    {
        return Response
                .status(204)
                .entity(e.getMessage())
                .build();
    }
    catch (Exception e)
    {
        e.printStackTrace();
        return Response
                .status(500) //internal server error
                .entity("Server was not able to process your request")
                .build();
    }

    return Response
            .ok(returnString)
            .build();
}

I've been searching and reading the same articles over and over for 3 days now and I have yet to find a solution that solves my error.

我已经反复搜索和阅读相同的文章 3 天了,但我还没有找到解决我的错误的解决方案。

All help is appreciated

感谢所有帮助

回答by Ju East

I'm going to go ahead and respond to my own question in hopes that it helps someone new to rest.

我将继续回答我自己的问题,希望它能帮助新人休息。

I made a pretty stupid mistake while creating my tutorial: My "url" for my web service was, for example, com.webservice.rest. When re-doing my project, I decided to change the names of my packages to correspond with the new url: so my class that had a path annotation, let's say, @PATH ("/v1/inventory") was changed to com.webservice.inventory.

我在创建教程时犯了一个非常愚蠢的错误:例如,我的 Web 服务的“url”是 com.webservice.rest。重新做我的项目时,我决定更改我的包的名称以与新的 url 相对应:所以我的类有一个路径注释,比方说,@PATH ("/v1/inventory") 被更改为 com.网络服务.库存。

When typing the new package name, I rushed and forgot to include the word restin com.webservice.rest.inventory .

输入新包名的时候,仓促忘记在com.webservice中包含rest这个词。休息。库存。

When programming a webservice, you have to remember to make sure the the prefix of each package containing a path annotation is the url of the web service.

在编写 webservice 时,你必须记住确保每个包含路径注释的包的前缀是 web 服务的 url。

回答by Yao Li

Make sure to include org.javassist:*for all scopes, since javassistpackage that Jersey uses it to do bytecode manipulation and the shaded jar did not include the javassistpackage In my case.

确保包含org.javassist:*所有范围,因为javassistJersey 使用它来进行字节码操作的包,而阴影 jar 不包含javassist在我的情况下的包。

<dependency>
    <groupId>org.javassist</groupId>
    <artifactId>javassist</artifactId>
    <version>3.26.0-GA</version>
</dependency>