Med*_*Man 3 java jetty jersey maven
我一直试图效仿这个例子(首先点击google for jersey + jetty),但运气不佳.
根据下面评论者的建议,我决定更新示例以使用最新版本的Jetty(9.1.0.RC2)和Jersey(2.7).
这是更新的pom,具有更新的依赖项:
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>HelloJerseyLatest</groupId>
<artifactId>HelloJerseyLatest</artifactId>
<version>0.0.1-SNAPSHOT</version>
<build>
<sourceDirectory>src</sourceDirectory>
<plugins>
<plugin>
<artifactId>maven-compiler-plugin</artifactId>
<version>3.1</version>
<configuration>
<source>1.7</source>
<target>1.7</target>
</configuration>
</plugin>
</plugins>
</build>
<dependencies>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet-core</artifactId>
<version>2.7</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-server</artifactId>
<version>9.1.0.RC2</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-servlet</artifactId>
<version>9.1.0.RC2</version>
</dependency>
</dependencies>
</project>
Run Code Online (Sandbox Code Playgroud)
首先要注意的是Jersey套装已经从改变com.sun.jersey.spi.*为org.glassfish.jersey.*.因此,主要方法也需要更改:
package example.server;
import org.eclipse.jetty.server.Server;
import org.eclipse.jetty.servlet.ServletContextHandler;
import org.eclipse.jetty.servlet.ServletHolder;
//import com.sun.jersey.spi.container.servlet.ServletContainer;
import org.glassfish.jersey.servlet.ServletContainer;
public class MinimalServerRest {
public static void main(String[] args) throws Exception {
ServletHolder sh = new ServletHolder(ServletContainer.class);
// these initialization strings will need to be updated.
sh.setInitParameter("com.sun.jersey.config.property.resourceConfigClass", "com.sun.jersey.api.core.PackagesResourceConfig");
sh.setInitParameter("com.sun.jersey.config.property.packages", "rest");//Set the package where the services reside
sh.setInitParameter("com.sun.jersey.api.json.POJOMappingFeature", "true");
Server server = new Server(9999);
ServletContextHandler context = new ServletContextHandler(server, "/", ServletContextHandler.SESSIONS);
context.addServlet(sh, "/*");
server.start();
server.join();
}
}
Run Code Online (Sandbox Code Playgroud)
将原始示例代码更新为最新版本需要进行哪些更改?我没有web.xml文件.我需要一个吗?

Wil*_*ill 11
我意识到这不是你给工作的例子(你的例子链接坏了) - 我不太了解泽西1,并且试图升级其他人的项目很困难.当你有另一个问题要求HelloWorld举例时,我认为你只需要一些东西让你自己去泽西和码头.
所以这里你去 - 两个例子,一个使用JettyHttpContainerFactory,另一个使用Jersey ServletContainer.
首先是泽西资源 - 非常简单.这将为类设置一个路径"test",并且路径为一个方法hello,接受GET以纯文本形式生成"Hello World".
@Path("/test")
public class TestResource {
@GET
@Path("hello")
@Produces(MediaType.TEXT_PLAIN)
public String hello() {
return "Hello World";
}
}
Run Code Online (Sandbox Code Playgroud)
接下来是服务器类:
public class ExampleServer {
public static void main(String[] args) {
URI baseUri = UriBuilder.fromUri("http://localhost/").port(9998).build();
ResourceConfig config = new ResourceConfig(TestResource.class);
Server server = JettyHttpContainerFactory.createServer(baseUri, config);
}
}
Run Code Online (Sandbox Code Playgroud)
最后是pom依赖项(注意这两个例子都有依赖关系).
<dependencies>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-server</artifactId>
<version>9.1.3.v20140225</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-servlet</artifactId>
<version>9.1.3.v20140225</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.core</groupId>
<artifactId>jersey-server</artifactId>
<version>2.7</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet-core</artifactId>
<version>2.7</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-jetty-http</artifactId>
<version>2.7</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-moxy</artifactId>
<version>2.7</version>
</dependency>
<!-- if you want to enable JSON support, include Moxy and Jersey will automatically enable the Feature -->
</dependencies>
Run Code Online (Sandbox Code Playgroud)
另请参阅https://jersey.java.net/apidocs/2.7/jersey/javax/ws/rs/core/Feature.html以了解功能 - 通过在类路径中包含Moxy,Jersey将自动注册MoxyJSONFeature.如果您更愿意使用Jackson,则需要手动注册JacksonFeature以及依赖项.您可以在注册资源的同一初始化参数中注册任何功能(以逗号分隔)
如果您希望配置为servlet,请将其用作ExampleServer代码
public class ExampleServer {
public static void main(String[] args) throws Exception {
Server server = new Server(9998);
ServletContextHandler context = new ServletContextHandler(ServletContextHandler.SESSIONS);
context.setContextPath("/");
server.setHandler(context);
ServletHolder jerseyServlet = context.addServlet(org.glassfish.jersey.servlet.ServletContainer.class, "/*");
jerseyServlet.setInitOrder(0);
/*This parameter tells the Jersey Servlet which of your REST resources to load. In this example we're adding the TestResource class. Jersey will then invoke this class for requests coming into paths denoted by the @Path parameter within the TestResource class. If you have multiple classes, you can either list them all comma separated, of use "jersey.config.server.provider.packages" and list the package name instead */
jerseyServlet.setInitParameter("jersey.config.server.provider.classnames", "foo.bar.TestResource");
server.start();
server.join();
}
}
Run Code Online (Sandbox Code Playgroud)
请注意servlet版本,我正在定义资源的类名.如果您有一些,最好提供包名称而不是使用jersey.config.server.provider.packages
希望这可以帮助.如果您有任何问题,请告诉我.
将
| 归档时间: |
|
| 查看次数: |
5794 次 |
| 最近记录: |