2012-02-15 73 views
3

我試圖用球衣與Spring在Tomcat實現REST API,但我得到的錯誤:錯誤而注射使用REST新澤西州/彈簧和Tomcat

INFO: Root resource classes found: class com.abc.restrequest.MyClass 
Feb 15, 2012 6:35:24 AM com.sun.jersey.api.core.ScanningResourceConfig init 
INFO: No provider classes found. 
Feb 15, 2012 6:35:24 AM com.sun.jersey.server.impl.application.WebApplicationImpl _initiate 
INFO: Initiating Jersey application, version 'Jersey: 1.8 06/24/2011 12:17 PM' 
Feb 15, 2012 6:35:24 AM com.sun.jersey.spi.inject.Errors processErrorMessages 
SEVERE: The following errors and warnings have been detected with resource and/or provider classes: 
    SEVERE: The class com.abc.service.interface.IMyClassService is an interface and cannot be instantiated. 
Feb 15, 2012 6:35:24 AM org.apache.catalina.core.ApplicationContext log 
SEVERE: StandardWrapper.Throwable 
com.sun.jersey.spi.inject.Errors$ErrorMessagesException 

web.xml中:

<listener> 
    <listener-class>org.springframework.web.util.Log4jConfigListener</listener-class> 
</listener> 

<context-param> 
     <param-name>log4jConfigLocation</param-name> 
    <param-value>/WEB-INF/classes/log4j.properties</param-value> 
</context-param> 

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

<context-param> 
    <param-name>contextConfigLocation</param-name> 
    <param-value>/WEB-INF/classes/startup.xml,classpath*:startup.xml</param-value> 
</context-param> 

<servlet> 
    <servlet-name>Jersey REST Service</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>com.abc.restrequest</param-value> 
    </init-param> 
    <load-on-startup>1</load-on-startup> 
</servlet> 

<servlet-mapping> 
    <servlet-name>Jersey REST Service</servlet-name> 
    <url-pattern>/service/*</url-pattern> 
</servlet-mapping> 

MyResourse.java

@Inject 
private IMyClassService serviceImpl; 

@Override 
public void setServiceImpl(IMyClassService impl) { 
    serviceImpl = impl;  
} 

@Path("/mycall") 
@POST 
@Consumes ({MediaType.APPLICATION_JSON}) 
@Produces ({MediaType.APPLICATION_JSON}) 
public Response hellouser() throws ParseException { 
     try { 
     serviceImpl.callme(); 
     return Response.noContent().build(); 
    } catch (Exception e) { 
     return Response.status(500).entity(e.getLocalizedMessage()).build(); 
    } 
} 

startup.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:cxf="http://cxf.apache.org/core" 
    xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:context="http://www.springframework.org/schema/context" 
    xmlns:http-conf="http://cxf.apache.org/transports/http/configuration" 
    xsi:schemaLocation=" 
     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/util http://www.springframework.org/schema/util/spring-util-3.0.xsd 
     http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd 
     http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd 
     http://cxf.apache.org/transports/http/configuration http://cxf.apache.org/schemas/configuration/http-conf.xsd 
    "> 

    <context:component-scan base-package="com.abc.restrequest" /> 
    <context:annotation-config /> 

    <bean id="myrestservice" scope="prototype" class="com.abc.service.implementation.MyClassServiceImpl"> 
    </bean> 
</beans> 

MyClassServiceImpl實現IMyClassService

任何一個可以告訴我,爲什麼我得到這個錯誤嗎? 類com.abc.service.interface.IMyClassService是一個接口,不能實例化。

我真的很感激,如果你能幫助我!

謝謝!

回答

1

有一點讓我擔心:

這個例外來自澤西島,而不是春天。如果你(像我一樣)期望Spring框架填充字段serviceImplMyResourse,那麼這可能是問題所在。它接近澤西試圖填充它,並失敗,並出現一些配置失敗。

快速解決方法是使用Springs @Autowire而不是@Inject

0

檢查此異常的原因(在控制檯上打印異常的位置的正上方)。當我配置了多個可響應請求類型的方法時,我得到了這個異常。 例如:/ rest/getData請求可能會轉到method1和method2。

相關問題