Amir Raminfar
Amir Raminfar

Reputation: 34179

Java JAX-RS custom parameter with default value

Let's say I have this (this is just a sample):

@GET
@Path(value="address")
@Produces("application/json")
public Response getAddress(@QueryParam("user") User user){
  ...
}

and User is

class User{
...
 public static User valueOf(String user){
   if(user == null) return DEFAULT_USER;
   return dao.findById(user);
 }    
}

If I do /api/address?user=amir everything works but the idea is if I don't provide a value for user then I want DEFAULT_USER to be used. But this doesn't actually call valueOf. Is there a way to fix this?

Upvotes: 3

Views: 5709

Answers (1)

Luciano Fiandesio
Luciano Fiandesio

Reputation: 10215

JAX-RS has the @DefaultValue annotation:

@QueryParam("user") @DefaultValue("__DEFAULT")



class User{
...
 public static User valueOf(String user){
   if(user == null||"__DEFAULT".equals(user) return DEFAULT_USER;
   return dao.findById(user);
 }    
}

Upvotes: 6

Related Questions