Operations, requests and responses changes
In v2.x of the SDK for Java, requests are passed to a client operation. For example
DynamoDbClient's
PutItemRequest
is passed to DynamoDbClient.putItem
operation. These
operations return a response from the Amazon Web Services service, such as a
PutItemResponse
.
Version 2.x of the SDK for Java has the following changes from 1.x.
-
Operations with multiple response pages now have a
Paginator
method for automatically iterating over all items in the response. -
You cannot mutate requests and responses.
-
You must create requests and responses with a static builder method instead of a constructor. For example, 1.x's
new PutItemRequest().withTableName(...)
is nowPutItemRequest.builder().tableName(...).build()
. -
Operations support a short-hand way to create requests:
dynamoDbClient.putItem(request -> request.tableName(...))
.
Streaming operations
Streaming operations such as Amazon S3 getObject
and putObject
methods now support non-blocking I/O. As a result, the request and response POJOs no longer
take an InputStream
as a parameter. Instead, for synchronous requests the
request object accepts RequestBody
, which is a stream of bytes. The
asynchronous equivalent accepts an AsyncRequestBody
.
Example of Amazon S3 putObject
operation in 1.x
s3client.putObject(BUCKET, KEY, new File(file_path));
Example of Amazon S3 putObject
operation in 2.x
s3client.putObject(PutObjectRequest.builder() .bucket(BUCKET) .key(KEY) .build(), RequestBody.of(Paths.get("myfile.in")));
In parallel, a streaming response object accepts a ResponseTransformer
for
synchronous clients and a AsyncResponseTransformer
for asynchronous
clients.
Example of Amazon S3 getObject
operation in 1.x
S3Object o = s3.getObject(bucket, key); S3ObjectInputStream s3is = o.getObjectContent(); FileOutputStream fos = new FileOutputStream(new File(key));
Example of Amazon S3 getObject
operation in 2.x
s3client.getObject(GetObjectRequest.builder().bucket(bucket).key(key).build(), ResponseTransformer.toFile(Paths.get("key")));
In the SDK for Java 2.x , streaming response operations have an AsBytes
method to
load the response into memory and simplify common in-memory type conversions.