2015-12-02 111 views
0

TLDR:多Pathparameters和端点与扬鞭扬鞭,端点和Pathparameters

我想做些什么来为某些API端点这样的:

/foo/{id}/bar 

现在据我所知foo或第一个节点的路径正在定义端点以及资源到aqcuire。因此生成一个FooApiServiceImpl。

生成ProjectsApiService存根看起来像这样ATM:

@GET 
@Path("/{id}/bars") 
... 
public Response getBarsByFooId(@ApiParam(value = "The id of the foo with the bars",required=true) @PathParam("id") String id) 
throws NotFoundException { 
    return delegate.getBarByFooId(id); 
} 

现在我希望的行为将是让那些connectted到Foo与给定{id}所有Bar资源。有点像相反的顺序。这是否有可能?

如果这是不可能的...那么还想问一下,我怎么能得到没有定义为{xxx}参数在括号中的url节点(富,酒吧)?

事情是这样的:从它

@Context 
private UriInfo uriInfo; 

,然后调用UriInfo.getPathSegments()

public Response getBarsByFooId(String foo, String id, String bar) 
throws NotFoundException { 
    return delegate.getBarByFooId(id); 
} 

回答

0

要获得路径段,注入UriInfo在您的资源类或方法

List<PathSegment> segments = uriInfo.getPathSegments(); 

参见UriInfo.getPathSegments()文档:

获取电流相对于基座URI作为PathSegment列表的请求的路径。 当需要分析路径时,此方法很有用,特别是当路径中可能存在矩阵参数时。路径段中的所有转义字节序列和矩阵参数值都被解码,相当于getPathSegments(true)