简体   繁体   中英

REST Path params conflicting with resource path?

This question came up while I was building a backend using JAX-RS but this can really apply to any REST API.

How does JAX-RS handle paths that might conflict due to param path variables? Suppose you have

@POST
@Path('createBox/{boxName}')
foo()

@POST
@Path('createBox/small')
bar()

And someone wants to call the first endpoint with a path param argument of small . What happens in this case? If foo() and bar() had different parmas (maybe like @FormParam ) would that help differentiate? What if they were exactly the same with no arguments? Is the behavior non-deterministic?

Reference: RESTful Java with JAX-RS 2.0, 2nd Edition by Bill Burke

  1. "/customers/{id: .+} <-- getCustomer
  2. "/customers/{id: .+}/address" <-- getAddress

Precedence rules

The JAX-RS specification has defined strict sorting and precedence rules for matching URI expressions and is based on a most specific match wins algorithm.

  1. The primary key of the sort is the number of literal characters in the full URI matching pattern and is in descending order ( 11 in getCustomer vs 18 in getAddress)
  2. The secondary key of the sort is the number of template expressions embedded within the pattern—that is, {id} or {id: .+} . This sort is in descending order.
  3. The tertiary key of the sort is the number of nondefault template expressions. A default template expression is one that does not define a regular expression—that is, {id}

Your example:

bar() wins because as per rule 1, it has more literal characters

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