【问题标题】:NullPointerException on @Autowired attribute with Jersey and Spring for REST service带有 Jersey 和 Spring 的 @Autowired 属性上的 NullPointerException 用于 REST 服务
【发布时间】:2013-05-22 04:17:58
【问题描述】:

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

用户.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;
...
}

客户.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>

【问题讨论】:

  • appcontext XML 看起来如何?你还记得&lt;context:component-scan base-package="..."/&gt;&lt;context:annotation-config/&gt;吗?
  • 嗯,是的……你写的不存在。我应该按照你写的方式复制xml文件吗?
  • 我添加了 因为另一个存在但问题仍未解决...
  • 只是一个问题:您为什么使用 Jersey? Spring 提供了相同的结果,在某些情况下甚至更好。
  • 春天很难用作休息服务……我们的时间太少了。无论如何,我刚刚解决了问题,包括这些 jar aspectjrt-1.5.3.jar aspectjweaver-1.5.3.jar cglib-2.2.2.jar asm-3.1.jar jackson-core-asl-1

标签: java spring rest servlets jersey


【解决方案1】:

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

将以下代码放入您的 applicationContext.xml 中:

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

并在您的 CustomerServiceImpl.java 类上添加 @Component 注释。

这应该适合你。作为参考,您可以使用this 教程。在这里可以更好的理解spring和JAX-RS的集成。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-01-05
    • 2022-11-08
    • 2014-12-05
    • 1970-01-01
    • 2023-03-31
    相关资源
    最近更新 更多