I donβt know how you did it.
ServletHolder holder = new ServletHolder(new ServletContainer());
I could not create a working example by simply creating an instance of ServletContainer() . Although I was going to make it 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 except com.sun.jersey:jersey-json , as this is not necessary. No other configuration. 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 am interested to know how you could work without ResourceConfig
Another thing I should mention is that jersey-container-servlet-core should be disabled for jersey-container-servlet . The former is for supporting container 2.5, but the latter is recommended for 3.x containers. This has no effect though, with my example
curl up
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"}
source share