I am trying to deploy a quiet web service in a main Java project (swing application). I am using jersy for this. I searched a lot of sites on google, but I can not find the reason for this addition.
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 {
@GET
@Produces(MediaType.TEXT_PLAIN)
public String sayPlainTextHello() {
return "Hello Jersey";
}
@GET
@Produces(MediaType.TEXT_XML)
public String sayXMLHello() {
return "sdasd";
}
@GET
@Produces(MediaType.TEXT_HTML)
public String sayHtmlHello() {
return "dsdfd";
}
}
An exception:
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>
How to resolve the above exception.
source
share