【问题标题】:Swagger, Endpoints and PathparametersSwagger、端点和路径参数
【发布时间】:2015-12-02 12:16:36
【问题描述】:

TLDR:使用 Swagger 的多个路径参数和端点

我想要的是一些这样的 API 端点:

/foo/{id}/bar

现在 afaik 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);
}

现在我希望的行为是使用给定的{id} 获取连接到Foo 的所有Bar 资源。有点像倒序。这有可能吗?

如果这是不可能的......那么还想问一下,我怎样才能获得未定义为{xxx}括号中的参数的url节点(foo,bar)?

类似这样的:

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

【问题讨论】:

    标签: web-services rest jax-rs swagger


    【解决方案1】:

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

    @Context
    private UriInfo uriInfo;
    

    然后从中调用UriInfo.getPathSegments()

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

    请参阅UriInfo.getPathSegments() 文档:

    PathSegment 列表的形式获取当前请求相对于基本 URI 的路径。 当需要解析路径时,此方法很有用,尤其是当路径中可能存在矩阵参数时。路径段和矩阵参数值中的所有转义字节序列都被解码,相当于getPathSegments(true)

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-01-25
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-12-22
      相关资源
      最近更新 更多