NPE 在我的服役 class 中使用球衣 spring

NPE in my service class of rest using jersey with spring

我是 spring 的新手,其余的我还在学习中。我在通过客户端从休息 api 调用资源时遇到问题。

我的数据库中有一些数据,我正在使用 spring 注入值并获得连接。

当我从客户端发出 GET 请求时,我收到了 NPE。

下面是我的 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;
        }
    });
}

服务class

public class CustomerService {

@Autowired
CustomerDao customersConnection;

public List<Customer> getAllCustomers() {

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

}

资源

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

@Autowired
CustomerService customerService;

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

    return customerService.getAllCustomers();

}

}

我的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> 

错误:

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)

第 23 行错误:

return customerService.getAllCustomers();

我使用@Autowired 连接 beans.I 知道我在连接它们时犯了一个错误 任何人都可以帮助我吗

谢谢

我对网络 xml 和我的 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>

修改了我的服务class

   @Component annotation in my service class

我的bean.xml添加了以下内容

<context:annotation-config />

我的 Impl class.I 在我的 class

上方添加了注释
@Repository("customerService")