2

i am trying to understand rest webservices and came a cross dynamic url building. below is piece of code i found in examples

@Path("/customers")
public class CustomerResource {
@GET
@Path("{id : \\d+}")
public String getCustomer(@PathParam("id") int id) {
...
}
}

@GET
@Path("{id : .+}")
public String getCustomer(@PathParam("id") String id) {
...
}

Path attribute is build using regular expression. i tried google and found some info but i am still confused

{} is this is used as range like a{2,3} means a should be atleast 2 times .+ is any character at least should occur once \d+ at least one digit

but i am unable to imagine which string matches {id : .+} and {id : \\d+} can you please help me understand

prred
  • 131
  • 1
  • 8
  • As an aside the two path expressions here are ambiguous and the behavior using both of these is undefined for this level of ambiguity (meaning results may differ for different implementations) as [examined here](http://stackoverflow.com/q/28103978/2587435) – Paul Samsotha Oct 13 '15 at 06:28

2 Answers2

2

\d+ means "any digit one or more times". Double "\" is used to escape special character "\".

"{id : \d+}" means method param id must be only number.

.+ means "any character one or more times". Dot symbol is not escaped because it is not special character

"{id : .+}" means that method param id can be any string

tillias
  • 1,035
  • 2
  • 12
  • 30
  • Thanks for response .. so if i understand properly {id: it is just saying that id is method parameter name and has nothing to do with regex .. the last part \d+ or .+ is regex which we need to look at – prred Oct 13 '15 at 04:18
  • Yes. Here are more samples: https://jersey.java.net/documentation/1.19/jax-rs.html#d4e102 – tillias Oct 13 '15 at 04:20
0

if you have any doubt, please refer this following link. http://java2novice.com/restful-web-services/jax-rs-path-regex-match/

you can do like that
@Path("{id: [0-9]+}")
Kushal
  • 8,100
  • 9
  • 63
  • 82
Kumaresan Perumal
  • 1,926
  • 2
  • 29
  • 35