Publishing a REST Service Using Java SE

I am trying to find an easy way to publish my RESTful web service using JAX-RS 2.0 with Java SE using Jersey and / or the Java SE embedded HTTP server.

I want my dependencies to be minimal, so I wanted to avoid the grizzly bear and also did not want to use an external application server.

Can you tell me how to publish a leisure service with these requirements?

Thanks in advance,

I want to achieve something like this:

public static void main(String args[]) { try { final HttpServer server = GrizzlyHttpServerFactory.createHttpServer("http://localhost:8080/calculator/",new ResourceConfig(SumEndpoint.class)); System.in.read(); server.stop(); } catch (IOException ex) { } 

}

... but avoiding the grizzly addiction

+4
source share
1 answer

If you just depend on

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

you can start the server

 JdkHttpServerFactory.createHttpServer(URI.create("http://localhost:8090/root"), new MyApplication()); 

where MyApplication extends ResourceConfig to get a resource scan.

 @ApplicationPath("/") public class MyApplication extends ResourceConfig { public MyApplication() { packages("..."); } @GET @Produces("text/plain") public Response foo() { return Response.ok("Hey, it working!\n").build(); } } 

There may be a better way to control the server life cycle, but it eludes me at the moment.

+3
source

Source: https://habr.com/ru/post/1497792/


All Articles