gpt4 book ai didi

java - 在 IntelliJ 中运行与从 JAR 文件运行之间的区别

转载 作者:行者123 更新时间:2023-12-01 09:04:31 26 4
gpt4 key购买 nike

我在 Grizzly 服务器上为 Jersey 应用程序运行以下简单示例:

@Path("example")
public class example {
@POST
public Response someMethod(@Context Request r) {
System.out.println("Received request at : " + LocalDateTime.now());
Response response = Response.ok().build();
return response;
}
}


public class GrizzlyServerMain {

public static HttpServer startServer(String BASE_URI) throws IOException {

// Scans for JAX-RS resources and providers in the specified code-package
final ResourceConfig resourceConfig = new PackagesResourceConfig("Application");

// create and start a new instance of grizzly http server exposing the Jersey application at BASE_URI
return GrizzlyServerFactory.createHttpServer(BASE_URI, resourceConfig);
}

public static void main(String[] args) throws IOException {
String BASE_URI = "http://0.0.0.0:8000";

// Base URI the Grizzly HTTP server will listen on
startServer(BASE_URI);

//noinspection ResultOfMethodCallIgnored
System.in.read();
}
}

当我从 IntelliJ 运行它时,一切都按预期运行。

Connected to the target VM, address: '127.0.0.1:61163', transport: 'socket' Dec 30, 2016 4:00:07 PM com.sun.jersey.api.core.PackagesResourceConfig init INFO: Scanning for root resource and provider classes in the packages: Application Dec 30, 2016 4:00:08 PM com.sun.jersey.api.core.ScanningResourceConfig logClasses INFO: Root resource classes found: class Application.Greetings class Application.example Dec 30, 2016 4:00:08 PM com.sun.jersey.api.core.ScanningResourceConfig init INFO: No provider classes found. Dec 30, 2016 4:00:08 PM com.sun.jersey.server.impl.application.WebApplicationImpl _initiate INFO: Initiating Jersey application, version 'Jersey: 1.19.3 10/24/2016 03:43 PM' Dec 30, 2016 4:00:08 PM org.glassfish.grizzly.http.server.NetworkListener start INFO: Started listener bound to [0.0.0.0:8000] Dec 30, 2016 4:00:08 PM org.glassfish.grizzly.http.server.HttpServer start INFO: [HttpServer] Started Received request at : 2016-12-30T16:00:13.097 Received request at : 2016-12-30T16:00:14.319 Received request at : 2016-12-30T16:00:15.583 Disconnected from the target VM, address: '127.0.0.1:61163', transport: 'socket'

但是当我创建一个包含所有依赖项的 JAR 时,一旦我从客户端发送请求,我就会得到:

WARNING: Exception during FilterChain execution java.lang.IllegalStateException at org.glassfish.grizzly.http.server.io.OutputBuffer.reset(OutputBuffer.java:217) at org.glassfish.grizzly.http.server.Response.reset(Response.java:732) at org.glassfish.grizzly.http.server.HttpServerFilter.handleRead(HttpServerFilter.java:168) at org.glassfish.grizzly.filterchain.ExecutorResolver$9.execute(ExecutorResolver.java:119) at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeFilter(DefaultFilterChain.java:265) at org.glassfish.grizzly.filterchain.DefaultFilterChain.executeChainPart(DefaultFilterChain.java:200) at org.glassfish.grizzly.filterchain.DefaultFilterChain.execute(DefaultFilterChain.java:134) at org.glassfish.grizzly.filterchain.DefaultFilterChain.process(DefaultFilterChain.java:112) at org.glassfish.grizzly.ProcessorExecutor.execute(ProcessorExecutor.java:78) at org.glassfish.grizzly.nio.transport.TCPNIOTransport.fireIOEvent(TCPNIOTransport.java:815) at org.glassfish.grizzly.strategies.AbstractIOStrategy.fireIOEvent(AbstractIOStrategy.java:112) at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.run0(WorkerThreadIOStrategy.java:115) at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy.access$100(WorkerThreadIOStrategy.java:55) at org.glassfish.grizzly.strategies.WorkerThreadIOStrategy$WorkerThreadRunnable.run(WorkerThreadIOStrategy.java:135) at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.doWork(AbstractThreadPool.java:567) at org.glassfish.grizzly.threadpool.AbstractThreadPool$Worker.run(AbstractThreadPool.java:547) at java.lang.Thread.run(Unknown Source)

你能告诉我我做错了什么吗?

最佳答案

异常似乎已从 IllegalStateException 更改为 IllegalArgumentException。

查看 Java 文档 ContainerProvider 。ContainerProvider 是一个 SPI 类,其实现将由您正在使用的服务器提供,在您的例子中是 Grizzly 服务器。根据 Java 文档:
实现(服务提供者)通过在资源中放置提供者配置文件(如果尚不存在)“com.sun.jersey.spi.container.ContainerProvider”来标识自身目录 META-INF/services,并在文件中包含实现的完全限定的服务提供者类。

所以,我认为您需要将“META-INF/services”更新到您的 jar 并将提供程序添加为“GrizzlyContainerProvider”(包限定)。

您可以看看类似的帖子: Grizzly and Jersey standalone jar

关于java - 在 IntelliJ 中运行与从 JAR 文件运行之间的区别,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41398075/

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