http://myapp.example.com/app/fizz http://myapp.example.com/app/buzz http://myapp.example.com/app/foo http://myapp.example.com/app/bar
假设我希望/ app成为父基础资源,/ app / *成为“子”资源.以下是否会完成我正在寻找的URL策略(?):
@Path('/app') @Produces(MediaType.APPLICATION_JSON) public abstract class AppResource { // Whatever... } @Path('/fizz') // <--- right here,will FizzResource live at /app/fizz? @Produces(MediaType.APPLICATION_JSON) public class FizzResource extends AppResource { // Whatever... }
FizzResource会暴露在/ app / fizz还是/ fizz?
解决方法
FizzResource将暴露在/ fizz.
答案很长
引用JSR 339(关于注释继承的第3.6节):
If a subclass or implementation method has any JAX-RS annotations then
all of the annotations on the superclass or interface method are
ignored.
For consistency with other Java EE specifications,it is recommended to always repeat annotations instead of relying on annotation inheritance.
创建子资源
JAX-RS/Jersey documentation解释了如何创建子资源:
07003 may be used on classes and such classes are referred to as root resource classes.
07003 may also be used on methods of root resource classes. This enables common functionality for a number of resources to be grouped together and potentially reused.
The first way 07003 may be used is on resource methods and such methods are referred to as sub-resource methods.
因此,请执行以下操作来创建子资源:
@Path("/app") public class YourHandler { @Produces(MediaType.APPLICATION_JSON) public String yourHandlerForApp() { // This method is be exposed at /app } @Path("/fizz") @Produces(MediaType.APPLICATION_JSON) public String yourHandlerForAppSlashFizz() { // This method is be exposed at /app/fizz } }