Using the JAX-RS @BeanParam

There are several ways to pass multiple parameters to Controller Methods. One such option is using multiple @FormParam parameters like this example of a Login Submit @POST method.

Another option is the MultivaluedMap<String, String> option where we convert the map to our POJO.

I propose you consider @BeanParam as a superior approach to multiple parameters. As you can see we are for all practical purposes dealing directly with the POJO, no conversion necessary.

To enable @BeanParam to work we need to specify the @FormParam properties in our POJO.

It's important to note that @BeanParam supports ALL @...Param types, and even @Context. The JAX-RS runtime inspects the @BeanParam parameter’s type for injection annotations and then sets them accordingly. Here's a good example from the Javax Docs on using multiple @...Param types.

In the Form HTML

In the Form HTML there is nothing special we need to do other than be sure our field names correspond to the POJO @FormParam names, like "fullName" in the HTML below.

Testing with @BeanParam

When testing a multiple parameter @POST it's important to remember we are building a FORM as you see in the @FormParam properties here.

Source Code Notes for this Post

Source code discussed in this post is found in my File Upload Download in Bootique Jersey Demo app on GitHub.

More Like This Post