Month: May 2020

  • How to consume a REST service which accepts form data in Spring?

    Let’s say you want to consume a REST service from your Spring Boot / Spring MVC application. The developer who created the REST service you wanted to consume, developed it in such a way that it accepts only form data. May be he created it to accept data from HTML forms. And now you want…

  • How to make sure resources are auto-closed in Java?

    Prior to Java 7 , if we wanted to close a resource like a FileOutputStream we had to do it inside a finally block. And the code looked something like this : We had to check if the resources is null in the finally block and again add a try-catch block as closing the resource…

  • How to create a custom JRE using Java Modules?

    Starting Java 9 , Java introduced Modules. It allowed us to organize our code into modules. Also Java organised its own internal libraries into modules. Here is the list of modules in Java by default(you can get this by running the command : java –list-modules from command prompt starting Java 9): G:\softwares\java14\jdk-14\bin>java –list-modules java.base@14 java.compiler@14…

  • How to map Enum Types to JSON requests in Spring Boot automatically?

    Let’s say you have created a REST service which accepts a JSON request. And one of the attributes in the request can only have a range of constant values. For example , the departments an employee can belong to. And the best way to represent this , is to use an Enum. But will a…

  • How to use Method References in Java?

    As explained in previous posts , lamda expressions allow us to pass a functionality as a parameter. We can dynamically choose function implementations and pass it to another method as an argument . What if the functionality we want to pass already exists and we want to pass that ? Use Method References! Let’s consider…

  • How to use Lamda expressions? – Part 2 – Applying Lamda Expressions

    Let’s apply lamda expressions to a specific use case and see how it helps in building better code. Consider this use case: A manager wants to give promotion to an employee. He follows the below process for this : Check if the employee is eligible Evaluate the employee through interview Grant promotion based on the…

  • How to use Lamda expressions – Part 1- What are lamda expressions?

    Lamda expressions were introducted in Java 8. They were out of the box , Java programmers weren’t used to anything like this before. So getting adapted to it and start using it need some practise. Once you understand lamda expressions you can super charge your programming skills and become a Super Hero. Ok I am…

  • How to handle pagination and sorting in Spring Data?

    Let’s say you want to handle pagination and sorting for your application and you don’t want to do the hard work of writing SQL queries or setting criteria using an ORM tool. Spring Data provides a very simple and efficient way to handle pagination and sorting. All you have to do is create a repository…

  • How to generate REST services automatically for data repositories using Spring Boot?

    There is no need to write REST service controller classes yourself if you just want to expose your databases as REST services. Spring Boot can automatically convert your database repositories into REST services. In addition to it, it returns the response in HAL format which allows you to navigate to the individual records. Let’s see…

  • How to modify a REST service response without modifying existing code in Spring Boot?

    I wanted to modify a REST service with a change in the response. The service was already doing what it was intended to do . There was a requirement to change certain things related to the representation of the response. I didn’t want to change the existing code and clutter it with this logic. So…