Reputation: 129
I have a service in rest that looks like:
@GET
@Path("get-policy/{policyNumber}/{endorsement}/{type}")
@Produces(MediaType.APPLICATION_XML)
public String getPolicyIndividual(
@PathParam("policyNumber")String policyNumber,
@PathParam("endorsement")String endorsement,
@PathParam("type")String type){
...
}
And i want to know if there is a way that i can accept every parameter as null value if they are not sent, so if somene makes a call to my service without the params or with not all the params still can match the definition of my service. Examples:
http://localhost:8080/service/policy/get-policy/
or this:
http://localhost:8080/service/policy/get-policy/5568
or this:
http://localhost:8080/service/policy/get-policy/5568/4
Im well aware that i can define a regex expression like in this answer, but in that case there was only 1 path param defined, what if i have more than one?
That didnt work for me but maybe im doing something wrong, i tried this with no success:
@GET
@Path("get-policy/{policyNumber: .*}/{endorsement: .*}/{type: .*}")
@Produces(MediaType.APPLICATION_XML)
public String getPolicyIndividual(
@PathParam("policyNumber")String policyNumber,
@PathParam("endorsement")String endorsement,
@PathParam("type")String type){
...
}
is the only way to achive this trough a POST? Im using Jersey btw!
Upvotes: 1
Views: 4704
Reputation: 667
You have to create a complete use case scenario for this and call a general method every time if you dont want to write code multiple times. Say: For an instance use only one parameter passed, then 2 and then all, and none
@GET
@Path("get-policy/{policyNumber: .*}")
@Produces(MediaType.APPLICATION_XML)
public String getPolicyIndividual(
@PathParam("policyNumber")String policyNumber)
{
doSomething(policyNumber, "", "");
}
@GET
@Path("get-policy/{policyNumber: .*}/{endorsement: .*}")
@Produces(MediaType.APPLICATION_XML)
public String getPolicyIndividual(
@PathParam("policyNumber")String policyNumber,
@PathParam("endorsement")String endorsement)
{
doSomething(policyNumber,endorsement, "");
}
Upvotes: 1