Java EE 8 MVC: Working with query parameters (JSR-371) - DZone Java
A query parameter can automatically convert to a type if the target type matches one of the following rules:
- It is a primitive type
- The type has a constructor that accepts a single String argument
- The type has a static factory method named valueOf() or fromString() with a single String argument
- The type is List<T>, Set<T>; or SortedSet<T> where T matches one of the previous rules
In the previous example, the query parameter id is automatically converted to long. If the id parameter is missing or a conversion to long is not possible, an exception will be thrown. It is also possible to use Long instead of long. In this case, we get null passed to the controller method if the id parameter is missing.
Enums have a valueOf() method by default. So, the query parameter role can automatically be converted to the corresponding enum value.
Using @QueryParam on Fields and Methods
@QueryParam is not limited to method parameters. It is also possible to map query parameters to fields or methods, like the following example shows:
Read full article from Java EE 8 MVC: Working with query parameters (JSR-371) - DZone Java
No comments:
Post a Comment