1

I have 2 almost identical sub-resources:

@GET
@Path("/{device}/{property}")
@Produces(MediaType.APPLICATION_JSON)
public Response getParameterValue(@PathParam("device") final String device,
            @PathParam("property") final String property) {
   ...
}

and

@GET
@Path("/{device}/{property}/{field}")
@Produces(MediaType.APPLICATION_JSON)
public Response getFieldValue(@PathParam("device") final String device,
            @PathParam("property") final String property,
            @PathParam("field") final String field ) {
   ...
}

that I want to replace with a single one using regular expressions. I saw among other related many articles about this for example this post and this one on SO. So I was almost sure to achive my goal writting the sub-resource like so:

@GET
@Path("/{device}/{property}{p:/?}{field:([a-zA-Z][a-zA-Z0-9_]*)?}")
@Produces(MediaType.APPLICATION_JSON)
public Response getParameterOrFieldValue(@PathParam("device") final String device,
           @PathParam("property") final String property,
           @PathParam("field") final String field ) {
   ...
}

Unfortunately if a value is present for the field path-parameter:

http://localhost:8080/my-rest/v1/device001/property001/field001

, I got:

HTTP 405 Method Not Allowed

But if there is no value for field:

http://localhost:8080/my-rest/v1/device001/property001 it works fine.

I than introduced a constant part 'just2C' in the path of the sub-resource:

@GET
@Path("/just2C/{device}/{property}{p:/?}{field:([a-zA-Z][a-zA-Z0-9_]*)?}")
@Produces(MediaType.APPLICATION_JSON)
public Response getParameterOrFieldValue(@PathParam("device") final String device,
        @PathParam("property") final String property,
        @PathParam("field") final String field ) {
       ...
}  

it works in both cases: with or without a value for the path-parameter 'field'. I'd like to undestand why? I am using jersey2.6

Meziane
  • 1,586
  • 1
  • 12
  • 22

0 Answers0