Java APIs

How to Use the Spring Example API

First, define a probe (an instance of the entity class): User user = new User();user.setName(“Ana”);user.setEmail(“[email protected]”); Next create a probe: Example userExample = Example.of(user); Next, define a repository that extends QueryByExampleExecutor interface: @Repository public interface UserRepository extends JpaRepository, QueryByExampleExecutor { } Finally, call one of the QueryByExampleExecutor methods (e.g., exists()): public

Trigger a GET Request via the JDK 11 HttpClient API

The JDK 11 HttpClient API is very flexible and intuitive. Here it is a sample of triggering a GET request and printing the response: HttpClient client = HttpClient.newHttpClient();HttpRequest request = HttpRequest.newBuilder() .uri(URI.create(“https://reqres.in/api/users/4”)) .build();HttpResponse response= client.send(request, BodyHandlers.ofString());System.out.println(“Status code: ” + response.statusCode());System.out.println(” Body: ” + response.body());

Get the Class-Level Annotation

Via Java Reflection API, we can obtain all the annotations of a class as follows: Class clazz = Foo.class;Annotation[] clazzAnnotations = clazz.getAnnotations();

How to Check Whether a Number Is Prime

Java has an API called BigInteger and it is easily able to identify if the number under consideration is a prime number or not. Code snippet: import java.math.BigInteger;public class ProbablePrime{ public static void main(String[] args) { ProbablePrime probablePrime = new ProbablePrime(); int numberArg = Integer.parseInt(args[0]); probablePrime.proceed(numberArg); } private void proceed(int

Does Your Timezone Support Daylight Saving Time?

You may want to know if your timezone supports Daylight Saving Time programmatically. This can be easily determined with the code below: Listing 1. Sample Code import java.util.TimeZone;public class DaylightSavingsTime{ public static void main(String args[]) { DaylightSavingsTime daylightSavingsTime = new DaylightSavingsTime(); daylightSavingsTime.proceed(); } private void proceed() { TimeZone tz =