简体   繁体   中英

Regular Expression in @Path for RESTEasy Web Method

I need to add a regular expression to my @Path statement for my RESTEasy web method to allow the two following types of urls below to connect to the web method:

... where the id = 'r2lXIcBfNfnp2yOK', and the version can be either '1' or '1.0.0'. How can I do this?

My method so far, which accepts version as '1.0.0' but not '1':

 @GET() 
 @Produces("application/x-protobuf") 
 @Path("/Things/{id}.{version:
 (([0-9\\*]+\\.[0-9\\*]+\\.[0-9\\*]+))}")
    public String getThing( 
       @PathParam("id") String id, 
    @PathParam("version") @DefaultValue("1.0.0") String version, 
    @Context final HttpServletResponse response) 
{       
       //.... (rest of the method, irrelevant
}

What is another regular expression that I can add to the @Path statement above to also allow '1' as a version?

I've tried this:

@Path("/{id}.{version: (([0-9\\*^\\.])|([0-9\\*]+\\.[0-9\\*]+\\.[0-9\\*]+))}

... but that doesn't work.

I've also tried this:

@Path("/{id}.{version: (([0-9\\*])|([0-9\\*]+\\.[0-9\\*]+\\.[0-9\\*]+))}

... but that just cuts off the first digit when I pass in a version '1.0.0' so that it becomes '0.0'.

Thanks in advance for the help.

Use \\\\d+((\\\\.\\\\d+){2})? for allowing either <number> or <number>.<number>.<number>

\\\\d+ allows one or more digits

(\\\\.\\\\d+){2} allows pattern of a . followed by one or more digits occurring twice

the following ? makes the second pattern optional

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