我正在将Grizzly服务器嵌入Java独立应用程序中。我正在使用Jersey通过REST调用提供一些资源。虽然这在我在Eclipse中运行/调试时非常有效,但是当尝试使用Export-> Runnable JAR创建一个JAR时,我总是遇到以下错误:

严重:ResourceConfig实例不包含任何根资源类。

这是我启动Grizzly Server的课程:

/**
 * Constructor
 */
public RestServer(){
    //set up default initParams
    initParams.put( "com.sun.jersey.config.property.packages", packageName);
    initParams.put( "com.sun.jersey.api.json.POJOMappingFeature", Boolean.toString(jsonParsing));
}

/**
 * Starts the Grizzly Server thread
 * @return  TRUE if start was correct
 */
public boolean startGrizzly(){
    if (grizzlyRunning){
        Logger.error(DEBUG_TAG, "Grizzly Server is already running");
        return false;
    }
    else{
        String hostString = host;
        String portString = Integer.toString(port);
        String server = "http://" + hostString + ":" + portString + "/";
        try {
            selector = GrizzlyWebContainerFactory.create( server, initParams );
            selector.setKeepAliveTimeoutInSeconds(-1);
            grizzlyRunning = true;
            return true;
        }
        catch (IllegalArgumentException | IOException e) {
            Logger.error(DEBUG_TAG, "There was an error trying to start the Grizzly Server. Reason is " + e.getMessage());
            grizzlyRunning = false;
            return false;
        }
    }

}

/**
 * Stops the Grizzly Server thread
 * @return  TRUE if stop was correct
 */
public boolean stopGrizzly(){
    if (grizzlyRunning){
        selector.stopEndpoint();
        grizzlyRunning = false;
        return true;
    }
    else{
        Logger.error(DEBUG_TAG, "Grizzly is not running. Cannot stop it");
        return false;
    }
}


包名称包含我的其他具有REST资源的类的路径,在本例中为“ com.mareculum.model”。也就是说,这是服务的类:

package com.mareculum.model;

@Path("/json")
public class RestResources {

    /**
     * Debugging tag
     */
    private static final String DEBUG_TAG = "RestResources";


    /**
     * Gets the JSON collection of data from itsaseusbuoys
     * @return The JSON array containing the data from itsaseus buoys
     */
    @GET
    @Path("/get/itsaseus_data_raw")
    @Produces(MediaType.APPLICATION_JSON)
    public ItsaseusDataEntity [] getItsaseusDataRaw() {
        Vector<ItsaseusDataEntity> ret = getItsaseusBuoysData();
        if (ret != null){
            return (ItsaseusDataEntity[]) ret.toArray();
        }
        else{
            return null;
        }
    }


    /**
     * Gets the JSON collection of data from itsaseusbuoys
     * @return The JSON array containing the data from itsaseus buoys
     */
    @GET
    @Path("/get/itsaseus_fusion_table")
    @Produces(MediaType.APPLICATION_JSON)
    public ItsaseusFusionTableRow [] getItsaseusDataEntity() {
        ArrayList<ItsaseusFusionTableRow> data = getDataFromItsaseusTable();
        ItsaseusFusionTableRow [] retArray = new ItsaseusFusionTableRow[]{};
        if (data != null){
            retArray = data.toArray(new ItsaseusFusionTableRow[data.size()]);
            return retArray;
        }
        else{
            return null;
        }
    }

    /**
     * Gets the JSON data from NOAA
     * @return The JSON object containing data from NOAA buoy
     */
    @GET
    @Path("/get/noaa_fusion_table")
    @Produces(MediaType.APPLICATION_JSON)
    public BuoyRSSData [] getNoaaDataEntity() {
        ArrayList<BuoyRSSData> data = getDataFromNoaaTable();
        BuoyRSSData [] retArray = new BuoyRSSData[]{};
        if (data != null){
                retArray = data.toArray(new BuoyRSSData[data.size()]);
                return retArray;
        }
        else{
            return null;
        }
    }
}


如您所见,这里没有什么真正复杂的。在我看来,这与如何打包JAR文件更相关,因此Jersey知道在哪里寻找...我尝试了此处提出的建议:
https://groups.google.com/d/msg/neo4j/0dNqGXvEbNg/xaNlRiU1cHMJ但这似乎不起作用...

更新:我设法通过创建一个手工完成的ANT文件来解决此问题,该文件具有以下典型任务:清理,构建,罐子等...我不能真正说出我的ANT的特定点正在解决此问题...但是可以。您可以下载here并在遇到相同问题的情况下重复使用,前提是您进行了相应更改以适合您的项目。

最佳答案

没有web.xml很难说,但是请检查以下内容:


  导致此“ ResourceConfig”错误消息的原因很多。很少
  我知道的解决方案:

com.sun.jersey.config.property.packages doesn’t exist in your web.xml.
com.sun.jersey.config.property.packages included a resource that doesn’t include any jersey services.

07-28 03:33
查看更多