gpt4 book ai didi

java - Jersey 2.26 - 启动服务器时出现 NoSuchFieldError

转载 作者:行者123 更新时间:2023-12-02 02:19:06 24 4
gpt4 key购买 nike

我正在尝试使用 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 类:

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-serverjersey-container-servlet-core等依赖项。这是同样的错误。我在做什么可能是错误的?

最佳答案

从依赖项中删除 jetty-serverjetty-servlet,它们将被 jersey-container-jetty-http 拉入,并且jersey-container-jetty-servlet,您需要添加它。以下是集成 jettyjersey 所需的一些基本依赖项:

<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创建一个容器,然后创建一个服务器并启动它。

关于java - Jersey 2.26 - 启动服务器时出现 NoSuchFieldError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48802924/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com