簡體   English   中英

使用Jersey和Spring進行REST服務的@Autowired屬性的NullPointerException

[英]NullPointerException on @Autowired attribute with Jersey and Spring for REST service

我一直在開發一個gwt應用程序,應該有一個休息服務來訪問數據庫,包括它自己的數據庫和其他遠程數據庫。 我使用Spring來更好地使用數據庫(objectdb)而不是我在使用Jersey進行練習。 這是給出問題的代碼:

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

@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

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

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

@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。 當我表演

http://127.0.0.1/rest/customers

似乎customerDAO為null並且它導致該異常......

你能幫我嗎?

這是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

<?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。

將以下代碼放在applicationContext.xml中:

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

並在CustomerServiceImpl.java類上添加@Component注釋。

這應該適合你。 作為參考,您可以使用教程。 在這里,您可以更好地了解彈簧和JAX-RS的集成。

暫無
暫無

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

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