spring-boot-hateoas-for-restful-services

Spring Boot – HATEOAS for RESTful Services

In this article, I will explain implementing HATEOAS for a REST API with Spring Boot with an example.

What is HATEOAS?

HATEOAS stands for โ€œHypermedia as the engine of application stateโ€

It’s a complicated acronym. Let me decode it for you.

What do you visualize when you visit a web page?

The data that you would want to see. Is that all? You would also see links and buttons to see related data.

For example, if you go to a product page, you will see

  • product details
  • Links to Edit and Delete product details
  • Links to see relevant products in that category

HATEOAS brings the exact same concepts to RESTful Web Services.

When some details of a resource are requested, you will provide the resource details as well as details of related resources and the possible actions you can perform on the resource. For example, when requesting information about a Facebook user, a REST service can return the following

  • User details
  • Links to get his recent comments
  • Links to retrieve his friend list
  • Links to get his recent posts

Sigh, a lot of things to read right? Let’s, get our hands dirty =D

Create the project

Create a new project using start.spring.io

Add basic CRUD Operation

I’m not gonna explain CRUD operation in detail, you can refer to my project uploaded on Github.

Enhancing the resource

To implement HATEOAS, we would need to include related resources in the response. Instead of Product, I’ll use a return type of EntityModel<Product>. EntityModel is a simple class wrapping a domain object and allows adding links to it.

@GetMapping("/{id}")
public EntityModel<Product> getProduct(@PathVariable long id) {
    Optional<Product> product = productRepository.getProductById(id);

    if (!product.isPresent())
        throw new ProductNotFoundException("id-" + id);
    EntityModel<Product> resource = EntityModel.of(product.get());
    WebMvcLinkBuilder linkTo = linkTo(methodOn(this.getClass()).getAllProducts());
    resource.add(linkTo.withRel("all-products"));
    return resource;
}

So what’s happening here actually?

EntityModel<Product> resource = EntityModel.of(product.get());

In the above line, we are creating a new resource. Then, adding the link to retrieve all products method to the link.

WebMvcLinkBuilder linkTo = linkTo(methodOn(this.getClass()).getAllProducts());
resource.add(linkTo.withRel("all-products"));

In the above line, we are creating a new resource. After making the changes so far, let’s start the service and see the change in action.

{
    “id”: 1,
    “name”: “Dell Precision 5550”,
    “currentStock”: 99,
    “description”: “The world’s smallest and thinnest 15 inch mobile workstation.”,
    “type”: “Laptop”,
    “dateCreated”: “2022-10-23T08:33:10.658+00:00”,
    “lastModified”: “2022-10-23T08:33:10.658+00:00”,
    “_links”: {
        “all-products”: {
            “href”: “http://localhost:3001/api/v1/product”
        }
    }
}

Don’t worry, I’ve added a bootstrap class to add a few products into the DB whenever the application starts.

The above example covers important concepts in enhancing resources with HATEOAS.

However, ain’t gonna make this post longer. You have to make the important decisions:

  • What are the important resources related to a specific resource?

Think of this as homework, go ahead and enhance the application with more HATEOAS links.

Oh! I forgot to give you the Github link, Here you go: https://github.com/nazrul-kabir/spring-boot-rest-service-with-hateoas Feel free to comment on the post if you have any comments.

And, don’t forget to read my other posts on Spring Boot

Convert currentTimeMillis to a datetime in JAVA(Spring Boot)

Spring Boot

In this tutorial I’ll show how to save Time Stamp in Database in currentTimeMillis and retrieve in Spring Boot

Want precise time for some purpose? Normally, we can save Time Stamp in Database but what if the data need to be converted into several formats?

Let’s say we need to save “2019-6-27 00:59:59” and then convert it to several formats like ’27/2019/6 00:59:59′ and ’20-JUN-1990 08:03:00′. Though “2019-6-27 00:59:59” can be converted but that’s a little bit tough. The ideal solution is to save the data in millisecond format. In millisecond format “2019-6-27 00:59:59” would be “1564167599000”

I assume you already have the datetime picker data in controller, here’s the conversion code:

String expireTime = model.attribute; //your model attribute or request parameter name
Date date = formatter.parse(expireTime);
Timestamp timestamp = new Timestamp(date.getTime());
String expireTime2 = String.valueOf(timestamp.getTime());
//That's it! We've converted '2019-6-27 00:59:59' to '1564167599000'
//Add your save logic

Now the reading part. User can’t read ‘1564167599000’, right? Let’s convert this to readable format:

String convertedExpireTime = "1564167599000";
//Create a calendar instance
Calendar calendar = Calendar.getInstance();
calendar.setTimeInMillis(Long.parseLong(convertedExpireTime));
int mYear = calendar.get(Calendar.YEAR);
int mMonth = calendar.get(Calendar.MONTH);
int mDay = calendar.get(Calendar.DAY_OF_MONTH);
int mHour = calendar.get(Calendar.HOUR_OF_DAY);
int mMin = calendar.get(Calendar.MINUTE);
int mSec = calendar.get(Calendar.SECOND);
String readableFormat = mYear + "-" + mMonth + "-" + mDay + " " + mHour + ":"+ mMin + ":"+ mSec; // The value is now converted to 2019-6-27 0:59:59
//Add some logic

That’s all! We’re done!

If you’ve any confusion please let me know ๐Ÿ™‚

How to query data out of the box using Spring data JPA by both Sort and Pageable

Spring Boot

Developing REST API? Clients wants both sort & pagination?

That’s quite simple btw!

Since last few hours was trying to add this simple feature my Spring Boot project. In my project pagination was done using pageable

In this tutorial I’m gonna show how to achieve this.

Let’s copy the code from below:

public List<Model> getAllData(Pageable pageable){
       List<Model> models= new ArrayList<>();
       modelRepository.findAllByOrderByIdDesc(pageable).forEach(models::add);
       return models;
   }
//Model = Your Model where you want to implement this feature.ย 

That’s all! We’re done!
If you’ve any confusion please let me know ๐Ÿ™‚

Show data from two JPA entity tables on same page using Spring Boot and Thymeleaf

spring boot thymeleaf bootstrap 4

Let’s say you want to display multiple table contents in Spring Boot project using Thymeleaf, JPA Entity and Model.

In my project I have mapped a relationship between the two entities I need to retrieve data from and am wondering how to display data from the two tables on list page. I’ve ‘category’ table and ‘product’ table like below:

Spring Boot, Thymeleaf Show data from two JPA entity tables on same page using Spring Boot and Thymeleaf

And in my controller I’ve list of both category and product in respective model. Here is the snippet:

model.addAttribute("products", productService.findAll());
model.addAttribute("categories", categoryService.findAll());

So, I’ve all the required data in the model and I can access them from Thymeleaf. Let’s copy the table code from below (N.B: I’ve used bootstrap table):

<table class="table table-striped table-bordered table-responsive" role="grid">
   <thead>
      <th>Category</th>
      <th>Product</th>
   </thead>
   <tbody>
      <tr role="row" class="odd" th:each="product:${products}">
         <td>
            <p th:each="category : ${categories}" th:if="(${category.id} == ${product.id})" th:text="${category.name}"></p>
         </td>
         <td th:text="${product.name}">
      </tr>
   </tbody>
</table>

It’ll produce something like this:

Spring Boot, Thymeleaf: Display data from two JPA entity tables on same page

That’s all! We’re done!
If you’ve any confusion please let me know ๐Ÿ™‚

Thymeleaf select required not working?

thymeleaf

Since last few days trying to add select “required” attribute in my Thymeleaf project.  Neither of these works in Thymeleaf:

  • <select required=”required”>
  • <select required=’required’>
  • <select required=required>
  • <select required=””>
  • <select required=”>
  • <select required>

In this tutorial I’m gonna show how to achieve this.

Let’s copy the code from below:

<select th:name="someName" class="form-control" id="someID" th:required="true">
    <option value="">Select Category</option>
    <option th:each="model: ${someModel}" th:value="${model.id}"
            th:text="${model.Title}"></option>
</select>

Please note that, first/default option value must be set to “” if you put “0” it won’t work!

That’s all! We’re done!
If you’ve any confusion please let me know ๐Ÿ™‚

Bootstrap Dynamic Modal with Spring Boot and Thymeleaf

spring boot thymeleaf bootstrap 4

Let’s say you want to dynamically create modal popup in your Spring Boot project using Twitter Bootstrap 4 to show the user a ‘warning popup’ before deleting a data or edit the contents on the fly in same page. Not to mention I’ve used Thymeleaf as my template engine.

In this tutorial I’m gonna show how to achieve this.

I assume you have already included required Bootstrap resources and necessary jQuery library in your project and are ready to implement modal popup. Let’s copy the modal code from below:

<div class="modal modal-warning fade in" id="myModal">
 <div class="modal-dialog">
 <div class="modal-content">
 <div class="modal-header">
 <button type="button" class="close" data-dismiss="modal" aria-label="Close">
 <span aria-hidden="true">ร—</span></button>
 <h5 class="modal-title">Delete User</h5>
 </div>
 <div class="modal-body">
 <h3>Are you sure want to delete this user?</h3>
 </div>
 <div class="modal-footer">
 <button type="button" class="btn btn-outline pull-left" data-dismiss="modal">Close</button>
 <a type="button" class="btn btn-danger"><i class="fa fa-check"></i>&nbsp;Yes</a>
 </div>
 </div>
 </div>
 </div>

It’ll produce something like this:

Bootstrap Dynamic Modal with Spring Boot and Thymeleaf

Now, the next step actually the final step. Make the modal dynamically generate for all the data in table.

I’ve some data in the table like below now I want to delete a user but before that I want that modal to generate dynamically for all data.

Bootstrap Table in Spring boot

On the ‘Red’ delete button the popup will generate.

<a data-toggle="modal" data-target="#modal-warning" th:attr="data-target='#modal-warning'+${user.id }"><span class="glyphicon glyphicon-trash"></span></a>

Here “${user.id}” is my thymeleaf object.

Now, let’s make the modal dynamic with the id:

<div class="modal modal-warning fade in" th:id="modal-warning+${user.id }" >
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button type="button" class="close" data-dismiss="modal" aria-label="Close">
                    <span aria-hidden="true">ร—</span></button>
                <h5 class="modal-title">Delete User</h5>
            </div>
            <div class="modal-body">
                <h3>Are you sure want to delete this user?</h3>
            </div>
            <div class="modal-footer">
                <button type="button" class="btn btn-outline pull-left" data-dismiss="modal">Close</button>
                <a type="button" class="btn btn-outline" th:href="@{/user/delete/{id}(id=${user.id})}"><i class="fa fa-check"></i>&nbsp;Yes</a>
            </div>
        </div>
    </div>
</div>

Here “th:href=”@{/user/delete/{id}(id=${user.id})}” is your POST URL.
You can also edit any data inside the modal by adding a form.

That’s all! We’re done!
If you’ve any confusion please let me know ๐Ÿ™‚