java – 是否可能定义一个jax-rs服务接口,与其实现分离(使用eclipse和jersey)?

我不知道标题是否令人困惑,但让我们说我有这个界面:
@Produces(MediaType.APPLICATION_JSON)
@Path("/user")
public interface UserService {

    @GET
    @Path("/{userId}")
    public Response getUser(@PathParam("userId") Long userId);

}

为什么当我尝试实现一个版本Eclipse重写注释的覆盖方法,但不是为类?

class UserServiceImpl implements UserService {

    @Override
    @GET
    @Path("/{userId}")
    public Response getUser(@PathParam("userId") Long userId) {
        // Todo Auto-generated method stub
        return null;
    }

}

我正在尝试为休息的Web服务创建一个标准定义,然后具有不同的实现.是否可以使用标准jax-rs?我有任何机会使用错误的注释吗?

解决方法

只有在实现类上不使用任何jax-rs注释时,才可以使用注释继承:它在JSR-339的第3.6节中说明.

您为方法重新定义@Path和@Produces,但不为该类定义.

所以你的代码中的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,规范鼓励我们复制具体类的注释:

For consistency with other Java EE specifications,it is recommended to always repeat annotations instead of relying on annotation inheritance.

相关文章

最近看了一下学习资料,感觉进制转换其实还是挺有意思的,尤...
/*HashSet 基本操作 * --set:元素是无序的,存入和取出顺序不...
/*list 基本操作 * * List a=new List(); * 增 * a.add(inde...
/* * 内部类 * */ 1 class OutClass{ 2 //定义外部类的成员变...
集合的操作Iterator、Collection、Set和HashSet关系Iterator...
接口中常量的修饰关键字:public,static,final(常量)函数...