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.)
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....
}
}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With