2015-12-09 40 views
0

我試圖用Spring連接JERSEY休息服務,我面臨這個異常。嵌套的異常是org.springframework.beans.factory.BeanCreationException:無法自動裝配字段

PodcastResource

package com.integration.messenger.resources; 

import java.util.List; 

import javax.ws.rs.GET; 
import javax.ws.rs.Path; 
import javax.ws.rs.Produces; 
import javax.ws.rs.core.MediaType; 

import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.stereotype.Component; 

import com.integration.messenger.model.Podcast; 
import com.integration.messenger.service.PodcastService; 


@Path("/podcasts") 
@Component 
public class PodcastResource { 

@Autowired 
PodcastService podcastService; 

@GET 
@Produces({ MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML }) 
public List<Podcast> getPodcasts(){ 
    return podcastService.getAllPodcasts(); 
} 

/*@GET 
@Produces({ MediaType.APPLICATION_JSON}) 
public String getMessage(){ 
    return "Helooo"; 
}*/ 
} 

PodcastService

package com.integration.messenger.service; 

import java.util.List; 

import com.integration.messenger.model.Podcast; 

public interface PodcastService { 
List<Podcast> getAllPodcasts(); 

} 

PodcastServiceImp

包com.integration.messenger.service;

import java.util.ArrayList; 
import java.util.List; 
import java.util.Map; 

import org.springframework.beans.factory.annotation.Autowired; 
import org.springframework.stereotype.Service; 

import com.integration.messenger.model.Podcast; 
import com.integration.messenger.repository.PodcastRepository; 

@Service("podcastService") 
public class PodcastServiceImp implements PodcastService { 

@Autowired 
PodcastRepository podcastRepository; 

private Map<Long, Podcast> podcasts = podcastRepository.getPodcasts(); 

public PodcastServiceImp() { 
    podcasts.put(1L, new Podcast(1, "Hello World News", "How to Programme", "www.podcastpedia.com/How to Programme", "Programme")); 
    podcasts.put(2L, new Podcast(2, "Hello Jersey News", "Restfull Implementation", "www.podcastpedia.com/Restfull Implementation", "Restfull")); 
} 

public List<Podcast> getAllPodcasts() { 
    return new ArrayList<Podcast>(podcasts.values()); 
} 
} 

PodcastRepository

package com.integration.messenger.repository; 

import java.util.Map; 

import com.integration.messenger.model.Podcast; 

public interface PodcastRepository { 
    Map<Long, Podcast> getPodcasts(); 
} 

PodcastRepositoryImpl

package com.integration.messenger.repository; 

import java.util.HashMap; 
import java.util.Map; 

import org.springframework.stereotype.Repository; 

import com.integration.messenger.model.Podcast; 

@Repository 
public class PodcastRepositoryImpl implements PodcastRepository { 

private static Map<Long, Podcast> podcasts = new HashMap<Long, Podcast>(); 

public Map<Long, Podcast> getPodcasts() { 
    return podcasts; 
} 
} 

播客

package com.integration.messenger.model; 

import java.io.Serializable; 

import javax.xml.bind.annotation.XmlElement; 
import javax.xml.bind.annotation.XmlRootElement; 

@XmlRootElement 
public class Podcast implements Serializable{ 

private static final long serialVersionUID = -1471231465385956738L; 

@XmlElement(name = "id") 
private long id; 

@XmlElement(name = "feed") 
private String feed; 

@XmlElement(name = "title") 
private String title; 

@XmlElement(name = "linkOnPodcastpedia") 
private String linkOnPodcastpedia; 

@XmlElement(name = "description") 
private String description; 


public Podcast(int id, String feed, String title, String linkOnPodcastpedia, String description) { 
    this.id = id; 
    this.feed = feed; 
    this.title = title; 
    this.linkOnPodcastpedia = linkOnPodcastpedia; 
    this.description = description; 
} 

public long getId() { 
    return id; 
} 

public void setId(long id) { 
    this.id = id; 
} 

public String getFeed() { 
    return feed; 
} 

public void setFeed(String feed) { 
    this.feed = feed; 
} 

public String getTitle() { 
    return title; 
} 

public void setTitle(String title) { 
    this.title = title; 
} 

public String getLinkOnPodcastpedia() { 
    return linkOnPodcastpedia; 
} 

public void setLinkOnPodcastpedia(String linkOnPodcastpedia) { 
    this.linkOnPodcastpedia = linkOnPodcastpedia; 
} 

public String getDescription() { 
    return description; 
} 

public void setDescription(String description) { 
    this.description = description; 
} 

} 

MyApplication的

package com.integration.messenger; 

import org.glassfish.jersey.jackson1.Jackson1Feature; 
import org.glassfish.jersey.server.ResourceConfig; 

import com.integration.messenger.resources.PodcastResource; 

public class MyApplication extends ResourceConfig{ 
/** 
* Register JAX-RS application components. 
*/ 
public MyApplication() { 
    // register application resources 
    register(PodcastResource.class); 

    // register features 
    // register Jackson JSON providers - for the application to understand JSON data 
    register(Jackson1Feature.class);   

} 
} 

的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:tx="http://www.springframework.org/schema/tx" 
    xmlns:context="http://www.springframework.org/schema/context"  
    xmlns:jpa="http://www.springframework.org/schema/data/jpa" 
    xsi:schemaLocation="http://www.springframework.org/schema/beans 
http://www.springframework.org/schema/beans/spring-beans-4.2.xsd 
http://www.springframework.org/schema/context 
http://www.springframework.org/schema/context/spring-context.xsd 
http://www.springframework.org/schema/tx 
http://www.springframework.org/schema/tx/spring-tx.xsd 
http://www.springframework.org/schema/data/jpa 
http://www.springframework.org/schema/data/jpa/spring-jpa.xsd"> 

<context:component-scan base-package="com.integration.messenger" /> 

</beans> 

Web.xml中

<?xml version="1.0" encoding="UTF-8"?> 
<web-app version="3.0" xmlns="http://java.sun.com/xml/ns/javaee" 
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_3_0.xsd"> 

<servlet> 
    <servlet-name>jersey-Spring-Application</servlet-name> 
     <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class> 
    <init-param> 
     <param-name>javax.ws.rs.Application</param-name> 
     <param-value>com.integration.messenger.MyApplication</param-value>   
    </init-param>  
    <load-on-startup>2</load-on-startup> 
</servlet> 

<servlet-mapping> 
    <servlet-name>jersey-Spring-Application</servlet-name> 
    <url-pattern>/api/*</url-pattern> 
</servlet-mapping> 

<context-param> 
    <param-name>contextConfigLocation</param-name> 
    <param-value>classpath:applicationContext.xml</param-value> 
</context-param> 

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

<display-name>Restful Web Application</display-name> 

</web-app> 

ERROR:

WARNING: Exception encountered during context initialization - cancelling refresh attempt 
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'podcastResource': Injection of autowired dependencies failed; nested exception is org.springframework.beans.factory.BeanCreationException: Could not autowire field: com.integration.messenger.service.PodcastService com.integration.messenger.resources.PodcastResource.podcastService; nested exception is org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'podcastService' defined in file [C:\My_Workspaces\My_Work\.metadata\.plugins\org.eclipse.wst.server.core\tmp0\wtpwebapps\messenger\WEB-INF\classes\com\integration\messenger\service\PodcastServiceImp.class]: Instantiation of bean failed; nested exception is org.springframework.beans.BeanInstantiationException: Failed to instantiate [com.integration.messenger.service.PodcastServiceImp]: Constructor threw exception; nested exception is java.lang.NullPointerException 
at org.springframework.beans.factory.annotation.AutowiredAnnotationBeanPostProcessor.postProcessPropertyValues(AutowiredAnnotationBeanPostProcessor.java:334) 
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.populateBean(AbstractAutowireCapableBeanFactory.java:1214) 
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.doCreateBean(AbstractAutowireCapableBeanFactory.java:543) 
at org.springframework.beans.factory.support.AbstractAutowireCapableBeanFactory.createBean(AbstractAutowireCapableBeanFactory.java:482) 
at org.springframework.beans.factory.support.AbstractBeanFactory$1.getObject(AbstractBeanFactory.java:305) 
at org.springframework.beans.factory.support.DefaultSingletonBeanRegistry.getSingleton(DefaultSingletonBeanRegistry.java:230) 
at org.springframework.beans.factory.support.AbstractBeanFactory.doGetBean(AbstractBeanFactory.java:301) 
at org.springframework.beans.factory.support.AbstractBeanFactory.getBean(AbstractBeanFactory.java:196) 
at org.springframework.beans.factory.support.DefaultListableBeanFactory.preInstantiateSingletons(DefaultListableBeanFactory.java:772) 
at org.springframework.context.support.AbstractApplicationContext.finishBeanFactoryInitialization(AbstractApplicationContext.java:834) 
at org.springframework.context.support.AbstractApplicationContext.refresh(AbstractApplicationContext.java:537) 
at org.springframework.web.context.ContextLoader.configureAndRefreshWebApplicationContext(ContextLoader.java:446) 
at org.springframework.web.context.ContextLoader.initWebApplicationContext(ContextLoader.java:328) 
at org.springframework.web.context.ContextLoaderListener.contextInitialized(ContextLoaderListener.java:107) 
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:4939) 
at org.apache.catalina.core.StandardContext.startInternal(StandardContext.java:5434) 
at org.apache.catalina.util.LifecycleBase.start(LifecycleBase.java:150) 
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1559) 
at org.apache.catalina.core.ContainerBase$StartChild.call(ContainerBase.java:1549) 
at java.util.concurrent.FutureTask.run(Unknown Source) 
at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source) 
at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source) 
at java.lang.Thread.run(Unknown Source) 
+0

發佈完整的錯誤! –

+0

我已更新錯誤消息。請參閱上文。 – user2842033

回答

0

我想你需要做一些配置相關的更改,以將Spring與Jersey集成。有些我能想到的,

  1. 註冊Spring ContextLoaderListener。 (這加載了spring xml文件)。
  2. 。利用彈簧相關球衣的servlet com.sun.jersey.spi.spring.container.servlet.SpringServlet或org.glassfish.jersey.servlet.ServletContainer(在最新的版本我相信)的
  3. 指定的春天xml文件。

-Madhu。

+0

嗨,我已經在我的web.xml中添加了這些配置。請參閱上文。 – user2842033

+0

對不起,我忽略了內容。我注意到的一件事是缺失。你也嘗試過放置註釋嗎?這是我想要在上面的第三項中指定的,但錯過了它。 –

+0

如果負責完成標籤的工作,您可以忽略我的上述評論。我有一個小混亂。我知道他們中的一個需要其他標籤,但不知道哪一個是這樣。 :( –

相關問題