我在 java 独立应用程序中嵌入了一个 Grizzly 服务器。我正在使用 Jersey 来使用 REST 调用来提供一些资源。虽然这在我在 Eclipse 中运行/调试时完美运行,但在尝试使用 Export -> Runnable JAR 创建 JAR 时,我一直收到以下错误:
严重:ResourceConfig 实例不包含任何根资源类。
这是我启动灰熊服务器的课程:
/**
* 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 文件更相关,因此泽西岛知道在哪里看……我尝试了这里的建议: https ://groups.google.com/d/msg/neo4j/0dNqGXvEbNg/xaNlRiU1cHMJ但是它似乎不起作用......
更新:我设法通过创建具有典型任务的手工制作的 ANT 文件来解决这个问题:清理、构建、jar 等......我不能真正说出我的 ANT 的具体点是什么来解决这个问题......但确实如此。如果您碰巧遇到同样的问题,您可以在此处下载并重复使用它,前提是您相应地更改它以适合您的项目。