Rafael
Rafael

Reputation: 2676

Publish REST service with Java SE

I am trying to find a simple way to publish my RESTful web service using JAX-RS 2.0 with Java SE using Jersey and/or the Java SE build-in http server.

I want to keep my dependencies to the minimum so i wanted to avoid grizzly and also do not want to use any external application server.

Can you point me how to publish a rest service with this requirements?

Thanks in advance,

I mean to achieve somethig 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 dependency

Upvotes: 5

Views: 6214

Answers (1)

forty-two
forty-two

Reputation: 12817

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 then start the server

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

where MyApplication extends ResourceConfig to obtain resource scanning.

@ApplicationPath("/")
public class MyApplication extends ResourceConfig {

    public MyApplication() {
        packages("...");
    }
    @GET
    @Produces("text/plain")
    public Response foo() {

        return Response.ok("Hey, it's working!\n").build();
    }
}

There may a better way to control the server life cycle, but that eludes me for the moment.

Upvotes: 6

Related Questions