Building a Hypermedia-Driven RESTful Web Service using Heteoas

                           Spring Boot Hateoas Example

Purpose: In this post, we will learn how we can create a Hibermedia link in API and Testing in spring boot. The steps are given below.

1. Setup and Create Project: Visit hereSpring Boot Rest API Hello World Examples.

Next Step. Spring Boot Parent Dependencies.


<parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.3.0.M1</version>
        <relativePath /> <!-- lookup parent from repository -->
    </parent>
Next Step. Add Heteoas Dependencies.
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-hateoas</artifactId>
</dependency>
Next Step. Spring Boot main class.

Class: Application.java

Note: @SpringBootApplication=@Configuration+ @EnableAutoConfiguration+ @ComponentScan.



package com.bce;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

@SpringBootApplication
public class Application {
    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }

}


Next Step. Create Rest Controller.
package com.bce.controller;

import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.linkTo;
import static org.springframework.hateoas.server.mvc.WebMvcLinkBuilder.methodOn;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.hateoas.EntityModel;
import org.springframework.hateoas.server.mvc.WebMvcLinkBuilder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import com.bce.model.Employee;
import com.bce.service.EmployeeService;

@RestController
public class EmployeeController {

    @Autowired
    EmployeeService employeeService;

    @GetMapping("/employee")
    public List<Employee> findAll() {
        return employeeService.getAllEmployee();
    }

    @GetMapping("/employee/{id}")
    public EntityModel<Employee> findEmployeeById(@PathVariable long id) {
        EntityModel<Employee> resource = EntityModel.of(employeeService.getEmployeeById(id));
        WebMvcLinkBuilder linkTo = linkTo(methodOn(this.getClass()).findAll());
        resource.add(linkTo.withRel("all-employees"));
        return resource;
    }

}

Next Step. Server Port:  The default server port is 8080. Wish you want to change open application.properties and add the below line.

server.port=8081





Download Code from GitHub.  Download

Arrays.parallelSort(byte[]) in Java

Arrays.parallelSort(byte[]): This method is available in java.util.Arrays class of Java.

Syntax:

void java.util.Arrays.parallelSort(byte[] a)

This method takes one argument of type byte array as its parameter. This method sorts the specified array into ascending numerical order.

Parameters: One parameter is required for this method.

a: the array to be sorted.

Returns: NA

Exceptions: NA

Approach

Java

import java.util.Arrays;

public class ArraysparallelSortbyte {
    public static void main(String[] args) {

        byte a[] = { 12, 77, 56, 18 };
        Arrays.parallelSort(a);

        System.out.println(Arrays.toString(a));
    }
}

Output:

[12, 18, 56, 77]


Arrays.parallelSetAll(long[], IntToLongFunction) in Java

Arrays.parallelSetAll(long[], IntToLongFunction): This method is available in java.util.Arrays class of Java.

Syntax:

void java.util.Arrays.parallelSetAll(long[] array, IntToLongFunction generator)

This method takes two arguments one of type long array and the other of type IntToLongFunction as its parameters. This method sets all elements of the specified array, in parallel, using the provided generator function to compute each element.

Note: If the generator function throws an exception, an unchecked exception is thrown from parallelSetAll and the array is left in an indeterminate state.

Parameters: Two parameters are required for this method.

array: array to be initialized.

generator: a function accepting an index and producing the desired value for that position.

Throws:

NullPointerException - if the generator is null.

Approach 1: When no exceptions

Java

import java.util.Arrays;
import java.util.function.IntToLongFunction;

public class ArraysparallelSetAlllong {
    public static void main(String[] args) {

        long array[] = { 12, 34, 56, 78 };

        IntToLongFunction generator = intVal -> {
            long doubleVal = intVal;
            return doubleVal;
        };

        Arrays.parallelSetAll(array, generator);

        System.out.println(Arrays.toString(array));
    }
}

Output:

[0, 1, 2, 3]


Approach 2: NullPointerException 

Java

import java.util.Arrays;
import java.util.function.IntToLongFunction;

public class ArraysparallelSetAlllong {
    public static void main(String[] args) {

        long array[] = { 12, 34, 56, 78 };

        IntToLongFunction generator = null;

        Arrays.parallelSetAll(array, generator);

        System.out.println(Arrays.toString(array));
    }
}

Output:

Exception in thread "main" java.lang.NullPointerException at java.base/java.util.Objects.requireNonNull(Objects.java:208) at java.base/java.util.Arrays.parallelSetAll(Arrays.java:5199)


Arrays.parallelSetAll(int[], IntUnaryOperator) in Java

Arrays.parallelSetAll(int[], IntUnaryOperator): This method is available in java.util.Arrays class of Java.

Syntax:

void java.util.Arrays.parallelSetAll(int[] array, IntUnaryOperator generator)

This method takes two arguments one of type int array and the other of type IntUnaryOperator as its parameters. This method sets all elements of the specified array, in parallel, using the provided generator function to compute each element.

Note: If the generator function throws an exception, an unchecked exception is thrown from parallelSetAll and the array is left in an indeterminate state.

Parameters: Two parameters are required for this method.

array: array to be initialized generator a function accepting an index and producing the desired value for that position.

Throws:

NullPointerException - if the generator is null.

Approach 1: When no exceptions

Java

import java.util.Arrays;
import java.util.function.IntUnaryOperator;

public class ArraysparallelSetAllint {
    public static void main(String[] args) {

        int array[] = { 12, 34, 56, 78 };

        IntUnaryOperator generator = intVal -> {
            int doubleVal = intVal;
            return doubleVal;
        };

        Arrays.parallelSetAll(array, generator);

        System.out.println(Arrays.toString(array));
    }
}

Output:

[0, 1, 2, 3]


Approach 2: NullPointerException 

Java

import java.util.Arrays;
import java.util.function.IntUnaryOperator;

public class ArraysparallelSetAllint {
    public static void main(String[] args) {

        int array[] = { 12, 34, 56, 78 };

        IntUnaryOperator generator = null;

        Arrays.parallelSetAll(array, generator);

        System.out.println(Arrays.toString(array));
    }
}

Output:

Exception in thread "main" java.lang.NullPointerException at java.base/java.util.Objects.requireNonNull(Objects.java:208) at java.base/java.util.Arrays.parallelSetAll(Arrays.java:5144)


Arrays.parallelSetAll(double[], IntToDoubleFunction) in Java

Arrays.parallelSetAll(double[], IntToDoubleFunction): This method is available in java.util.Arrays class of Java.

Syntax:

void java.util.Arrays.parallelSetAll(double[] array, IntToDoubleFunction generator)

This method takes two arguments one of type double array and the other of type IntToDoubleFunction as its parameters. This method set all elements of the specified array, in parallel, using the provided generator function to compute each element.

Note: If the generator function throws an exception, an unchecked exception is thrown from parallelSetAll and the array is left in an indeterminate state.

Parameters: Two parameters are required for this method.

array: array to be initialized.

generator: a function accepting an index and producing the desired value for that position.

Throws:

NullPointerException - if the generator is null.

Approach 1: When no exceptions

Java

import java.util.Arrays;
import java.util.function.IntToDoubleFunction;

public class ArraysparallelSetAlldouble {
    public static void main(String[] args) {

        double array[] = { 12, 34, 56, 78 };

        IntToDoubleFunction generator = intVal -> {
            double doubleVal = intVal;
            return doubleVal;
        };

        Arrays.parallelSetAll(array, generator);

        System.out.println(Arrays.toString(array));
    }
}

Output:

[0.0, 1.0, 2.0, 3.0]


Approach 2: NullPointerException

Java

import java.util.Arrays;
import java.util.function.IntToDoubleFunction;

public class ArraysparallelSetAlldouble {
    public static void main(String[] args) {

        double array[] = { 12, 34, 56, 78 };

        IntToDoubleFunction generator = null;

        Arrays.parallelSetAll(array, generator);

        System.out.println(Arrays.toString(array));
    }
}

Output:

Exception in thread "main" java.lang.NullPointerException at java.base/java.util.Objects.requireNonNull(Objects.java:208) at java.base/java.util.Arrays.parallelSetAll(Arrays.java:5254)


Arrays.parallelPrefix(long[], int, int, LongBinaryOperator) in Java

Arrays.parallelPrefix(long[], int, int, LongBinaryOperator): This method is available in java.util.Arrays class of Java.

Syntax:

void java.util.Arrays.parallelPrefix(long[] array, int fromIndex, int toIndex, LongBinaryOperator op)

This method takes four arguments one of type long array and the other two are of type int and the other one of type LongBinaryOperator as its parameters. This method performs parallelPrefix(long [], LongBinaryOperator)for the given subrange of the array.

Parameters: Four parameters are required for this method.

array: the array.

fromIndex: the index of the first element, inclusive.

toIndex: the index of the last element, exclusive.

op: a side-effect-free, associative function to perform the accumulation

Throws:

1. IllegalArgumentException - if fromIndex > toIndex.

2. ArrayIndexOutOfBoundsException - if fromIndex < 0 or toIndex > array.length.

3. NullPointerException - if the specified array or function is null.

Approach 1: When no exceptions

Java

import java.util.Arrays;
import java.util.function.LongBinaryOperator;

public class ArraysparallelPrefixlongrange {
    public static void main(String[] args) {

        long array[] = { 1, 2, 3, 4, 10, 23, 45, 56, 78 };

        int fromIndex = 2, toIndex = 4;
        LongBinaryOperator sum = (d1, d2) -> d1 + d2;
        Arrays.parallelPrefix(array, fromIndex, toIndex, sum);

        System.out.println(Arrays.toString(array));
    }
}

Output:

[1, 2, 3, 7, 10, 23, 45, 56, 78]


Approach 2: IllegalArgumentException

Java

import java.util.Arrays;
import java.util.function.LongBinaryOperator;

public class ArraysparallelPrefixlongrange {
    public static void main(String[] args) {

        long array[] = { 1, 2, 3, 4, 10, 23, 45, 56, 78 };

        int fromIndex = 5, toIndex = 4;
        LongBinaryOperator sum = (d1, d2) -> d1 + d2;
        Arrays.parallelPrefix(array, fromIndex, toIndex, sum);

        System.out.println(Arrays.toString(array));
    }
}

Output:

Exception in thread "main" java.lang.IllegalArgumentException: fromIndex(5) > toIndex(4) at java.base/java.util.Arrays.rangeCheck(Arrays.java:718) at java.base/java.util.Arrays.parallelPrefix(Arrays.java:1456)



Approach 3: ArrayIndexOutOfBoundsException 

Java

import java.util.Arrays;
import java.util.function.LongBinaryOperator;

public class ArraysparallelPrefixlongrange {
    public static void main(String[] args) {

        long array[] = { 1, 2, 3, 4, 10, 23, 45, 56, 78 };

        int fromIndex = -1, toIndex = 4;
        LongBinaryOperator sum = (d1, d2) -> d1 + d2;
        Arrays.parallelPrefix(array, fromIndex, toIndex, sum);

        System.out.println(Arrays.toString(array));
    }
}

Output:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Array index out of range: -1 at java.base/java.util.Arrays.rangeCheck(Arrays.java:722) at java.base/java.util.Arrays.parallelPrefix(Arrays.java:1456)



Approach 4: NullPointerException

Java

import java.util.Arrays;
import java.util.function.LongBinaryOperator;

public class ArraysparallelPrefixlongrange {
    public static void main(String[] args) {

        long array[] = null;

        int fromIndex = 2, toIndex = 4;
        LongBinaryOperator sum = (d1, d2) -> d1 + d2;
        Arrays.parallelPrefix(array, fromIndex, toIndex, sum);

        System.out.println(Arrays.toString(array));
    }
}

Output:

Exception in thread "main" java.lang.NullPointerException: Cannot read the array length because "array" is null at java.base/java.util.Arrays.parallelPrefix(Arrays.java:1456)



Arrays.parallelPrefix(long[], LongBinaryOperator) in Java

Arrays.parallelPrefix(long[], LongBinaryOperator): This method is available in java.util.Arrays class of Java.

Syntax:

void java.util.Arrays.parallelPrefix(long[] array, LongBinaryOperator op)

This method takes two arguments one of type long array and the other of type LongBinaryOperator as its parameters. This method cumulates, in parallel, each element of the given array in place,using the supplied function.

Parameters: Two parameters are required for this method.

array: the array, which is modified in-place by this method.

op: a side-effect-free, associative function to perform the accumulation.

Throws:

NullPointerException - if the specified array or function is null.

Approach 1: When no exceptions

Java

import java.util.Arrays;
import java.util.function.LongBinaryOperator;

public class ArraysparallelPrefixlong {
    public static void main(String[] args) {

        long array[] = { 1, 2, 3, 4 };

        LongBinaryOperator sum = (d1, d2) -> d1 + d2;
        Arrays.parallelPrefix(array, sum);

        System.out.println(Arrays.toString(array));
    }
}

Output:

[1, 3, 6, 10]


Approach 2: NullPointerException

Java

import java.util.Arrays;
import java.util.function.LongBinaryOperator;

public class ArraysparallelPrefixlong {
    public static void main(String[] args) {

        long array[] = null;

        LongBinaryOperator sum = (d1, d2) -> d1 + d2;
        Arrays.parallelPrefix(array, sum);

        System.out.println(Arrays.toString(array));
    }
}

Output:

Exception in thread "main" java.lang.NullPointerException: Cannot read the array length because "array" is null at java.base/java.util.Arrays.parallelPrefix(Arrays.java:1433)


Arrays.parallelPrefix(int[], int, int, IntBinaryOperator) in Java

Arrays.parallelPrefix(int[], int, int, IntBinaryOperator): This method is available in java.util.Arrays class of Java.

Syntax:

void java.util.Arrays.parallelPrefix(int[] array, int fromIndex, int toIndex, IntBinaryOperator op)

This method takes four arguments one of type int array and the other two of type int and the one of type IntBinaryOperator as its parameters. This method performs parallelPrefix(int [], IntBinaryOperator)for the given subrange of the array.

Parameters: Four parameters are required for this method.

array: the array.

fromIndex: the index of the first element, inclusive.

toIndex: the index of the last element, exclusive.

op: a side-effect-free, associative function to perform the accumulation.

Throws:

1. IllegalArgumentException - if fromIndex > toIndex.

2. ArrayIndexOutOfBoundsException - if fromIndex < 0 or toIndex > array.length.

3. NullPointerException - if the specified array or function is null.

Approach 1: When no exceptions

Java

import java.util.Arrays;
import java.util.function.IntBinaryOperator;

public class ArraysparallelPrefixintrange {
    public static void main(String[] args) {

        int array[] = { 1, 2, 3, 4, 10, 23, 45, 56, 78 };

        int fromIndex = 2, toIndex = 4;
        IntBinaryOperator sum = (d1, d2) -> d1 + d2;
        Arrays.parallelPrefix(array, fromIndex, toIndex, sum);

        System.out.println(Arrays.toString(array));
    }
}

Output:

[1, 2, 3, 7, 10, 23, 45, 56, 78]


Approach 2: IllegalArgumentException 

Java

import java.util.Arrays;
import java.util.function.IntBinaryOperator;

public class ArraysparallelPrefixintrange {
    public static void main(String[] args) {

        int array[] = { 1, 2, 3, 4, 10, 23, 45, 56, 78 };

        int fromIndex = 5, toIndex = 4;
        IntBinaryOperator sum = (d1, d2) -> d1 + d2;
        Arrays.parallelPrefix(array, fromIndex, toIndex, sum);

        System.out.println(Arrays.toString(array));
    }
}

Output:

Exception in thread "main" java.lang.IllegalArgumentException: fromIndex(5) > toIndex(4) at java.base/java.util.Arrays.rangeCheck(Arrays.java:718) at java.base/java.util.Arrays.parallelPrefix(Arrays.java:1549)



Approach 3: ArrayIndexOutOfBoundsException 

Java

import java.util.Arrays;
import java.util.function.IntBinaryOperator;

public class ArraysparallelPrefixintrange {
    public static void main(String[] args) {

        int array[] = { 1, 2, 3, 4, 10, 23, 45, 56, 78 };

        int fromIndex = -1, toIndex = 4;
        IntBinaryOperator sum = (d1, d2) -> d1 + d2;
        Arrays.parallelPrefix(array, fromIndex, toIndex, sum);

        System.out.println(Arrays.toString(array));
    }
}

Output:

Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Array index out of range: -1 at java.base/java.util.Arrays.rangeCheck(Arrays.java:722) at java.base/java.util.Arrays.parallelPrefix(Arrays.java:1549)



Approach 4: NullPointerException 

Java

import java.util.Arrays;
import java.util.function.IntBinaryOperator;

public class ArraysparallelPrefixintrange {
    public static void main(String[] args) {

        int array[] = null;

        int fromIndex = 2, toIndex = 4;
        IntBinaryOperator sum = (d1, d2) -> d1 + d2;
        Arrays.parallelPrefix(array, fromIndex, toIndex, sum);

        System.out.println(Arrays.toString(array));
    }
}

Output:

Exception in thread "main" java.lang.NullPointerException: Cannot read the array length because "array" is null at java.base/java.util.Arrays.parallelPrefix(Arrays.java:1549)



Arrays.parallelPrefix(int[], IntBinaryOperator) in Java

Arrays.parallelPrefix(int[], IntBinaryOperator): This method is available in java.util.Arrays class of Java.

Syntax:

void java.util.Arrays.parallelPrefix(int[] array, IntBinaryOperator op)

This method takes two arguments one of tye int array and the other of type IntBinaryOperator as its parameters. This method cumulates, in parallel, each element of the given array in place, using the supplied function.

Parameters: Two parameters are required for this method.

array: the array, which is modified in-place by this method.

op: a side-effect-free, associative function to perform the accumulation.

Throws:

NullPointerException - if the specified array or function is null.

Approach 1: When no exceptions

Java

import java.util.Arrays;
import java.util.function.IntBinaryOperator;

public class ArraysparallelPrefixint {
    public static void main(String[] args) {

        int array[] = { 1, 2, 3, 4 };

        IntBinaryOperator sum = (d1, d2) -> d1 + d2;
        Arrays.parallelPrefix(array, sum);

        System.out.println(Arrays.toString(array));
    }
}

Output:

[1, 3, 6, 10]


Approach 2: NullPointerException 

Java

import java.util.Arrays;
import java.util.function.IntBinaryOperator;

public class ArraysparallelPrefixint {
    public static void main(String[] args) {

        int array[] = null;

        IntBinaryOperator sum = (d1, d2) -> d1 + d2;
        Arrays.parallelPrefix(array, sum);

        System.out.println(Arrays.toString(array));
    }
}

Output:

Exception in thread "main" java.lang.NullPointerException: Cannot read the array length because "array" is null at java.base/java.util.Arrays.parallelPrefix(Arrays.java:1526)