Creating a ResourceConfig that behaves the same way as default Jetty's Jersey registering Creating a ResourceConfig that behaves the same way as default Jetty's Jersey registering json json

Creating a ResourceConfig that behaves the same way as default Jetty's Jersey registering


I don't know how you got this to work

ServletHolder holder = new ServletHolder(new ServletContainer());

I could not produce a working example simply instantiating the ServletContainer(). Though I was about to get it to work with the following code

public class TestJerseyServer {    public static void main(String[] args) throws Exception {        ResourceConfig config = new ResourceConfig();        config.packages("jetty.practice.resources");        ServletHolder jerseyServlet                         = new ServletHolder(new ServletContainer(config));        Server server = new Server(8080);        ServletContextHandler context                 = new ServletContextHandler(server, "/");        context.addServlet(jerseyServlet, "/*");        server.start();        server.join();    }}

Using all your dependencies, excluding the com.sun.jersey:jersey-json, as it's not needed. No other configuration. The resource class

@Path("test")public class TestResource {    @GET    @Produces(MediaType.APPLICATION_JSON)    public Response getTest() {        Hello hello = new Hello();        hello.hello = "world";        return Response.ok(hello).build();    }    @POST    @Consumes(MediaType.APPLICATION_JSON)    public Response postHello(Hello hello) {        return Response.ok(hello.hello).build();    }    public static class Hello {        public String hello;    }}

in the jetty.practice.resources package.

I'm curious to see how you got it to work without the ResourceConfig


Another thing I should mention is that jersey-container-servlet-core should be switched out for jersey-container-servlet. The former is for 2.5 container support, but the latter is recommended for 3.x containers. It not have any effect though, with my example


cURL

C:\>curl http://localhost:8080/test -X POST -d "{\"hello\":\"world\"}" -H "Content-Type:application/json"

world

C:\>curl http://localhost:8080/test

{"hello":"world"}