Remove "/" from api call if optional parameter is null
We are using RESTful Web Services (Jersey) for API calls in java. Although the API needs an extra parameter, we do it like:
api-interface/user/userid/9000/companyid/90909/{optionalparameter*}
and we have to call it api when there is no optional parameter:
api-interface/user/userid/9000/companyid/90909/
What do you need:
Case: 1 If an optional parameter exists
api-interface/user/userid/9000/companyid/90909/name/john/address/MA/age/34
Case: 2 If the optional parameter does not exist.
api-interface/user/userid/9000/companyid/90909
My current implementation:
@GET
@Path("user/companyid/{companyid}/userid/{userid}/{optionalparameter:.*}")
@Produces(MediaType.APPLICATION_JSON)
public Response getUserList(@PathParam("companyid") String companyId, @PathParam("userid") String userId,
@PathParam("optionalparameter") String syncDate) throws BadRequestException, InternalServerException {
//parsing the param.
}
In the above code, I need to add a trailing "/", but I am looking for a way to remove this trailing "/" if someone doesn't want to pass these parameters.
I followed this link but it didn't work and the previous parameter length is greater than 1.
Please suggest me a better way.
source to share
After looking at my link , have you tried this:
@Path("userid/{userid}/companyid/{companyid}{optparam:(/[^/]+?)*}")
public Response getLocation(
@PathParam("userid") int userid,
@PathParam("companyid") int companyid,
@PathParam("optparam") String optparam) {
String[] params = parseParams(optparam);
...
}
private String[] parseParams(String params) {
if (params.startsWith("/")) {
params = path.substring(1);
}
return params.split("/");
}
This should work, giving you all the parameters in one array.
EDIT . I updated the search bar and checked it when installing locally.
source to share