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

前端之家收集整理的这篇文章主要介绍了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.

原文链接:https://www.f2er.com/java/125861.html

猜你在找的Java相关文章