开发者

JAX-RS @PathParam to inject in class member variable?

开发者 https://www.devze.com 2023-02-10 03:56 出处:网络
I want to do something like this: @Stateless @Path(\"/sensors/{sensorid}/version\") @Consumes({MediaType.APPLICATION_XML, MediaType.TEXT_XML})

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 SensorVersionRe开发者_JS百科stView 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.

0

精彩评论

暂无评论...
验证码 换一张
取 消