Tagged: cdi

injecting sub-resource directly


다른 방법도 많지만 sub-resource를 직접 주입할 수도 있나보다.

@Path("/parents");
public class ParentsResource {

    @GET
    @Path("/{parentId: \\d+}/children")
    public ChildrenResource readChidren(
        @PathParam("parentId") final long parentId) {
        return childrenResource.parentId(parentId);
    }

    @Inject
    private ChildResource childResource;
}
Advertisements

various ways to persist entities in jax-rs resources


references

via EJB

@Stateless
public class TestService {

    public Test persist(final Test test) {
        em.persist(test);
        return test;
    }

    @PersistenceContext(unitName = "pu")
    private EntityManager em;
}
@Path("/tests")
public class TestsResource {

    @POST
    @Path("/1")
    public void create1(final Test test) {
        ts.persist(test);
    }

    @EJB
    private TestService ts;
}

via PersistenceUnit

@POST
@Path("/2")
public void create2(final Test test) throws Exception {

    final EntityManager em = emf.createEntityManager();
    try {
        try {
            ut.begin();
            em.joinTransaction();
            em.persist(test);
            ut.commit();
        } catch (final Exception e) {
            ut.rollback();
            throw e;
        }
    } finally {
        em.close();
    }
}

@POST
@Path("/3")
public void create3(final Test test) throws Exception {

    try {
        ut.begin();
        final EntityManager em = emf.createEntityManager();
        try {
            em.persist(test);
        } finally {
            em.close();
        }
        ut.commit();
    } catch (final Exception e) {
        ut.rollback();
        throw e;
    }
}

@PersistenceUnit(unitName = "pu")
private transient EntityManagerFactory emf;

@Resource
private UserTransaction ut;

via PersistenceContext

@POST
@Path("/4")
public void create4(final Test test) throws Exception {

    try {
        ut.begin();
        em.joinTransaction();
        em.persist(test);
        ut.commit();
    } catch (final Exception e) {
        ut.rollback();
        throw e;
    }
}

@PersistenceContext(unitName = "pu")
private transient EntityManager em;

@Resource
private UserTransaction ut;

via PersistenceContext with Transactional Interceptors

@POST
@Path("/5")
@Transactional(rollbackOn = Exception.class)
public void create5(final Test Test) throws Exception {

    em.persist(test);
}

@PersistenceContext(unitName = "pu")
private transient EntityManager em;

Unit-Testing JAX-RS with Jersey Test Framework and Weld


요 근래 JAX-RS에 기반한 라이브러리를 하나 만들던 중 단위 테스트에 문제가 생겨서 SO에 이것 저것 물어봤다.

References

JAX-RS

만들고 있는 라이브러리는 JAX-RS와 DI만 사용한다.

<dependency>
  <groupId>javax.inject</groupId>
  <artifactId>javax.inject</artifactId>
  <scope>provided</scope>
</dependency>
<dependency>
  <groupId>javax.ws.rs</groupId>
  <artifactId>javax.ws.rs-api</artifactId>
  <scope>provided</scope>
</dependency>

Jersey Test Framework

단위 시험을 하기위해 다음과 같이 Jersey Test Framework을 추가하였다.

<dependency>
  <groupId>org.glassfish.jersey.test-framework.providers</groupId>
  <artifactId>jersey-test-framework-provider-grizzly2</artifactId>
  <scope>test</scope>
</dependency>

Weld

Jersey가 사용하고 있는 HK2(JSR-330)은 주입(injection)은 잘 수행하는 듯 하나 JSR-250에 해당하는 @PostConstruct 등은 수행하지 못한다. 다음과 같이 dependency를 추가했다.

<dependency>
  <groupId>org.glassfish.jersey.ext.cdi</groupId>
  <artifactId>jersey-weld2-se</artifactId>
  <scope>test</scope>
</dependency>

그리고 다음과 같이 Weld를 적용시켰다.

@Override
protected Application configure() {

    final Weld weld = new Weld();
    weld.initialize();
    Runtime.getRuntime().addShutdownHook(new Thread(() -> weld.shutdown()));

    // ...
}