REST - supporting multiple possible identifiers

后端 未结 5 1219
一向
一向 2020-12-13 02:00

For the site I am working on, we are in the process of improving our URLs for one type of resource - specifically, moving away from numerical IDs toward unique, descriptive

5条回答
  •  借酒劲吻你
    2020-12-13 02:17

    Quite an old question but I had the same and finnaly found the solution : use regex in your path param.

    Here's how I coded that use case

    @GET
    @Path("/{id : \\d+}")
    @Produces(APPLICATION_JSON)
    public Response getById(@PathParam("id") long id) {
     <>
    }
    
    @GET
    @Path("/{name}")
    @Produces(APPLICATION_JSON)
    public Response getByName(@PathParam("name") String name) {
     <>
    }
    

提交回复
热议问题