JAX-RS @PathParam for entering a class variable?

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 ie // domain.com/rs/sensors/111211/version/count public void getCount() { // do something with the sensorId.... } } 

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

What happened to my design?

The main problem is why I don’t want to use all the way for each method in this class, that there is another class that handles some rest operations at the touch level (deomain.com/rs/sensors/count that is)

+4
source share
2 answers

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.... } } 
+6
source

Since injection takes place at the creation time object, the use of this annotation over the fields of the resource class and beans properties is only supported for the default resource class for each life cycle request. Resource classes that use other lifecycles should use this annotation using the resource method parameters. - JSR-311 Javadocs

You should be able to comment on fields using @PathParam if a resource of the lifecyle class is intended for each request. By default, the life cycle of the root resource classes runs on request.

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

+6
source

Source: https://habr.com/ru/post/1339637/


All Articles