在部署为JAR时,Jersey无法找到资源,但在Eclipse中可以使用

时间:2013-06-05 09:15:52

标签: java eclipse jersey grizzly

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

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

这是我的类启动Grizzly服务器:

/**
 * 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文件来解决这个问题:干净,构建,jar等等...我真的不能说我的ANT的具体内容是什么解决这个问题...但确实如此。您可以下载它here并在遇到相同问题时重复使用它,只要您根据项目进行相应更改即可。

1 个答案:

答案 0 :(得分:0)

如果没有你的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.