是否可以定义一个与其实现分开的jax-rs服务接口(使用eclipse和jersey)? [英] Is it possible to define a jax-rs service interface separated from its implementation (with eclipse and jersey)?

查看:172
本文介绍了是否可以定义一个与其实现分开的jax-rs服务接口(使用eclipse和jersey)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道标题是否令人困惑,但让我说我有这个界面:

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);

}

为什么当我尝试实现Eclipse重写注释时重写的方法但不适用于类?

Why when I try to implement a version Eclipse rewrites annotation for the overridden method but not for the class?

class UserServiceImpl implements UserService {

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

}

我试图创建一个标准restful Web服务的定义,然后具有不同的实现。用标准的jax-rs可以这样吗?我是否有机会使用错误的注释?

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?

推荐答案

只有在不使用 any时才能使用注释继承 jax-rs 实现类的注释:它在JSR-339的第3.6节中说明。

You can use annotation inheritance only if you don't use any jax-rs annotation on the implementing class: it is stated on section 3.6 of JSR-339.

您重新定义 @Path @Produces 方法但不适用于类。

You redefine @Path and @Produces for the method but not for the class.

因此代码中的 Path 注释应该在具体的类上:

So the Path annotation in your code should be on the concrete class:

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,规范鼓励我们复制具体类上的注释:

BTW, the specification encourages us to replicate the annotations on the concrete classes:


为了与其他Java EE规范保持一致,建议始终重复注释而不是
依赖注释继承。

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

这篇关于是否可以定义一个与其实现分开的jax-rs服务接口(使用eclipse和jersey)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆