尝试添加资源类时出现冲突的 URI 模板错误

2023-12-23

我有一个使用 Jersey 和 Tomcat7 的宁静实现。我在 Campher.rest 包中定义了 3 个名为 RegionService、ClientService 和 NoteService 的资源。

当我尝试添加另一个名为 TestResource 的资源并且 Tomcat 启动时,出现以下错误。我不明白 /{notes} 如何与 /{test} 冲突? 请帮忙,我的头发会感谢你的。

Aug 22, 2012 2:23:39 AM com.sun.jersey.api.core.ScanningResourceConfig logClasses
INFO: Root resource classes found:
  class campher.rest.NoteService
  class campher.rest.ClientService
  class campher.rest.TestResource
  class campher.rest.RegionService
Aug 22, 2012 2:23:39 AM com.sun.jersey.api.core.ScanningResourceConfig init
INFO: No provider classes found.
Aug 22, 2012 2:23:40 AM com.sun.jersey.server.impl.application.WebApplicationImpl _initiate
INFO: Initiating Jersey application, version 'Jersey: 1.12 02/15/2012 04:51 PM'
Aug 22, 2012 2:23:40 AM 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 /{test} for root resource class campher.rest.TestResource and the URI template /{notes} transform to the same regular expression /([^/]+?)(/.*)?
Aug 22, 2012 2:23:40 AM org.apache.catalina.core.ApplicationContext log
SEVERE: StandardWrapper.Throwable

以下是这 4 项服务的框架实现。

package campher.rest;
@Path("regions")
public class RegionService {
    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public Response regions() {}

    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    public Region addRegion(Region region){}

    @PUT
    @Consumes(MediaType.APPLICATION_JSON)
    public Region updateRegion(Region region){}

    @GET @Path("{id}")  
    @Produces(MediaType.APPLICATION_JSON)
    public Response getRegion(@PathParam("id") long id) {}

    @DELETE @Path("{id}")   
    @Produces(MediaType.APPLICATION_JSON)
    public Response deleteRegion(@PathParam("id") long id) {}

    @GET @Path("{id}/clients")  
    @Produces(MediaType.APPLICATION_JSON)
    public Response getClients(@PathParam("id") long id) {}
}



package campher.rest;
@Path("clients")
public class ClientService {
    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public Response clients() {}

    @GET @Path("{id}")  
    @Produces(MediaType.APPLICATION_JSON)
    public Response getClient(@PathParam("id") long id) {}

    @GET @Path("{id}/notes")    
    @Produces(MediaType.APPLICATION_JSON)
    public Response getNotes(@PathParam("id") long id) {}

    @GET @Path("{id}/alerts")   
    @Produces(MediaType.APPLICATION_JSON)
    public Response getAlerts(@PathParam("id") long id) {}

    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    public Client addClient(Client client){}

    @PUT
    @Consumes(MediaType.APPLICATION_JSON)
    public Client updateClient(Client client){}

    @DELETE @Path("{id}")   
    @Consumes(MediaType.APPLICATION_JSON)
    public Response deleteClient(@PathParam("id") long id){}
}

package campher.rest;
@Path("{notes}")
public class NoteService {  
    @GET @Path("{id}")  
    @Produces(MediaType.APPLICATION_JSON)
    public Response getNote(@PathParam("id") long id) {}

    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    public Note addNote(Note note){}

    @PUT
    @Consumes(MediaType.APPLICATION_JSON)
    public Note updateNote(Note note){}

    @DELETE @Path("{id}")   
    @Produces(MediaType.APPLICATION_JSON)
    public Response deleteNote(@PathParam("id") long id) {}     
}

package campher.rest;
import javax.ws.rs.Path;
@Path("{test}")
public class TestResource {

}

@Path("test") 

将匹配<web-root>/test

@Path("{test}")

将匹配<web-root>/foo and <web-root>/bar。这里的单词 test 只是要关联的路径参数映射键foo and bar values.

注意存在和不存在{}围绕着名字。它们完全改变了表达的含义。它们的存在表明您想要将其提取出来并将其放入带有注释的实例变量中@PathParam("name-between-brackets").

Your @Path("{test}") and @Path("{notes}")两者本质上都是要求 Jersey 查找表单的根 URLhttp://<host:port>/<webapp>/{capture-text}并复制capture-text into test and notes分别是路径变量。这是模棱两可的。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

尝试添加资源类时出现冲突的 URI 模板错误 的相关文章

随机推荐