简体   繁体   中英

REST resources with embedded Jetty server

I am using an embedded Jetty server to create a POC for a REST service.

I want to create the following resources;

/foo/{id}
/foo/{id}/bar

I require each to call different classes, each of which access the id within the path.

I have the following code;

        Server server = new Server(7777);
        ServletHandler handler = new ServletHandler();
        server.setHandler(handler);

        handler.addServletWithMapping(DefaultServlet.class, "/*");
        handler.addServletWithMapping(Foo.class, "/foo/*");
        handler.addServletWithMapping(Bar.class, "/foo/*/bar");

Unfortunately, both requests result in Foo.class.

Does Jetty allow me to achieve this? Can I set a more intelligent path?

I believe it's not possible. It looks like jetty follows Java Servlet Specification , then the asterisk can be either at the beginning of the path or at the end.

Here is quotation from the spec:

  • A string beginning with a '*.' prefix is used as an extension mapping
  • A string beginning with a '/' character and ending with a '/*' suffix is used for path mapping.

See "12.2 Specification of Mappings" for more details.

So you should analyse request.getPathInfo() in your Foo class or use some framework that can do it for you, fx Spring MVC or Jersey

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM