Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Required @QueryParam in JAX-RS (and what to do in their absence)

Tags:

java

jax-rs

I deploy a web-services component to JBoss Application Server 7 using the RESTEasy JAX-RS implementation.

Is there an annotation available to declare required, mandatory @QueryParam parameters in JAX-RS ? And, if not, what is the 'standard' way to deal with situations where such parameters are missing?

My web service (resource) methods return JSON-stringified results when properly invoked with all the mandatory arguments, but I'm not sure what is the best way to indicate to the caller that a required parameter was missing.

like image 500
Marcus Junius Brutus Avatar asked Dec 20 '12 08:12

Marcus Junius Brutus


People also ask

What is the @QueryParam annotation?

The @QueryParam annotation allows you to map a URI query string parameter or url form encoded parameter to your method invocation.

What's the difference between @PathParam and @QueryParam restful annotations?

PathParam could be used to drill down to entity class hierarchy. Whereas, QueryParam could be reserved for specifying attributes to locate the instance of a class. For example, /Vehicle/Car?

Can query Param be optional?

Query parameters allow you to pass optional parameters like page number to the component. In this tutorial, we look at how to pass the query parameters using the queryParams directive.

What is @QueryParam in Java?

Basically, @QueryParam denotes that the value of the Query Parameter with the corresponding name will be parsed, and if parsed correctly it will be available on the method argument denoted with @QueryParam . There are baically two ways to pass parameters in a GET request in REST services.


2 Answers

Good question. Unfortunately (or maybe fortunately) there is no mechanism in JAX-RS to make any params mandatory. If a parameter is not supplied it's value will be NULL and your resource should deal with it accordingly. I would recommend to use WebApplicationException to inform your users:

@GET @Path("/some-path") public String read(@QueryParam("name") String name) {   if (name == null) {     throw new WebApplicationException(       Response.status(Response.Status.BAD_REQUEST)         .entity("name parameter is mandatory")         .build()     );   }   // continue with a normal flow } 
like image 200
yegor256 Avatar answered Sep 21 '22 07:09

yegor256


You can use javax.validation annotations to enforce that the parameters are mandatory by annotating them with @javax.validation.constraints.NotNull. See an example for Jersey and one for RESTeasy.

So your method would simply become:

@GET @Path("/some-path") public String read(@NotNull @QueryParam("name") String name) {   String something =    // implementation   return something; } 

Note that the exception gets then translated by the JAX-RS provider to some error code. It can usually be overridden by registering your own implementation of javax.ws.rs.ext.ExceptionMapper<javax.validation.ValidationException>.

This provides a centralized way to translate mandatory parameter to error responses and no code duplication is necessary.

like image 31
Giovanni Botta Avatar answered Sep 22 '22 07:09

Giovanni Botta