Troubleshooting
Problem
When using Java API for RESTful Web Services (JAX-RS) to access resources via Spring resource injection, the resource might not be accessible and a NullPointerException error might occur.
Symptom
NullPointerException error
Cause
In the
<bean id="springMongoDirectDAOImpl" class="com.ibm.spring.certification.direct.dao.impl.ContactDocImpl">
<property name="template" ref="mongoTemplate" />
</bean>
<bean id="contactorRestService" class="com.ibm.spring.certification.web.service.rest.ContactRestService">
<property name="service" ref="springMongoDirectDAOImpl" />
</bean>
While in the JAX-RS class ContactRestService
, a service variable is defined as follows to use the resource:
public class ContactRestService {
private ContactDoc service;
...
}
When you use the ContactRestService
class as resource injection to access the service object, a NullPointerException error might occur.
Resolving The Problem
You can update the ContactRestService
class as follows to retrieve the service object:
ClassPathXmlApplicationContext context = new ClassPathXmlApplicationContext("/META-INF/spring/integration/spring-integration-context.xml", ContactRestService.class);
ContactDoc service = context.getBean("springMongoDirectDAOImpl",ContactDocImpl.class);
Related Information
Was this topic helpful?
Document Information
Modified date:
06 June 2019
UID
swg21636854