Jersey创建standalone server

2014-11-24 10:14:18 · 作者: · 浏览: 0

第一,pom.xml要修改一下:

[plain]

com.sun.jersey
jersey-server
${jersey-version}


com.sun.jersey
jersey-grizzly2
${jersey-version}
compile

[plain]

1.15

第二,Main.java导入的包如下:
[plain]
import com.sun.jersey.api.container.grizzly2.GrizzlyServerFactory;
import com.sun.jersey.api.core.PackagesResourceConfig;
import com.sun.jersey.api.core.ResourceConfig;
import java.io.IOException;
import java.net.URI;
import javax.ws.rs.core.UriBuilder;
import org.glassfish.grizzly.http.server.HttpServer;
代码也要做如下修改:
[plain]
public class Main {

private static int getPort(int defaultPort) {
//grab port from environment, otherwise fall back to default port 9998
String httpPort = System.getProperty("jersey.test.port");
if (null != httpPort) {
try { www.2cto.com
return Integer.parseInt(httpPort);
} catch (NumberFormatException e) {
}
}
return defaultPort;
}

private static URI getBaseURI() {
return UriBuilder.fromUri("http://localhost/").port(getPort(9998)).build();
}

public static final URI BASE_URI = getBaseURI();

protected static HttpServer startServer() throws IOException {
System.out.println("Starting grizzly...");
ResourceConfig rc = new PackagesResourceConfig("com.esri");
return GrizzlyServerFactory.createHttpServer(BASE_URI, rc);
/*
final Map initParams = new HashMap();

initParams.put("com.sun.jersey.config.property.packages", "com.esri");

System.out.println("Starting grizzly2...");
return GrizzlyWebContainerFactory.create(BASE_URI, initParams);
*/
}

public static void main(String[] args) throws IOException {
// Grizzly 2 initialization
HttpServer httpServer = startServer();
System.out.println(String.format("Jersey app started with WADL available at "
+ "%sapplication.wadl\nHit enter to stop it...",
BASE_URI));
System.in.read();
httpServer.stop();
}
}

startServer函数中注释掉的部分是向导产生的旧的代码。
好,再启动看看,一切正常。