我想根据某些注释在 Jersey 中动态调用资源方法。例如,如果我在不同的包中有两个相同的资源,具有类似的资源方法......
package com.mycompany.controller.v1;
@Component(value = "UsersControllerv1")
@Path("/users")
public class UsersController {
@GET
@Version("v1")
@Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
public Response getUsers() {
}
}
package com.mycompany.controller.v2;
@Component(value = "UsersControllerv2")
@Path("/users")
public class UsersController {
@GET
@Version("v2")
@Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
public Response getUsers() {
}
}
其中@Version 是一个自定义注解,用于指定api 的版本。无论如何我可以拦截一个请求(可能使用ResourceFilterFactory),它指定一个版本作为头参数并调用具有匹配版本的特定资源方法
如果尝试这个,我会在启动时收到以下错误
12-Sep-2012 14:36:00 com.sun.jersey.spi.inject.Errors processErrorMessages
SEVERE: The following errors and warnings have been detected with resource and/or provider classes:
SEVERE: Conflicting URI templates. The URI template /users for root resource class com.mycompany.controller.v2.UsersController and the URI template /users transform to the same regular expression /users(/.*)?
12-Sep-2012 14:36:00 com.sun.jersey.spi.spring.container.servlet.SpringServlet initiate
SEVERE: Exception occurred when intialization
com.sun.jersey.spi.inject.Errors$ErrorMessagesException
at com.sun.jersey.spi.inject.Errors.processErrorMessages(Errors.java:170)
at com.sun.jersey.spi.inject.Errors.postProcess(Errors.java:136)
at com.sun.jersey.spi.inject.Errors.processWithErrors(Errors.java:199)
at com.sun.jersey.server.impl.application.WebApplicationImpl.initiate(WebApplicationImpl.java:771)
at com.sun.jersey.spi.spring.container.servlet.SpringServlet.initiate(SpringServlet.java:117)
at com.sun.jersey.spi.container.servlet.ServletContainer$InternalWebComponent.initiate(ServletContainer.java:318)
at com.sun.jersey.spi.container.servlet.WebComponent.load(WebComponent.java:609)
at com.sun.jersey.spi.container.servlet.WebComponent.init(WebComponent.java:210)
at com.sun.jersey.spi.container.servlet.ServletContainer.init(ServletContainer.java:373)
at com.sun.jersey.spi.container.servlet.ServletContainer.init(ServletContainer.java:719)
at org.eclipse.jetty.servlet.FilterHolder.doStart(FilterHolder.java:114)