JAX-RS @PathParam 注入类成员变量?

发布于 2024-10-17 10:48:46 字数 760 浏览 7 评论 0原文

我想做这样的事情:

@Stateless
@Path("/sensors/{sensorid}/version")
@Consumes({MediaType.APPLICATION_XML, MediaType.TEXT_XML})
@Produces({MediaType.APPLICATION_XML, MediaType.TEXT_XML})
public class SensorVersionRestView extends VersionRestView{

    @PathParam("sensorid")
    private String sensorid;

    @GET
    @Path("count")
     // so the complete path is i.e. 
     // domain.com/rs/sensors/111211/version/count
    public void getCount() {

        // do something with the sensorId....

    }
}

但我得到的唯一结果是运行时的 null (我使用 Glassfish v3 和 Jersey)。编译器和 Eclipse 从未提及成员类变量中的 @PathParam 问题。

我的构造有什么问题?

主要问题是,为什么我不想在此类中的每个方法上使用整个路径,存在另一个类处理传感器层上的一些休息操作(deomain.com/rs/sensors/count ie)

I want to do something like this:

@Stateless
@Path("/sensors/{sensorid}/version")
@Consumes({MediaType.APPLICATION_XML, MediaType.TEXT_XML})
@Produces({MediaType.APPLICATION_XML, MediaType.TEXT_XML})
public class SensorVersionRestView extends VersionRestView{

    @PathParam("sensorid")
    private String sensorid;

    @GET
    @Path("count")
     // so the complete path is i.e. 
     // domain.com/rs/sensors/111211/version/count
    public void getCount() {

        // do something with the sensorId....

    }
}

But the only thing I get is null on runtime (I use Glassfish v3 with Jersey). The compiler and eclipse never mentions a problem with the @PathParam at the member class variable.

What's wrong with my construct?

The main problem is, why I doesn't want to use the whole path on each method in this class, that there exists another class which handles some rest operations on the sensor layer (deomain.com/rs/sensors/count i.e.)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

゛时过境迁 2024-10-24 10:48:46

我相信你需要将其更改为:

@Stateless
@Path("/sensors/{sensorid}/version")
public class SensorVersionRestView extends VersionRestView {

@GET
@Path("count")
@Consumes({MediaType.APPLICATION_XML, MediaType.TEXT_XML})
@Produces({MediaType.APPLICATION_XML, MediaType.TEXT_XML})
 // domain.com/rs/sensors/111211/version/count
public void getCount(@PathParam("sensorid") String sensorid) {
    // do something with the sensorId....
}
}

I believe you need to change it to this:

@Stateless
@Path("/sensors/{sensorid}/version")
public class SensorVersionRestView extends VersionRestView {

@GET
@Path("count")
@Consumes({MediaType.APPLICATION_XML, MediaType.TEXT_XML})
@Produces({MediaType.APPLICATION_XML, MediaType.TEXT_XML})
 // domain.com/rs/sensors/111211/version/count
public void getCount(@PathParam("sensorid") String sensorid) {
    // do something with the sensorId....
}
}
苏大泽ㄣ 2024-10-24 10:48:46

因为注入发生在对象处
创建时间,使用该注解
关于资源类字段和 bean
属性仅支持
每个请求的默认资源类
生命周期。资源类使用
其他生命周期应该只使用这个
资源方法的注释
参数。 - JSR-311 Javadocs

您应该能够使用 < 来注释字段code>@PathParam 只要资源类生命周期是按请求的。默认情况下,根资源类的生命周期是针对每个请求的。

编辑:我认为您无法使用 EJB 来实现此目的。如果删除 @Stateless 注释,它应该可以工作。

Because injection occurs at object
creation time, use of this annotation
on resource class fields and bean
properties is only supported for the
default per-request resource class
lifecycle. Resource classes using
other lifecycles should only use this
annotation on resource method
parameters. - JSR-311 Javadocs

You should be able to annotate fields with @PathParam as long as the resource class lifecyle is per-request. By default the life-cycle of root resource classes is per-request.

EDIT: I don't think you can achieve this using EJBs. If you remove the @Stateless annotation, it should work.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文