Over a million developers have joined DZone.

Java EE 8 MVC: Working with query parameters (JSR-371)

Earlier we looked at Java EE MVC Framework Controllers. Here's how to query parameters and more!

· Java Zone

Learn more about how the Java language, tools and frameworks have been the foundation of countless enterprise systems, brought to you in partnership with Salesforce.

In the previous post about the new Java EE MVC Framework, we had a detailed look on Controllers. In this and the following posts, we will see how to access various types of request data in MVC Controllers.

Java EE MVC makes heavy use of JAX-RS and most of the things we will see in this and the next posts are JAX-RS features. So, if you are familiar with JAX-RS you probably will not learn much new in this post.

Query Parameters

This post focuses on query parameters. If you read my Java EE MVC Getting started post, you might already know the @QueryParam annotation. The most common use case of  @QueryParam is to map a query parameter to a controller method parameter.

For example:

public class QueryParamsController {

  public String queryParams(@QueryParam("name") String name) {

If we now send an HTTP GET request to


The string "john" will be passed as name parameter to the method queryParams().

Type Conversion

With @QueryParam query parameters can automatically be converted to various types.

For example:

public enum Role {
  admin, reporter, accountant
public class QueryParamsController {

  public String queryParams(
      @QueryParam("id") long id,
      @QueryParam("name") String name,
      @QueryParam("role") Role role) {


We can now send a request like this:


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:

public class QueryParamsFieldController {

  private Long id;

  private Role role;

  private String name;

  public void setName(String name) {
    this.name = name;

  public String queryParams() {
    // use id, role and name

If we now send an HTTP GET request to


the parameters are set to the fields id, role and name (via setName()) before queryParams() is called.

Do not forget that a new instance of the class is created for every request, so it is safe to have fields that contain request information.

Quick Summary

The @QueryParam annotation can be used to obtain query parameters. @QueryParam can be used on fields, methods and method parameters. Query parameters can be automatically converted to various types, as long as the target type is a primitive type, contains a String constructor or contains valueOf() or fromString() factory methods.

You can find the source code for all the shown examples on GitHub.

In the next post, we will see how to work with path parameters.

Discover how the Force.com Web Services Connector (WSC) is a code-generation tool and runtime library for use with Force.com Web services, brought to you in partnership with Salesforce.

java 8

Published at DZone with permission of Michael Scharhag, DZone MVB. See the original article here.

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}