java如何更新示例以使用最新版本Jetty(9.1.0.RC2)和Jersey(2.7)?
我一直在尝试跟踪这个example(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>
首先要注意的是,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();
}
}
要将原始示例代码更新为当前代码,需要进行哪些更改?我没有网络。xml文件。我需要吗
# 1 楼答案
我意识到这并没有让你给出的例子发挥作用(你的例子链接断了)-我对Jersey 1不太了解,尝试升级其他人的项目很困难。因为你有另一个question要求一个
HelloWorld
的例子,我想你只是需要一些东西来让自己与泽西&;码头这里有两个例子,一个使用}
JettyHttpContainerFactory
,另一个使用Jersey
{首先是泽西岛资源——非常简单。这将设置路径为
"test"
的类,以及路径为hello
的方法,接受GET
以纯文本形式生成“Hello World”接下来是服务器类:
最后是pom依赖项(请注意,这两个示例都有依赖项)
另请参见https://jersey.java.net/apidocs/2.7/jersey/javax/ws/rs/core/Feature.html了解特性——通过在类路径上包含Moxy,Jersey将自动注册MoxyJSONFeature。如果你想使用Jackson,你需要手动注册JacksonFeature和依赖项。您可以在注册资源的同一初始参数中注册任何功能(逗号分隔)
如果您希望配置为servlet,请将其用作示例服务器代码
注意,在servlet版本中,我定义了资源的类名。如果有一些,最好使用
jersey.config.server.provider.packages
提供包名希望这有帮助。如果你有任何问题,请告诉我
威尔