【问题标题】:How to get only part of path from @Path?如何仅从@Path 获取路径的一部分?
【发布时间】:2017-04-27 15:45:25
【问题描述】:

我需要从路径(“/api/webservice/cursos/sugeridos/java-para-desenvolvimento-web”)获取(“/api/webservice/cursos/”)。

我有课:

@Path("/webservice/cursos")
public class Resource {

@GET
    @Path("/sugeridos/{uri: [\\w+\\-]*}")
    .........
    public Response getCourseByURI(@PathParam("uri") String uri, @Context UriInfo uriInfo) {
                String baseUrl = uriInfo.getAbsolutePath().getPath();
     }
}

uriInfo.getAbsolutePath().getPath() 返回:

"/api/webservice/cursos/sugeridos/java-para-desenvolvimento-web"

我只需要:

"/api/webservice/cursos/"

【问题讨论】:

    标签: java web-services url path uri


    【解决方案1】:

    可以使用java-7 Path类:

    Path path = Paths.get("/api/webservice/cursos/sugeridos/java-para-desenvolvimento-web");
    System.out.println(path.subpath(0,3)); // api/webservice/cursos
    

    【讨论】:

      【解决方案2】:

      你可以这样做:

      String pathInfo = request.getPathInfo(); // //api/webservice/cursos/sugeridos/java-para-desenvolvimento-web
      String[] parts = pathInfo.split("/");
      String part0 = parts[0]; // api
      String part1 = parts[1]; // webservice
      String part2 = parts[2]; // cursos
      

      获取HttpServletRequest中的请求。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2018-04-18
        • 1970-01-01
        • 2010-12-28
        • 2010-10-29
        • 1970-01-01
        • 2011-09-20
        • 2020-07-27
        • 2011-10-20
        相关资源
        最近更新 更多