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, f.x. Spring MVC or Jersey
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句