繁体   English   中英

如何解决java.lang.IllegalStateException:服务器处于错误状态异常

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

我正在尝试在核心java项目(swing应用程序)中部署restful web-service。 我正在使用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)

Maven的:

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

如何解决上述异常。

通过删除行server.start();解决了此问题

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

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM