Reputation: 115
I have resource class - UserResource interface and I have defined an endpoint as getUsers.
Now I want to filter these users based on users status - (Active, Inactive) and its not mandatory so if I does not pass the status it will fetch all the users.
Now the question is should I pass this as @QueryParam or get it from
HttpServletRequest - httpServletRequest.getParameter("Status").
Which one from the above two is best way and in what scenario I should use which one.
1. First way is pass the status as query param and define in the resource file itself. Here UserResource is the controller interface or resource class. In getUsers method has @QueryParam.
import javax.ws.rs.core.Response;
@Path(/user)
public interface UserResource{
@GET
@Path("/")
@Produces({ MediaType.APPLICATION_JSON })
Response getUsers(@QueryParam("status") String status);
}
@Component
Public class UsersResourceImpl implement UserResource{
public Response getPlan(String status){
String userStatus = status;
// some logic
}
}
2. Second way is get the query param from HttpServletRequest. so I have autowired the HttpServletRequest and getting the query param from the httpservletrequest.
import javax.ws.rs.core.Response;
@Path(/user)
public interface UserResource {
@GET
@Path("/")
@Produces({ MediaType.APPLICATION_JSON })
Response getUsers();
}
import javax.servlet.http.HttpServletRequest;
@Component
Public class UsersResourceImpl implements UserResource{
@Autowired
private HttpServletRequest httpRequest;
public Response getPlan(String status){
String status = httpRequest.getParameter(status)
// some logic
}
}
'''
Upvotes: 1
Views: 1338
Reputation: 130947
Well, I honestly don't see any appealing reason to avoid using the @QueryParam
annotation given that you need the value from a query parameter.
Some benefits of using @QueryParam
that I can think of:
The @QueryParam
annotation will automatically bind the value(s) of a query parameter to a resource method parameter, resource class field, or resource class bean property. So you won't need to extract and parse parameters manually, once you respect some rules described in the documentation:
The type
T
of the annotated parameter, field or property must either:
- Be a primitive type
- Have a constructor that accepts a single String argument
- Have a static method named
valueOf
orfromString
that accepts a singleString
argument (see, for example,Integer.valueOf(String)
)- Have a registered implementation of
ParamConverterProvider
JAX-RS extension SPI that returns aParamConverter
instance capable of a "from string" conversion for the type. BeList<T>
,Set<T>
orSortedSet<T>
, where T satisfies 2, 3 or 4 above. The resulting collection is read-only.
@QueryParam
can be combined with @DefaultValue
to define a default value for the parameter in case it's not present in the request.
If multiple endpoints support the same query parameters, you could aggregate them in a class and receive an instance of such class as a @BeanParameter
.
Upvotes: 1
Reputation: 2119
Go with annotations (i.e @QueryParam) that's why we chose such framework , remember convention over configuration.
Upvotes: 0