Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the default values for @QueryParam when @DefaultValue is not specified?

For example, having the following Java rest definition:

@GET @Path("/something") public String somthing(     @QueryParam("valString") String valString,     @QueryParam("valInt") int valInt,     @QueryParam("valBool") boolean valBool ) {   ... } 

And invocation:

curl -X GET 127.0.0.1/something 

What will the parameters values be if not specified in the invocation? (valString=?, valInt=?, valBool=?)

like image 941
AlikElzin-kilaka Avatar asked Feb 25 '16 10:02

AlikElzin-kilaka


People also ask

What is the @QueryParam annotation?

Annotation Type QueryParamBinds the value(s) of a HTTP query parameter to a resource method parameter, resource class field, or resource class bean property. Values are URL decoded unless this is disabled using the Encoded annotation. A default value can be specified using the DefaultValue annotation.

What is @QueryParam in REST API?

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.

Is query parameter optional?

As query parameters are not a fixed part of a path, they can be optional and can have default values.

What is @QueryParam?

@QueryParam: QueryParam is used when the requirement is to filter the request based on certain criteria/criterias.


2 Answers

Short answer

The parameter values will be:

  • valString: null
  • valInt: 0
  • valBool: false

A bit longer answer

Quoting the Java EE 7 tutorial about extracting request parameters:

If @DefaultValue is not used in conjunction with @QueryParam, and the query parameter is not present in the request, the value will be an empty collection for List, Set, or SortedSet; null for other object types; and the default for primitive types.

The default values for primitive types are described in the Java Tutorials from Oracle:

 Primitive       Default Value -------------------------------  byte            0  short           0  int             0  long            0L  float           0.0f  double          0.0d  char            '\u0000'  boolean         false 

As you already know, this behavior can be changed by using the @DefaultValue annotation as following:

@GET @Path("/foo") public String myMethod(@DefaultValue("foo") @QueryParam("valString") String valString,                        @DefaultValue("1") @QueryParam("valInt") int valInt,                        @DefaultValue("true") @QueryParam("valBool") boolean valBool) {     .... } 
like image 117
cassiomolin Avatar answered Sep 21 '22 22:09

cassiomolin


the values will be null, 0, false, i.e. the default values for non-initialized variables of those types. If the client does not put the parameters in the URL and the service does not specify default values, what the service will get are Java default values for non-initialized variables.

like image 41
francesco foresti Avatar answered Sep 22 '22 22:09

francesco foresti