我是一个REST / Jersey新手,我有一个简单的RESTful Java Web服务,用Spring版本的Jersey实现...
@Service
@Path("/getAccountBalance")
public class GetAccountBalanceService {
@Autowired
private ILicenseService licenseService;
@GET
@Path("/{param}")
public Response provideService(@PathParam("param") String licenseUUID) {
License license = this.licenseService.getByUUID(licenseUUID);
String output = "Balance on the account : " + license.getBalanceValue();
return Response.status(200).entity(output).build();
}
并在web.xml中配置了带有Spring支持的jersey servlet ..
<servlet>
<servlet-name>jersey-servlet</servlet-name>
<servlet-class>
com.sun.jersey.spi.spring.container.servlet.SpringServlet
</servlet-class>
<init-param>
<param-name>com.sun.jersey.config.property.packages</param-name>
<param-value>com.freedomoss.crowdcontrol.api</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
<servlet-mapping>
<servlet-name>jersey-servlet</servlet-name>
<url-pattern>/service/*</url-pattern>
</servlet-mapping>
但是当我尝试调用该服务时,我得到以下异常....
Dec 19, 2014 11:34:04 AM com.sun.jersey.spi.spring.container.SpringComponentProviderFactory registerSpringBeans
INFO: Registering Spring bean, jsonProvider, of type com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider as a provider class
SEVERE: Exception occurred when intialization
java.lang.RuntimeException: The scope of the component class com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider must be a singleton
at com.sun.jersey.core.spi.component.ioc.IoCProviderFactory.wrap(IoCProviderFactory.java:102)
所以我在我的应用程序的另一部分使用JacksonJaxbJsonProvider,那么为什么Jersey会触及这个类然后抱怨呢?
感谢。
答案 0 :(得分:0)
通过搜索对JacksonJaxbJsonProvider的所有引用的应用程序上下文XML(对于我的应用程序而言非常重要),我能够使这个错误消失。我看到2个单独的bean被定义为使用这个类。我删除了其中一个,并将singleton属性添加到我保留的那个......
<bean id="jaxbObjectMapper" class="com.fasterxml.jackson.databind.ObjectMapper"/>
<bean id="jsonProvider" class="com.fasterxml.jackson.jaxrs.json.JacksonJaxbJsonProvider" scope="singleton">
<property name="mapper" ref="jaxbObjectMapper" />
</bean>
希望这有助于某人。