带有Jersey和Spring的REST服务的@Autowired属性上的NullPointerException [英] NullPointerException on @Autowired attribute with Jersey and Spring for REST service

查看:100
本文介绍了带有Jersey和Spring的REST服务的@Autowired属性上的NullPointerException的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我一直在开发gwt应用程序,该应用程序应该具有rest服务来访问数据库(包括它自己的数据库和其他远程数据库).我使用Spring来更好地使用数据库(objectdb),而不是在Jersey实习. 这是给出问题的代码:

I've been development a gwt application with should have a rest service to access the database, both its own database and the other remote ones. I use Spring to better work with database (objectdb) and not I was having practice with Jersey. Here's the code giving the problem:

User.java

User.java

@Entity
@Inheritance(strategy=InheritanceType.TABLE_PER_CLASS)
@XmlRootElement
public class User implements java.io.Serializable {

private static final long serialVersionUID = 1L;

@Id @GeneratedValue(strategy=GenerationType.IDENTITY)
private long id;
private String name;
private String surname;
private int age;
...
}

Customer.java

Customer.java

@Entity
@XmlRootElement
public class Customer extends User implements java.io.Serializable{

private static final long serialVersionUID = 1L;

@Column(unique=true)
private String fiscalCode;
@Column(unique=true)
private String docNumber;
...
}

CustomerDAO.java

CustomerDAO.java

@Repository("customerDAO")
public class CustomerDAO extends JpaDAO<Customer> {
...
}

JpaDAO.java

JpaDAO.java

public abstract class JpaDAO<E> {
protected Class<E> entityClass;

@PersistenceContext(unitName = "MyPersistenceUnit")
protected EntityManager em;

@SuppressWarnings("unchecked")
public JpaDAO() {
 ParameterizedType genericSuperclass = (ParameterizedType) getClass().getGenericSuperclass();
 this.entityClass = (Class<E>) genericSuperclass.getActualTypeArguments()[0];
}

public List<E> findAll() {
    TypedQuery<E> q = em.createQuery(
            "SELECT h FROM " + entityClass.getName() + " h", entityClass);
    return q.getResultList();
}

最后是CustomerServiceImpl.java

and finally CustomerServiceImpl.java

@Service("customerService")
@Path("/customers")
public class CustomerServiceImpl implements CustomerService {

 @Autowired
 private CustomerDAO customerDAO;

 @Override
 @GET
 @Produces({MediaType.APPLICATION_XML})
 public List<Customer> findAll() {
    return customerDAO.findAll();
 }
}

正确编写了web.xml. 当我表演

the web.xml is correctly written. When I perform

http://127.0.0.1/rest/customers

customerDAO似乎为空,并导致该异常...

It seems that customerDAO is null and it causes that exception...

你能帮忙吗?

这是web.xml

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

<!-- Servlets -->

<!-- Default page to serve -->
<welcome-file-list>
    <welcome-file>RONF.html</welcome-file>
</welcome-file-list>

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>


<servlet>
    <servlet-name>springGwtRemoteServiceServlet</servlet-name>
    <servlet-class>org.spring4gwt.server.SpringGwtRemoteServiceServlet</servlet-class>
</servlet>


<servlet-mapping>
    <servlet-name>springGwtRemoteServiceServlet</servlet-name>
    <url-pattern>/ronf/ronfServices/*</url-pattern>
</servlet-mapping>

<servlet>
    <servlet-name>Jersey</servlet-name>
    <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
    <init-param>
      <param-name>com.sun.jersey.config.property.packages</param-name>
      <param-value>it.unibo.ronf.server.services</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>Jersey</servlet-name>
    <url-pattern>/rest/*</url-pattern>
</servlet-mapping>

<dependency>
    <groupId>com.sun.jersey.contribs</groupId>
    <artifactId>jersey-spring</artifactId>
    <version>${jersey.version}</version>
</dependency>

</web-app>

这是applicationContext.xml

and here is applicationContext.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"
xmlns:aop="http://www.springframework.org/schema/aop" xmlns:context="http://www.springframework.org/schema/context"
xmlns:jee="http://www.springframework.org/schema/jee" xmlns:tx="http://www.springframework.org/schema/tx"
xmlns:task="http://www.springframework.org/schema/task"
xsi:schemaLocation="
        http://www.springframework.org/schema/aop http://www.springframework.org/schema/aop/spring-aop-3.0.xsd
        http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
        http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd
        http://www.springframework.org/schema/jee http://www.springframework.org/schema/jee/spring-jee-3.0.xsd
        http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx-3.0.xsd
        http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task-3.0.xsd">

<context:component-scan base-package="it.unibo.ronf"/>

<context:annotation-config/>

<task:annotation-driven executor="myExecutor" scheduler="myScheduler"/>

<task:executor id="myExecutor" pool-size="5"/>

<task:scheduler id="myScheduler" pool-size="10"/>

<tx:annotation-driven/>

<bean class="org.springframework.orm.jpa.LocalEntityManagerFactoryBean" id="entityManagerFactory">
    <property name="persistenceUnitName" value="MyPersistenceUnit"/>
</bean>

<bean class="org.springframework.orm.jpa.JpaTransactionManager" id="transactionManager">
    <property name="entityManagerFactory" ref="entityManagerFactory"/>
</bean>

</beans>

推荐答案

您尚未在applicationContext.xml文件中为customerDAO创建bean.如果要将其用作CustomerServiceImpl.java中的bean,请在applicationContext.xml中创建其bean.

You have not created bean for customerDAO in your applicationContext.xml file. If you want to use this as bean in your CustomerServiceImpl.java, create its bean in applicationContext.xml.

将以下代码放在applicationContext.xml中:

Put below code in your applicationContext.xml :

<bean class="name.of.package.CustomerDAO" id="customerDAO">
</bean>

并在CustomerServiceImpl.java类上添加@Component批注.

这应该为您工作.作为参考,您可以使用教程.在这里您可以更好地了解spring和JAX-RS的集成.

This should work for you. For reference you can use this tutorial. Here you can better understand integration of spring and JAX-RS.

这篇关于带有Jersey和Spring的REST服务的@Autowired属性上的NullPointerException的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆