Create a ResourceConfig that behaves the same as registering with the default Jetty Jersey

I have an endpoint with:

@POST @Path("/test") @Consumes(MediaType.APPLICATION_JSON) @Produces(MediaType.APPLICATION_JSON) public String canaryTest(String JSON) { return JSON; } 

When will I register it in Jetty using Jersey

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

everything is working fine. But in case I try to explicitly specify the default configuration, it stops working (returning a media type error from the endpoint). Even just passing the default ResourceConfig instance to ServletContainer, it stops working.

 ResourceConfig config = new ResourceConfig(); //config.property(x,defaultX) //config.property(y,defaultY) ServletHolder holder = new ServletHolder(new ServletContainer(config)); 

I would like to emulate the default configuration behavior manually and explicitly, so I ask here how can I configure ResourceConfig so that the behavior continues to work (that is, what properties to set)

PS: I am using Jetty 9.2.6.v20141205 and Jersey 2.14. Dependencies in Maven:

  • org.eclipse.jetty.jetty-server org.eclipse.jetty.jetty-servlet
  • org.eclipse.jetty.jetty servlets
  • org.glassfish.jersey.containers.jersey-container-servlet-core
  • com.sun.jersey.jersey-json
  • org.glassfish.jersey.media.jersey-media-json-jackson
+3
source share
1 answer

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"}

+3
source

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


All Articles