简体   繁体   中英

Match last path element using JaxRS @Path annotation in Jersey

Using a previous version of Jersey (~1.12), I used to match the literal path element "data" with one annotation if it occurred at the end of the path, and another annotation if it was somewhere else:

@Path("data$")
public Object getDataResource(@Context UriInfo uriInfo) {
    // Matched when 'data' is the last path element
    ...
}

@Path("{name}")
public Object getNamedResource(@Context UriInfo uriInfo) {
    // Matched when 'data' is not the last path element
    ...
}

At some point between 1.12 and 1.17, this behavior has changed and the '$' character is now escaped before the pattern is applied to the incoming URI. Now the first method (getDataResource) is never matched.

For example, when matching http://.../data/data , I expect getNamedResource to match the first "data" and getDataResource to match the second "data". Instead, getNamedResource now matches both.

  1. Can I revert to the old behavior where I am responsible for escaping my regex?
  2. Is there a new/better/other technique for matching only the last path element on a URI?

The best solution I've found is to manually detect when the second method is matching the last path element and manually delegate to the first.

@Path("{name}")
public Object getNamedResource(@Context UriInfo uriInfo) {
    boolean lastElement = uriInfo.getMatchedURIs()
                           .iterator().next().equals(uriInfo.getPath());
    if (lastElement && "data".equals(name)) {
        return getDataResource(uriInfo);
    }
    ...
}

Not a bad solution to my particular problem, but I'm still frustrated that Jersey is escaping my pattern without asking me first.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM