我的spring jdbcTemplate在从一个宁静的Web服务调用它时返回null

时间:2016-08-17 23:05:32

标签: java spring rest jdbctemplate

我创建了一个休息服务并在客户端上使用它并从MySQL db中获取数据会返回一个空指针异常。

我的DAO课程

public class SpringDB {

@Autowired
private JdbcTemplate template;

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

public List<Book> getAllBooksDao() {
    return template.query("select * from books.library", new RowMapper<Book>() {
        @Override
        public Book mapRow(ResultSet rs, int rownumber) throws SQLException {
            Book e = new Book();
            e.setId(rs.getString(1));
            e.setName(rs.getString(2));
            e.setPrice(rs.getString(3));
            e.setAuthor(rs.getString(4));
            return e;
        }
    });
}

我的服务类

public class BookService {


SpringDB spring = new SpringDB();
public List<Book> getAllBooks() throws SQLException, ClassNotFoundException {


    return new ArrayList<Book>(spring.getAllBooksDao());
}
}

我的资源类

@Path("/library")
@Produces({ MediaType.APPLICATION_JSON + ";charset=UTF-8",MediaType.APPLICATION_XML + ";charset=utf-8" })     
@Consumes({ MediaType.APPLICATION_JSON + ";charset=UTF-8",MediaType.APPLICATION_XML + ";charset=utf-8" })    
public class BookResource {

BookService bookService = new BookService();

@GET
public Response getBooks(@QueryParam("format") String format) throws SQLException, ClassNotFoundException {

    return Response.status(Status.OK).entity((new GenericEntity<List<Book>>(bookService.getAllBooks()) {
    })).header(HttpHeaders.CONTENT_TYPE, "XML".equalsIgnoreCase(format)
            ? MediaType.APPLICATION_XML + ";charset=UTF-8" : MediaType.APPLICATION_JSON + ";charset=UTF-8").build();

}

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:p="http://www.springframework.org/schema/p"   
xsi:schemaLocation="http://www.springframework.org/schema/beans   
http://www.springframework.org/schema/beans/spring-beans-3.0.xsd">

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

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

<bean id="bookDAO" class="com.nag.library.database.SpringDB">
    <property name="template" ref="jdbcTemplate"></property>
</bean>

错误

java.lang.NullPointerException
at com.nag.library.database.SpringDB.getAllBooksDao(SpringDB.java:24)
at com.nag.library.service.BookService.getAllBooks(BookService.java:18)
at com.nag.library.resource.BookResource.getBooks(BookResource.java:36)
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)
at org.glassfish.jersey.server.model.internal.ResourceMethodInvocationHandlerFactory$1.invoke(ResourceMethodInvocationHandlerFactory.java:81)
at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher$1.run(AbstractJavaResourceMethodDispatcher.java:144)
at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.invoke(AbstractJavaResourceMethodDispatcher.java:161)
at org.glassfish.jersey.server.model.internal.JavaResourceMethodDispatcherProvider$ResponseOutInvoker.doDispatch(JavaResourceMethodDispatcherProvider.java:160)
at org.glassfish.jersey.server.model.internal.AbstractJavaResourceMethodDispatcher.dispatch(AbstractJavaResourceMethodDispatcher.java:99)
at org.glassfish.jersey.server.model.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:389)
at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:347)
at org.glassfish.jersey.server.model.ResourceMethodInvoker.apply(ResourceMethodInvoker.java:102)
at org.glassfish.jersey.server.ServerRuntime$2.run(ServerRuntime.java:326)
at org.glassfish.jersey.internal.Errors$1.call(Errors.java:271)
at org.glassfish.jersey.internal.Errors$1.call(Errors.java:267)
at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
at org.glassfish.jersey.internal.Errors.process(Errors.java:297)

错误第33行:

return template.query("select * from books.library", new RowMapper<Book>() {

错误第18行:

return new ArrayList<Book>(spring.getAllBooksDao());

错误第38行:

? MediaType.APPLICATION_XML + ";charset=UTF-8" :MediaType.APPLICATION_JSON + ";charset=UTF-8").build();  

0 个答案:

没有答案