Jackson Module HTTP Query
A jackson module for allowing HTTP queries to be represented as typed objects
Installation
To install from Maven Central:
<dependency>
<groupId>com.github.trickl</groupId>
<artifactId>jackson-module-httpquery</artifactId>
<version>1.0.2</version>
</dependency>
Registering module
Like all standard Jackson modules (libraries that implement Module interface), registration is done as follows:
ObjectMapper mapper = new ObjectMapper();
mapper.registerModule(new HttpQueryModule());
after which functionality is available for all normal Jackson operations.
Purpose
Converts POJOs to query strings and vice versa.
- Use type objects for convenient conversion to query strings (and back).
- Less boilerplate code required for supporting a variety of query combinations.
- Supports existing Jackson JSON annotations for formatting.
- Extra annotations allow for different strategies on handling multi-valued parameters.
Usage Example
@HttpQuery
private static class TypedExample {
private String valueA;
private String valueB;
private int valueC;
}
// To produce "?valueA=test&valueB=testB&valueC=123"...
TypedQuery typed = new TypedQuery(... // omitted for brevity
String queryString = objectMapper.valueToTree(typedQuery).textValue();
Features
- Supports many Jackson annotations
- @JsonProperty (have a different query parameter name from the variable name).
- @JsonIgnore (don't serialize a property)
- @JsonFormat (useful for date strings).
- Supports new parameters
- @HttpQuery (required to serialize to a query string, not a JSON object).
- @HttpQueryDelimited (serialize a multi valued property using a delimited list e.g "?values=1,2,3").
- @HttpQueryNoValue (allow boolean valueless params, e.g. "?debug")