Jin Kwon
Jin Kwon

Reputation: 21978

How can I get the whole sub path with JAX-RS?

With following URL.

http://doma.in/context/resource/some/.../undefined

I want to get the path name after ../resource which is /some/.../undefined

@Path("/resource")
class Response Resource {
    final String path; // "/some/.../undefined"
}

Thanks.

UPDATE

There is, as answered, a way to do this.

@Path("/resource")
class class Resource {

    @Path("/{paths: .+}")
    public String readPaths() {
        final String paths
            = uriInfo.getPathParameters().getFirst("paths");
    }

    @Context
    private UriInfo uriInfo;
}

When I invoke

http://.../context/resource/paths/a/b/c

I get

a/b/c

.

Upvotes: 3

Views: 4002

Answers (1)

Paul Samsotha
Paul Samsotha

Reputation: 208984

You can get all the URI information from UriInfo, which you can inject either as a field or method parameter, using the @Context annotation.

public Response getResponse(@Context UriInfo uriInfo) {
}

-- OR --

@Context 
private UriInfo uriInfo;

There's a bunch of methods you can use for reading the URI. Just look at the API linked above.

Upvotes: 4

Related Questions