JAX-RS @PathParam 注入类成员变量?
我想做这样的事情:
@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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我相信你需要将其更改为:
I believe you need to change it to this:
您应该能够使用 < 来注释字段code>@PathParam 只要资源类生命周期是按请求的。默认情况下,根资源类的生命周期是针对每个请求的。
编辑:我认为您无法使用 EJB 来实现此目的。如果删除
@Stateless
注释,它应该可以工作。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.