使用 Jersey/Spring 和 Tomcat 在 REST 中注入时出错

发布于 2025-01-05 10:26:11 字数 4285 浏览 0 评论 0原文

我正在尝试在 Tomcat 上使用 Jersey 和 Spring 来实现 REST API,但出现错误:

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

:

<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.javastartup.xml

@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();
    }
}

web.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 类是一个接口,无法实例化。

如果您能帮助我,我将非常感激!

谢谢!

I'm trying to implement REST API using Jersey with Spring on Tomcat but I'm getting error:

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 implements IMyClassService

Can any one tell me why I'm getting the error?
The class com.abc.service.interface.IMyClassService is an interface and cannot be instantiated.

I'll really appreciate if you can help me out!!

Thanks!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

任谁 2025-01-12 10:26:11

有一点让我担心:

异常来自于Jersey,而不是Spring。如果您(像我一样)期望 MyResourse 的字段 serviceImpl 由 Spring 框架填充,那么这可能就是问题所在。看来 Jersey 尝试填充它,但因某些配置失败而失败。

一个快速的解决方法是使用 Springs @Autowire 而不是 @Inject

There is one point that makes me worry:

The exception comes from Jersey, not Spring. If you (like me) expect that the field serviceImpl of MyResourse is populated by the Spring framework then this is likely the problem. It seams that Jersey attempts to populate it, and fails with some configuration failure.

A quick workaround would be using Springs @Autowire instead of @Inject.

眼前雾蒙蒙 2025-01-12 10:26:11

检查此异常的原因(在控制台上打印异常的正上方)。当我配置了多个能够响应请求类型的方法时,我遇到了此异常。
例如:/rest/getData 请求可能会转到 method1 和 method2。

Check the reason for this exception( Right above where the exception is printed on console). I got this exception when I had multiple methods configured capable to respond to a request type.
eg: /rest/getData request could possibly go to method1 and method2.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文