尝试添加资源类时出现冲突URI模板错误 [英] Getting Conflicting URI templates errors when trying to add resource class

查看:269
本文介绍了尝试添加资源类时出现冲突URI模板错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个使用Jersey和Tomcat7的restfull实现.我在campher.rest软件包中定义了3个资源,分别称为RegionService,ClientService和NoteService.

I have a restfull implementation using Jersey and Tomcat7. I have 3 resources called RegionService, ClientService and NoteService defined in my campher.rest package.

当我尝试添加另一个名为TestResource的资源,并且Tomcat启动时,它在下面给了我以下错误.我不了解/{notes}与/{test}有何冲突?? 请帮忙,我的头发会谢谢你.

When I try to add another resource called TestResource, and Tomcat starts, it gives me the following error below. I don't understand how /{notes} conflicts with /{test}?? Please help, my hair will thank you.

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个服务的基本实现.

Here are the skeleton implementations of those 4 services.

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<web-root>/bar.这里的单词test仅仅是将foobar值相关联的path-param映射键.

will match <web-root>/foo and <web-root>/bar. The word test here is merely the path-param map key to associate foo and bar values.

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

Notice the presence and absence of {} around the names. They completely change the meaning of the expression. Their presence indicates that you want to extract that out and put it in an instance variable annotated with @PathParam("name-between-brackets").

您的@Path("{test}")@Path("{notes}")实质上都是在要求Jersey查找http://<host:port>/<webapp>/{capture-text}形式的根URL,并将capture-text分别复制到testnotes路径变量中.这是模棱两可的.

Your @Path("{test}") and @Path("{notes}") both are essentially asking Jersey to look for root URLs of the form http://<host:port>/<webapp>/{capture-text} and copy the capture-text into test and notes path variables respectively. This is ambiguous.

这篇关于尝试添加资源类时出现冲突URI模板错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆