java – JAX-RS / Jersey资源路径是否值得继承?

前端之家收集整理的这篇文章主要介绍了java – JAX-RS / Jersey资源路径是否值得继承?前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
说我希望我的JAX-RS / Jersey应用程序公开以下URL:
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.

specification还说:

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
    }
}
原文链接:https://www.f2er.com/java/126121.html

猜你在找的Java相关文章