java 是否可以定义一个与其实现分离的 jax-rs 服务接口(使用 eclipse 和 jersey)?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16950873/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Is it possible to define a jax-rs service interface separated from its implementation (with eclipse and jersey)?
提问by dierre
I don't know if the title is confusing, but let's say I have this interface:
我不知道标题是否令人困惑,但假设我有这个界面:
@Produces(MediaType.APPLICATION_JSON)
@Path("/user")
public interface UserService {
@GET
@Path("/{userId}")
public Response getUser(@PathParam("userId") Long userId);
}
Why when I try to implement a version Eclipse rewrites annotation for the overridden method but not for the class?
为什么当我尝试实现某个版本时,Eclipse 会为重写的方法而不是为类重写注释?
class UserServiceImpl implements UserService {
@Override
@GET
@Path("/{userId}")
public Response getUser(@PathParam("userId") Long userId) {
// TODO Auto-generated method stub
return null;
}
}
I was trying to create a standard definition for the restful web service and then having different implementations. Is something like this possible with standard jax-rs? Am I using wrong annotations by any chance?
我试图为 restful web 服务创建一个标准定义,然后有不同的实现。使用标准的 jax-rs 可以实现这样的事情吗?我是否有机会使用错误的注释?
回答by Carlo Pellegrini
You can use annotation inheritance only if you don't use anyjax-rs
annotation on the implementing class: it is stated on section 3.6 of JSR-339.
只有在实现类上不使用任何jax-rs
注解时,才能使用注解继承:在 JSR-339 的第 3.6 节中有说明。
You redefine @Path
and @Produces
for the method but not for the class.
您重新定义 @Path
和@Produces
为方法而不是为类。
So the Path
annotation in your code should be on the concrete class:
所以Path
你的代码中的注解应该在具体的类上:
public interface UserService {
@GET
@Path("/{userId}")
@Produces(MediaType.APPLICATION_JSON)
public Response getUser(@PathParam("userId") Long userId);
}
@Path("/user")
class UserServiceImpl implements UserService {
@Override
@GET
@Path("/{userId}")
@Produces(MediaType.APPLICATION_JSON)
public Response getUser(@PathParam("userId") Long userId) {
// TODO Auto-generated method stub
return null;
}
}
BTW, the specification encourages us to replicate the annotations on the concrete classes:
顺便说一句,规范鼓励我们在具体类上复制注释:
For consistency with other Java EE specifications, it is recommended to always repeat annotations instead of relying on annotation inheritance.
为了与其他 Java EE 规范保持一致,建议始终重复注解,而不是依赖注解继承。