Home  >  Article  >  Java  >  How to Inject EJBs into JAX-RS Web Services and Avoid NullPointerExceptions?

How to Inject EJBs into JAX-RS Web Services and Avoid NullPointerExceptions?

Susan Sarandon
Susan SarandonOriginal
2024-11-07 15:46:02850browse

How to Inject EJBs into JAX-RS Web Services and Avoid NullPointerExceptions?

Injection of EJBs into JAX-RS Web Services

In an effort to integrate JAX-RS web services with EJBs via annotations, a common issue arises when the injected EJB remains null, leading to a NullPointerException. To address this problem, we need to further explore the options available.

Option 1: Utilize the Injection Provider SPI

An effective approach involves implementing a provider that performs the necessary lookup and injection of the EJB. This approach leverages the injection provider Service Provider Interface (SPI).

Option 2: Design the BookResource as an EJB

Alternatively, the BookResource itself can be designed as an EJB, providing a viable solution for integrating EJBs into the web service.

Option 3: Employ CDI (Contexts and Dependency Injection)

CDI offers another option for injection by incorporating the @Inject annotation. This approach aligns well with CDI's principles of managing dependencies and providing dependency injection capabilities.

For more in-depth exploration, refer to the following resources:

  • [EJB Injection](https://docs.oracle.com/javaee/7/api/javax/ejb/EJB.html)
  • [How to Combine REST Services with EJB 3.1](https://jaxenter.com/how-to-combine-rest-services-with-ejb-3-1-58834.html)
  • [EJB 3.1 And REST - The Lightweight Hybrid](https://www.infoq.com/articles/ejb31-rest-lightweight-hybrid)
  • [Injecting an EJB from a jar into a jax-rs class in a war](https://stackoverflow.com/questions/15770910/injecting-an-ejb-from-a-jar-into-a-jax-rs-class-in-a-war)

The above is the detailed content of How to Inject EJBs into JAX-RS Web Services and Avoid NullPointerExceptions?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn