Skip to main content

Improved Java Code Samples

· 7 min read

The generated code samples are now idiomatic with no errors. This improves the developer experience by allowing developers to run the code samples without any error in their applications and to get started quickly.

Details

This release introduces significant improvements to the code samples generated for your API. We've made several updates to the code samples to ensure that they are idiomatic, easier to understand and production-ready. The new code samples are designed to enhance the developer experience that will allow them to grasp the intention of an endpoint as to what are the parameters, how they are being initialized. This will let them get started with your API quickly and easily by skimming through the code samples.

What Has Changed?

To meet the standards, the updated code samples are passed through the linter i.e. CheckStyle (Click here to see the configuration being used).

Following major improvements are now made part of the Java code samples:

  • Simplified code samples to make them easier to read and understand with the help of properly indented code samples and inline-initialization of the parameters.
  • Removed unused imports for models and utility classes.
  • Fixed linting issues in the updated code samples to ensure that they comply with the best practices in terms of coding styles and standards.
  • Fixed bugs and errors in the code samples to ensure that they work as expected.

Imports Section Improvements

Wildcard Imports

Wildcard imports have been replaced with the specific imports.

import localhost3000.TesterClient;
import localhost3000.controllers.ResponseTypesController;
import localhost3000.models.Employee;
import java.util.List;
import java.util.Map;
import java.util.Arrays;
import localhost3000.models.Person;
import localhost3000.DateTimeHelper;
import localhost3000.models.Days;
import java.util.LinkedHashMap;

Unused Imports

Previously imports are added even if they are not required in any other sections, but now we are adding imports on demand.

// imports section
import java.util.List;
import java.util.Arrays;

// parameters initialization section
List<String> optionalParam = Arrays.asList(
"Bob",
"Alice",
"John",
"Doe"
);
String requiredParam = 'some string value';

After removing the Optional parameter optionalParam from the API call, all related imports will vanish:

// empty imports section

// parameters initialization section
String requiredParam = 'some string value';

Parameters Initialization Section Improvements

Model Initialization

The in-line initlization has been added to ensure that no intermediate variables should be used in the code samples and they are easier to understand, and more user-friendly.

Employee model = new Employee.Builder(
"John",
DateTimeHelper.fromRfc1123DateTime("Mon, 15 Jun 2009 20:45:30 GMT"),
new Person.Builder(
"Doe",
40
)
.build()
)
.build();

Date Initialization

Now the code samples make use of the utility method for parsing a date.

LocalDate dateOfBirth = DateTimeHelper.fromSimpleDate("2016-03-13");

DateTime Initialization

Now the code samples make use of the utility methods for parsing date-time of different formats.

RFC-3339 Initialization

LocalDateTime rfc3339DateTime = DateTimeHelper.fromRfc8601DateTime("2016-03-13T12:52:32.123Z");

UNIX Timestamp Initialization

LocalDateTime unixTimestamp = DateTimeHelper.fromUnixTimestamp(1480809600L);

Array Initialization

Example 1

List<String> stringArray = Arrays.asList(
"Bob",
"Alice",
"John",
"Doe"
);

Example 2

List<Map<String, Integer>> numberArrayOfMap = Arrays.asList(
new LinkedHashMap<String, Integer>() {{
put("key0", 216);
}},
new LinkedHashMap<String, Integer>() {{
put("key0", 217);
put("key1", 218);
}}
);

Map Initialization

Example 1

Map<String, Integer> numberMap = new LinkedHashMap<String, Integer>() {{
put("key0", 110);
put("key1", 111);
put("key2", 112);
}};

Example 2

Map<String, List<Integer>> numberMapOfArray = new LinkedHashMap<String, List<Integer>>() {{
put("key0", Arrays.asList(
0,
1
));
put("key1", Arrays.asList(
1,
2,
3
));
put("key2", Arrays.asList(
2
));
}};

Multi-Dimensional Array Initialization

List<List<Integer>> numbers2dArray = Arrays.asList(
Arrays.asList(
158,
160
),
Arrays.asList(
168,
170
)
);

API Call Section Improvements

The API call will print the response in case of success or exception in case of an error.

ResponseTypesController responseTypesController = client.getResponseTypesController();
responseTypesController.sendDataAsync(param1, param2, param3).thenAccept(result -> {
// TODO success callback handler
System.out.println(result);
}).exceptionally(exception -> {
// TODO failure callback handler
exception.printStackTrace();
return null;
});

A Comparison of Complete Example Code Sample

The following code shows the complete comparison between the code sample generation for initializing an employee model as an input parameter for ResponseTypesController Send endpoint.

Employee Model

"employee_model": {
"name": "John",
"hiredAt": "Mon, 15 Jun 2009 20:45:30 GMT",
"salary": null,
"boss": {
"name": "Doe",
"age": 40
}
}
package com.example;

import localhost3000.TesterClient;
import localhost3000.controllers.ResponseTypesController;
import localhost3000.models.Employee;
import localhost3000.models.Person;
import localhost3000.DateTimeHelper;

public class Program {

public static void main(String[] args) {
TesterClient client = new TesterClient.Builder()
.httpClientConfig(configBuilder -> configBuilder
.timeout(0))
.port("80")
.build();

ResponseTypesController responseTypesController = client.getResponseTypesController();

Employee model = new Employee.Builder(
"John",
DateTimeHelper.fromRfc1123DateTime("Mon, 15 Jun 2009 20:45:30 GMT"),
new Person.Builder(
"Doe",
40
)
.build()
)
.build();

responseTypesController.sendAsync(model).thenAccept(response -> {
// TODO success callback handler
System.out.println(response);
}).exceptionally(exception -> {
// TODO failure callback handler
exception.printStackTrace();
return null;
});
}
}

Bugs Fixed

  • Fixed an issue with void type response handling for synchronous API calls.