Jersey 2.26 - 启动服务器时出现 NoSuchFieldError
Jersey 2.26 - NoSuchFieldError when starting server
我正在尝试使用 Jersey 和 Jetty 设置一个简单的服务器。但是 Jersey documentation 中的代码抛出此错误:
Exception in thread "main" java.lang.NoSuchFieldError: GIT_HASH
at org.eclipse.jetty.server.Server.doStart(Server.java:370)
at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
at org.glassfish.jersey.jetty.JettyHttpContainerFactory.createServer(JettyHttpContainerFactory.java:305)
at org.glassfish.jersey.jetty.JettyHttpContainerFactory.createServer(JettyHttpContainerFactory.java:135)
at com.zoho.dfs.App.main(App.java:17)
这是我的 App.java
class:
public class App
{
public static void main(String[] args) {
URI baseUri = UriBuilder.fromUri("http://127.0.0.1/").port(8050).build();
ResourceConfig config = new ResourceConfig(RestController.class);
Server server = JettyHttpContainerFactory.createServer(baseUri, config);
try {
server.start();
server.join();
} catch (Exception e) {
e.printStackTrace();
}
}
}
和RestController.class
(我想映射一个
@Path("/{username}")
public class RestController {
@GET
@Path("/home")
@Produces(MediaType.APPLICATION_JSON)
public Response read(@PathParam("username") String username) {
String name = "hello " + username;
return Response.status(200).entity(name).build();
}
}
这是 pom.xml
依赖项:
<dependencies>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-server</artifactId>
<version>9.4.8.v20171121</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-servlet</artifactId>
<version>9.4.8.v20171121</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-jetty-http</artifactId>
<version>2.26</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-moxy</artifactId>
<version>2.26</version>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20180130</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.inject</groupId>
<artifactId>jersey-hk2</artifactId>
<version>2.26</version>
</dependency>
</dependencies>
我也尝试添加 jersey-server
、jersey-container-servlet-core
等依赖项。这是同样的错误。我在做什么可能是错误的?
从您的依赖项中删除 jetty-server
和 jetty-servlet
,它们将被您需要添加的 jersey-container-jetty-http
和 jersey-container-jetty-servlet
引入。这是您需要集成 jetty
和 jersey
:
的一些基本依赖项
<dependencies>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-jetty-http</artifactId>
<version>2.26</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-jetty-servlet</artifactId>
<version>2.26</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.inject</groupId>
<artifactId>jersey-hk2</artifactId>
<version>2.26</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-moxy</artifactId>
<version>2.26</version>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20180130</version>
</dependency>
</dependencies>
此外,在您的 App.java
中,您不需要执行 server.start()
。
public class App
{
public static void main(String[] args) {
URI baseUri = UriBuilder.fromUri("http://127.0.0.1/").port(8050).build();
ResourceConfig config = new ResourceConfig(RestController.class);
Server server = JettyHttpContainerFactory.createServer(baseUri, config);
}
}
当您执行 JettyHttpContainerFactory.createServer(baseUri, config)
时,它会使用给定的 config
创建一个容器,然后创建一个服务器并启动它。
我正在尝试使用 Jersey 和 Jetty 设置一个简单的服务器。但是 Jersey documentation 中的代码抛出此错误:
Exception in thread "main" java.lang.NoSuchFieldError: GIT_HASH
at org.eclipse.jetty.server.Server.doStart(Server.java:370)
at org.eclipse.jetty.util.component.AbstractLifeCycle.start(AbstractLifeCycle.java:68)
at org.glassfish.jersey.jetty.JettyHttpContainerFactory.createServer(JettyHttpContainerFactory.java:305)
at org.glassfish.jersey.jetty.JettyHttpContainerFactory.createServer(JettyHttpContainerFactory.java:135)
at com.zoho.dfs.App.main(App.java:17)
这是我的 App.java
class:
public class App
{
public static void main(String[] args) {
URI baseUri = UriBuilder.fromUri("http://127.0.0.1/").port(8050).build();
ResourceConfig config = new ResourceConfig(RestController.class);
Server server = JettyHttpContainerFactory.createServer(baseUri, config);
try {
server.start();
server.join();
} catch (Exception e) {
e.printStackTrace();
}
}
}
和RestController.class
(我想映射一个
@Path("/{username}")
public class RestController {
@GET
@Path("/home")
@Produces(MediaType.APPLICATION_JSON)
public Response read(@PathParam("username") String username) {
String name = "hello " + username;
return Response.status(200).entity(name).build();
}
}
这是 pom.xml
依赖项:
<dependencies>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-server</artifactId>
<version>9.4.8.v20171121</version>
</dependency>
<dependency>
<groupId>org.eclipse.jetty</groupId>
<artifactId>jetty-servlet</artifactId>
<version>9.4.8.v20171121</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-jetty-http</artifactId>
<version>2.26</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-moxy</artifactId>
<version>2.26</version>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20180130</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.inject</groupId>
<artifactId>jersey-hk2</artifactId>
<version>2.26</version>
</dependency>
</dependencies>
我也尝试添加 jersey-server
、jersey-container-servlet-core
等依赖项。这是同样的错误。我在做什么可能是错误的?
从您的依赖项中删除 jetty-server
和 jetty-servlet
,它们将被您需要添加的 jersey-container-jetty-http
和 jersey-container-jetty-servlet
引入。这是您需要集成 jetty
和 jersey
:
<dependencies>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-jetty-http</artifactId>
<version>2.26</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-jetty-servlet</artifactId>
<version>2.26</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.inject</groupId>
<artifactId>jersey-hk2</artifactId>
<version>2.26</version>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-moxy</artifactId>
<version>2.26</version>
</dependency>
<dependency>
<groupId>org.json</groupId>
<artifactId>json</artifactId>
<version>20180130</version>
</dependency>
</dependencies>
此外,在您的 App.java
中,您不需要执行 server.start()
。
public class App
{
public static void main(String[] args) {
URI baseUri = UriBuilder.fromUri("http://127.0.0.1/").port(8050).build();
ResourceConfig config = new ResourceConfig(RestController.class);
Server server = JettyHttpContainerFactory.createServer(baseUri, config);
}
}
当您执行 JettyHttpContainerFactory.createServer(baseUri, config)
时,它会使用给定的 config
创建一个容器,然后创建一个服务器并启动它。