2016-08-18 33 views
1

我是新来的春天和休息我仍然在学习它。通过客户端从我的其余api调用资源时遇到问题。NPE在我的服务类的休息与春季运动衫使用

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

当我从客户端获取GET请求时,我正在获得NPE。

下面是我的DAO类

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

服务类

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知道,我做了一个错误,而它们连接任何人可以请帮我

谢谢

+0

是它getBooks或getAllCustomers上面的注释? – user641887

+0

编辑它是getAllCustomers –

+0

我认为你使用球衣+ spring.Use @InjectParam而不是Autowire并检查 – Vaibs

回答

0

我已经做了一些修改网页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> 

修改了服务类

@Component annotation in my service class 

我bean.xml添加以下

<context:annotation-config /> 

我默认地将Impl class.I增加了我的课

@Repository("customerService")