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

How to solve java.lang.IllegalStateException: server in wrong state Exception

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

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.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 服务器以及启动服务器。

"server in wrong state Exception" 当您尝试再次启动服务器时出现。