Skip to main content

POJO Marshalling

By default, JSON support is provided for HTTP request and response bodies.

Other languages can be specified using any of the following builder methods:

RestClient.Builderjson()json5()xml()html()plainText()msgPack()uon()urlEnc()openApi()
Example
// Create a basic REST client with JSON 5 support.
// Typically easier to use when performing unit tests.
RestClient client = RestClient.create().json5().build();

Clients can also support multiple languages:

Example
// Create a REST client with support for multiple languages.
RestClient client1 = RestClient.create().json().xml().openApi().build();

// Create a REST client with support for all supported languages.
RestClient client2 = RestClient.create().universal().build();

When using clients with multiple language support, the request language is selected by setting the Content-Type request header.

// Create a REST client with support for multiple languages.
RestClient client = RestClient.create().universal().build();

client.post(URI, myBean)
.contentType("application/json")
.complete()
.assertStatus().asCode().is(200);

Languages can also be specified per-request.

// Create a REST client with no default languages supported.
RestClient client = RestClient.create().build();

// Use JSON for this request.
client.post(URI, myBean)
.json()
.complete()
.assertStatus().asCode().is(200);

The RestClient.Builder class provides convenience methods for setting common serializer and parser settings.

Example
// Create a basic REST client with JSON support.
// Use single-quotes and whitespace.
RestClient client1 = RestClient.create().json().sq().ws().build();

Other methods are also provided for specifying the serializers and parsers used for lower-level marshalling support:

RestClient.Builderserializer(Serializer)parser(Parser)marshaller(Marshaller)

HTTP parts (headers, query parameters, form data...) are serialized and parsed using the HttpPartSerializer and HttpPartParser APIs. By default, clients are configured to use OpenApiSerializer and OpenApiParser. These can be overridden using the following methods:

RestClient.BuilderpartSerializer(Class<? extends HttpPartSerializer>)partParser(Class<? extends HttpPartParser>)

Session Options via HTTP​

The Juneau REST client supports sending serializer and parser session properties via HTTP headers or query parameters. This allows controlling per-request marshalling behavior when the server has enabled it:

RestClient client = RestClient.create()
.rootUrl("http://localhost:8080/myapi")
.build();

// Send serializer options via header (JSON5 format)
client.get("/mybean")
.serializerSessionOptionsHeader("{useWhitespace:true}")
.run()
.getContent().as(MyBean.class);

// Send parser options via query parameter (UON format)
client.post("/mybean", body)
.parserSessionOptionsQuery("(trimStrings=true)")
.run();

// Set default options for all requests via builder
RestClient clientWithDefaults = RestClient.create()
.rootUrl("http://localhost:8080/myapi")
.serializerSessionOptionsHeader("{addBeanTypes:false}")
.parserSessionOptionsQueryDefault("(trimStrings=true)")
.build();

See Session Options via HTTP for complete documentation on server configuration and security.