簡體   English   中英

NPE在我的服務類休息時使用彈簧球衣

[英]NPE in my service class of rest using jersey with spring

我是新來的春天和休息,我還在學習它。 我通過客戶端從我的rest api調用資源時遇到問題。

我在我的數據庫中有一些數據,我使用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來連接豆子。我知道我在接線的時候做錯了可以請任何人幫幫我

謝謝

我對web 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課程。我在課堂上面添加了注釋

@Repository("customerService")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM