简体   繁体   中英

Resteasy optional Path element

Is it possible to use RestEasy's Path annotation to get the following string:

/items.json

I was thinking something like this: /items{(\\.)?format}, where format could be json, xml etc.

I would then have a method with an argument like: @PathParam("format") String format .

Thanks.

I managed to make the following work with my use case: item{format:(\\.(json|xml))?}

I chose to make the reg exp restrictive so as not to have to handle unsupported or invalid formats inside the actual service method, but if one prefers a more general approach I think that instead of (json|xml) one can add \\S+.

you might want to create two methods, one for the default type and one for the optional types but yes, your logic should work:

@Path(items.{format})
public Response getItems(@PathParam("format") String format) {

}

@Path(items)
public Response getItems() {
    return getItems("json");
}

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