一种方法,多个休息路径

时间:2012-10-14 09:17:55

标签: regex rest path annotations resteasy

是否可以在Java中的同一REST方法上设置多个@Path注释?

显然我已经尝试了这个,但它没有工作,它没能编译,但有一些方法来做到这一点?也许是正则表达式?

如果有帮助,我正在使用resteasy。

我的方法看起来像这样(下面的伪代码):

@Path("/project/{projecID}/car/{carID}/carService/{carserviceID}/engine/{engineID}")
public Engine getCarEngin(@PathParam("projecID") projectID, @PathParam("carID") carID, @PathParam("carserviceID") carserviceID, @PathParam{engineID}){
    // return engine based on the id
}

@Path("/project/{projecID}/bus/{busID}/busService/{busserviceID}/engine/{engineID}")
public Engine getBusEngin(@PathParam("projecID") projectID, @PathParam("busID") busID, @PathParam("carserviceID") carserviceID, @PathParam{engineID}){
    // return engine based on the id
}

所以方法是一样的,逻辑是相同的但是我想在一种方法下将它联合起来但保留两条路径。

1 个答案:

答案 0 :(得分:1)

  

是否可以在同一个REST上设置多个@Path注释   Java中的方法?

你发现那是不可能的。

Perhaps vie the regular expression?

你可以用正则表达式来做,但这只会带来很多不必要的复杂性。它还会降低@Path - 注释的可读性。恕我直言,不要使用正则表达式。

也许你可以这样做:

enum VehicleType {
    BUS, CAR
}


@Path("/project/{projecID}/{vehicleType}/{vehicleID}/service/{serviceID}/engine/{engineID}")
public Engine getEngine(@PathParam("projecID") projectID, 
    @PathParam("vehicleType") VehicleType vehicleType,  
    @PathParam("vehicleID") vehicleID, @PathParam("serviceID") serviceID, 
    @PathParam("engineID"} engineID){

    // return engine based on the id
}