带泽西岛或Resteasy的嵌入式码头 [英] Embedded jetty with Jersey or resteasy
问题描述
我想使用带有JAX-RS(resteasy或jersey)的嵌入式码头来提供RESTful服务.
我正在尝试使用Maven/Eclipse安装程序进行创建.
如果我尝试遵循 http://wikis.sun.com/pages/viewpage.action?pageId=21725365 链接我无法解决
I want make RESTful services using embedded jetty with JAX-RS (either resteasy or jersey).
I am trying to create with maven/eclipse setup.
if I try to follow http://wikis.sun.com/pages/viewpage.action?pageId=21725365 link I am not able to resolve error from ServletHolder sh = new ServletHolder(ServletContainer.class);
public class Main {
@Path("/")
public static class TestResource {
@GET
public String get() {
return "GET";
}
}
/**
* @param args the command line arguments
*/
public static void main(String[] args) throws Exception {
ServletHolder sh = new ServletHolder(ServletContainer.class);
/*
* For 0.8 and later the "com.sun.ws.rest" namespace has been renamed to
* "com.sun.jersey". For 0.7 or early use the commented out code instead
*/
// sh.setInitParameter("com.sun.ws.rest.config.property.resourceConfigClass",
// "com.sun.ws.rest.api.core.PackagesResourceConfig");
// sh.setInitParameter("com.sun.ws.rest.config.property.packages",
// "jetty");
sh.setInitParameter("com.sun.jersey.config.property.resourceConfigClass",
"com.sun.jersey.api.core.PackagesResourceConfig");
sh.setInitParameter("com.sun.jersey.config.property.packages",
"edu.mit.senseable.livesingapore.platform.restws");
// sh.setInitParameter("com.sun.jersey.config.property.packages",
// "jetty");
Server server = new Server(9999);
ServletContextHandler context = new ServletContextHandler(server, "/",
ServletContextHandler.SESSIONS);
context.addServlet(sh, "/*");
server.start();
server.join();
// Client c = Client.create();
// WebResource r = c.resource("http://localhost:9999/");
// System.out.println(r.get(String.class));
//
// server.stop();
}
}
即使这不起作用. 有人可以给我一些建议/教程/例子吗?
even this is not working. can anyone suggest me something/tutorial/example ?
推荐答案
呵呵,链接页面很古老-最近更新于3年前.
huh, linked page is ancient - last update 3 years ago.
您真的需要码头吗?泽西岛与Grizzly进行了经过全面测试的出色集成(请参见 http://grizzly.java.net ),它同时也是Glassfish的传输层,它可以像您的示例一样使用它.
Do you really need jetty? Jersey has excellent thoroughly tested integration with Grizzly (see http://grizzly.java.net) which is also acting as Glassfish transport layer and it is possible to use it as in your example.
从Jersey工作区com.sun.jersey.samples.helloworld.helloworld中查看helloworld示例.主类启动Grizzly,并部署" helloworld应用程序:
See helloworld sample from Jersey workspace, com.sun.jersey.samples.helloworld.Main class starts Grizzly and "deploys" helloworld app: http://repo1.maven.org/maven2/com/sun/jersey/samples/helloworld/1.9.1/helloworld-1.9.1-project.zip .
如果您真的需要基于码头的样品,我想我应该能够提供(随时与我联系).
If you really need jetty based sample, I guess I should be able to provide it (feel free to contact me).
好吧,如果您真的想要跳船,可以拥有:),看起来非常简单.我遵循了 http://docs.codehaus.org/display/JETTY/Embedding+Jetty 中的说明,能够启动helloworld示例:
ok, if you really want jetty, you can have it :) and looks like its fairly simple. I followed instructions from http://docs.codehaus.org/display/JETTY/Embedding+Jetty and was able to start helloworld sample:
public static void main(String[] args) throws Exception {
Server server = new Server(8080);
Context root = new Context(server,"/",Context.SESSIONS);
root.addServlet(new ServletHolder(new ServletContainer(new PackagesResourceConfig("com.sun.jersey.samples.helloworld"))), "/");
server.start();
}
可以访问
http://localhost:8080/helloworld .我使用了Jetty 6.1.16.希望对您有帮助!
http://localhost:8080/helloworld is accessible. I used Jetty 6.1.16. Hope it helps!
您可以在用户指南中找到有关在servlet环境中配置Jersey的更多信息,请参见 http://jersey.java. net/nonav/documentation/latest/
You can find more information about configuring Jersey in servlet environment in user guide, see http://jersey.java.net/nonav/documentation/latest/
依赖性..但这很难指定,最近在球衣中改变了...
dependencies.. but this is kind of hard to specify, it changed recently in jersey.. so..
1.10之前的版本:
pre 1.10:
<dependency>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty</artifactId>
<version>6.1.16</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-server</artifactId>
<version>${jersey.version}</version>
</dependency>
发布1.10:
<dependency>
<groupId>org.mortbay.jetty</groupId>
<artifactId>jetty</artifactId>
<version>6.1.16</version>
</dependency>
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-servlet</artifactId>
<version>${jersey.version}</version>
</dependency>
您需要这个Maven回购码头:
and you need this maven repo for jetty:
<repositories>
<repository>
<id>codehaus-release-repo</id>
<name>Codehaus Release Repo</name>
<url>http://repository.codehaus.org</url>
</repository>
</repositories>
这篇关于带泽西岛或Resteasy的嵌入式码头的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!