55

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=?)

AlikElzin-kilaka
  • 34,335
  • 35
  • 194
  • 277
  • Take a look here [https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html](https://docs.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html) – Valijon Feb 25 '16 at 10:53
  • @Valijon - You're saying that the REST default values are similar to Java's default class members? – AlikElzin-kilaka Feb 25 '16 at 10:59
  • they aren't similar, they are the same thing, since this is an implementation issue. REST does not mandate "default values" for parameters since it's not a specification. – francesco foresti Feb 25 '16 at 11:01

2 Answers2

111

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) {
    ....
}
cassiomolin
  • 124,154
  • 35
  • 280
  • 359
2

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.

francesco foresti
  • 2,004
  • 20
  • 24