3 Replies Latest reply on Oct 6, 2008 11:31 AM by markeger

    How to build a RestEasy-Sub-Resource as Seam-Component ?

    markeger

      Hi,


      I'am trying to get the Seam-Injection-Support in a RestEasy-Sub-Resource, but cant get it work.
      I wrote a rest-resource as a seam-component wich provide a Path to the sub-resource TestsResource:


      @Path("/sample/")
      @Name("sampleResource")
      public class SampleResource {
      
          @In 
          private EntityManager entityManager;
          @In(create=true)
          private TestsResource testsResource;
          
          @Path("tests/")
          public TestsResource getTestsResource(){   
              return testsResource;    <---- ????
          }
      ...


      It seems as I can't return the injected 'testsResource' because it produce a NullPointerException
      at org.resteasy.ResourceLocator.invokeOnTargetObject(ResourceLocator.java:149)
      ....



      The Sub-Resource looks like this:


      @Name("testsResource")
      public class TestsResource {
      
          @In
          private EntityManager entityManager;
          @In(create=true)
          private TestHome testHome;
      
          @GET
          @ProduceMime({"application/xml", "application/json"})
          public TestsConverter get(@Context UriInfo uriInfo) {
              return new TestsConverter(getEntities(), uriInfo.getAbsolutePath());
          }
      
          @POST
          @ConsumeMime({"application/xml", "application/json"})
          public Response post(TestConverter data, @Context UriInfo uriInfo) {
      
              Test entity = data.getEntity();
              testHome.setInstance(entity);
              testHome.persist();
      
              return Response.created(uriInfo.getAbsolutePath().resolve(entity.getUsrrid() + "/")).build();
          }




      Of course, if the sub-resource is just a normal resource (not a seam-component), I can instantiate the class with 'new TestsResource(...)' and it works. But I need the seam-component 'TestHome extends EntityHome' in the sub-resource, to persist my entity and I can't use injection in a non-seam-component. So I have to inject 'TestHome' in 'SampleResource' and pass it to the sub-resource:


          @Path("tests/")
          public TestsResource getTestsResource(){
              return new TestsResource(entityManager, testHome);
          }




      Anybody knows a better way to handle this?