IBM Support

WebSphere Application Server: Accessing JAX-RS resources via the Spring resource injection might not work

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 spring-integration-context.xml file, the following code is used to define the resource injection:

<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);

[{"Product":{"code":"SSEQTP","label":"WebSphere Application Server"},"Business Unit":{"code":"BU053","label":"Cloud & Data Platform"},"Component":"Not Applicable","Platform":[{"code":"PF002","label":"AIX"},{"code":"PF010","label":"HP-UX"},{"code":"PF012","label":"IBM i"},{"code":"PF016","label":"Linux"},{"code":"PF014","label":"iOS"},{"code":"PF027","label":"Solaris"},{"code":"PF033","label":"Windows"},{"code":"PF035","label":"z\/OS"}],"Version":"8.5.5","Edition":"Base;Express;Liberty;Network Deployment","Line of Business":{"code":"LOB45","label":"Automation"}}]

Document Information

Modified date:
06 June 2019

UID

swg21636854