簡體   English   中英

為servlet Jersey REST服務錯誤分配異常

[英]Allocate exception for servlet Jersey REST Service Error

我正在重新編寫一個我已經創建的Web服務,這樣我就可以為我的小組中將接管該項目的其他成員用git創建一個“how-to”。

這一次,我在嘗試使用我的webservice時遇到了錯誤,我似乎無法找到問題,因為代碼看起來與我之前編寫的代碼完全相同(代碼有效)。

我正在使用apache tomcat和Jersey與JSON。

這是錯誤:

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)

這是我的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>

這是一個使用@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();
}

我一直在尋找和閱讀相同的文章3天,我還沒有找到解決我的錯誤的解決方案。

所有幫助表示贊賞

我將繼續回答我自己的問題,希望能幫助新人休息。

在創建我的教程時我犯了一個非常愚蠢的錯誤:我的web服務的“url”是,例如,com.webservice.rest。 當重新執行我的項目時,我決定更改我的包的名稱以與新url相對應:所以我的具有路徑注釋的類,比方說@PATH(“/ v1 / inventory”)已更改為com。 webservice.inventory。

在輸入新的包名時,我匆匆忙忙地忘了在com.webservice中加入rest這個詞。 休息 。庫存。

編寫Web服務時,您必須記住確保包含路徑注釋的每個包的前綴是Web服務的URL。

暫無
暫無

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

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