加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

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

发布时间:2020-12-14 14:26:58 所属栏目:Java 来源:网络整理
导读:说我希望我的JAX-RS / Jersey应用程序公开以下URL: http://myapp.example.com/app/fizzhttp://myapp.example.com/app/buzzhttp://myapp.example.com/app/foohttp://myapp.example.com/app/bar 假设我希望/ app成为父基础资源,/ app / *成为“子”资源.以下
说我希望我的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
    }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读