hte_guy hte_guy - 3 months ago 10
reST (reStructuredText) Question

NPE in my service class of rest using jersey with spring

I am new to spring and rest i am still learning it. I have a problem while calling the resource from my rest api through a client.

I have some data in my database and i am using spring to inject the values and get connection.

When i am making a GET request from the client i am getting a NPE.

Below is my DAO Class

public interface CustomerDao {

public List<Customer> getCustomersDao();

}


DAOImpl

public class CustomerDaoImpl implements CustomerDao {

private JdbcTemplate template;

@Autowired
public void setTemplate(JdbcTemplate template) {
this.template = template;
}

public List<Customer> getCustomersDao() {
return template.query("SELECT * FROM books.customers", new RowMapper<Customer>() {
@Override
public Customer mapRow(ResultSet rs, int rownumber) throws SQLException {
Customer e = new Customer();
e.setId(rs.getString(1));
e.setName(rs.getString(2));
e.setAge(rs.getString(3));
return e;
}
});
}


Service class

public class CustomerService {

@Autowired
CustomerDao customersConnection;

public List<Customer> getAllCustomers() {

return new ArrayList<Customer>(customersConnection.getCustomersDao());

}


Resource

@Path("/persons")
public class CustomerResource {

@Autowired
CustomerService customerService;

@GET
@Produces(MediaType.APPLICATION_JSON)
public List<Customer> getAllCustomers {

return customerService.getAllCustomers();

}


}

My bean.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xmlns:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-4.0.xsd">

<context:component-scan base-package="com.sumanth.customers" />

<bean id="ds" class="org.springframework.jdbc.datasource.DriverManagerDataSource">
<property name="driverClassName" value="com.mysql.jdbc.Driver" />
<property name="url" value="--:--://----/---" />
<property name="username" value="----" />
<property name="password" value="-----" />
</bean>

<bean id="jdbcTemplate" class="org.springframework.jdbc.core.JdbcTemplate">
<property name="dataSource" ref="ds"></property>
</bean>

<bean id="customerService" class="com.sumanth.customer.db.CustomerDaoImpl">
<property name="template" ref="jdbcTemplate"></property>
</bean>

</beans>


Web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">

<servlet>

<servlet-name>Jersey Web Application</servlet-name>

<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>

<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>com.sumanth.customer</param-value>
</init-param>

<init-param>
<param-name>com.sun.jersey.api.json.POJOMappingFeature</param-name>
<param-value>true</param-value>
</init-param>

<load-on-startup>1</load-on-startup>

</servlet>

<servlet-mapping>
<servlet-name>Jersey Web Application</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>

</web-app>


Error:

Caused by: java.lang.NullPointerException
at com.sumanth.customer.resource.CustomerResource.getAllCustomers(CustomerResource.java:23)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
at java.lang.reflect.Method.invoke(Unknown Source)


Error line 23 :

return customerService.getAllCustomers();


I used the @Autowired to wire the beans.I know that i did a mistake while wiring them can anybody please help me out

Thank you

Answer

I have made some modifications to the web xml and my bean.xml

<listener>
    <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class>
</listener>
<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>WEB-INF/bean.xml</param-value>
</context-param>

Modified my service class

   @Component annotation in my service class

My bean.xml added the below

<context:annotation-config />

My Impl class.I have added the annotation above my class

@Repository("customerService")
Comments