4

I have an endpoint with:

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

When I register it in Jetty using Jersey

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

everything seems to work fine. But in case I try to specify explictly the default config, it stops working (returning a media type error from the endpoint). Even by just passing a default instance of a ResourceConfig to the ServletContainer, it stops working.

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

I'd like to emulate the default configuration behavior manually and explicitly, so what I am asking here is how should I configure ResourceConfig so the behavior keeps working (i.e, what properties to set)

P.S: i'm 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
Whimusical
  • 6,401
  • 11
  • 62
  • 105

1 Answers1

8

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

Paul Samsotha
  • 205,037
  • 37
  • 486
  • 720
  • Could you try it with POST and @Consumes(JSON)? I don't know how I do it but sure org.eclipse.jetty.servlet.ServletHolder holder = new org.eclipse.jetty.servlet.ServletHolder(new org.glassfish.jersey.servlet.ServletContainer()) is compiling and starting the server for me :). Perhaps is that the problem, internally? – Whimusical Jan 15 '15 at 16:40
  • 1) Yes I'll try. 2) Starting the server, but are you able to hit your resources? – Paul Samsotha Jan 15 '15 at 16:44
  • Interesting. You're right it doesn't work. Let me look into it – Paul Samsotha Jan 15 '15 at 16:49
  • Sorry, I take that back. I had a typeo on the `Content-Type` header :-D But it works fine with post also – Paul Samsotha Jan 15 '15 at 16:57
  • One way I could see it working in your case is if you have a webapp structure, as mention [here](https://jersey.java.net/apidocs/2.8/jersey/org/glassfish/jersey/servlet/ServletContainer.html), it will have some initial configurations on the implicitly created `ResourceConfig`. But in a standalone structure, I don't see how those rules would apply. That's what has me baffled. That's why I said I am curious to see your code/configuration/structure or whatever to help me understand (more for my benefit, than yours :-) – Paul Samsotha Jan 15 '15 at 17:05
  • My code is pure vainilla, just maven dependencies added and simple code in main and endpoint classes :). No XML config – Whimusical Jan 15 '15 at 17:40