Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 236
Amir Raminfar Avatar asked Jul 16 '26 05:07

Amir Raminfar


1 Answers

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);
 }    
}
like image 53
Luciano Fiandesio Avatar answered Jul 17 '26 18:07

Luciano Fiandesio



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!