gpt4 book ai didi

java - 如何解决java.lang.IllegalStateException : server in wrong state Exception

转载 作者:搜寻专家 更新时间:2023-11-01 01:25:26 30 4
gpt4 key购买 nike

我正在尝试在核心 Java 项目(swing 应用程序)中部署 restful web 服务。我为此使用了 jersy。我在谷歌中搜索了很多网站,但我找不到为什么要追加。

public class Main { 
public static void main(String[] args) throws Exception{
ResourceConfig resourceConfig = new ResourceConfig(MyResource.class);
URI baseUri = UriBuilder.fromUri("http://localhost/").port(10049).build();

HttpServer server = JdkHttpServerFactory.createHttpServer(baseUri,resourceConfig);
server.start();
System.out.println("Press Enter to stop the server. ");
System.in.read();
server.stop(0);
}
}


@Path("/hello")
public class MyResource {

// This method is called if TEXT_PLAIN is request
@GET
@Produces(MediaType.TEXT_PLAIN)
public String sayPlainTextHello() {
return "Hello Jersey";
}

// This method is called if XML is request
@GET
@Produces(MediaType.TEXT_XML)
public String sayXMLHello() {
return "sdasd";
}

// This method is called if HTML is request
@GET
@Produces(MediaType.TEXT_HTML)
public String sayHtmlHello() {
return "dsdfd";
}
}

异常:

 Exception in thread "main" java.lang.IllegalStateException: server in wrong state
at sun.net.httpserver.ServerImpl.start(ServerImpl.java:139)
at sun.net.httpserver.HttpServerImpl.start(HttpServerImpl.java:58)
at org.glassfish.jersey.jdkhttp.JdkHttpServerFactory$2.start(JdkHttpServerFactory.java:363)

专家:

<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-jdk-http</artifactId>
<version>2.22.1</version>
</dependency>

如何解决上述异常。

最佳答案

此问题已通过删除 server.start();

行得到解决

JdkHttpServerFactory.createHttpServer(baseUri,resourceConfig); 正在创建 http 服务器以及启动服务器。

当您尝试再次启动服务器时出现“服务器处于错误状态异常”。

关于java - 如何解决java.lang.IllegalStateException : server in wrong state Exception,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35053786/

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