Author: Vijay SRJ

  • How to prevent basic auth pop up for invalid credentials in Spring Boot REST API?

    Let’s say you have created a backend REST API application in Spring Boot. Your front end application is deployed separately and it communicates with the backend via REST API calls. You decide to protect the backend with Spring Security. You also decide to use inbuilt Spring mechanism to validate login. So to authenticate a user…

  • How to restrict users from uploading huge files in Spring Boot?

    Let’s say you have created a REST API which accepts files as input. You deployed the API and users started uploading huge files and your server’s memory gets fast filled up. How do you prevent this? You can do this with zero code changes in Spring Boot. All you have to do is add a…

  • How to create a custom Health Check in Spring Boot for Microservices?

    Let’s say you have three microservices in your application. One of the microservices connects to the other two microservices. And in addition it also connects to a database. You want to check the health status of this microservice and its dependent microservices and database. How to do this in Spring Boot? Spring Boot provides an…

  • Attribute vs Property in HTML

    The terms attribute and property can be confusing in HTML. For example the javascript framework Angular has the concepts Property Binding and Attribute Binding . Without knowing the difference between the two it is difficult to grasp the difference between those concepts. The major difference between the two is this: Attribute is related to HTML…

  • How to copy only updatable values while updating resources through Spring Boot REST API?

    Let’s say you are creating an application to manage your daily tasks. You decide to write REST APIs in Spring Boot. Following REST API conventions , You create an API named /tasks to create a new task using Http POST method. You store the task in database. You create an API with the same name…

  • What is “effectively final” in Java?

    Let’s say you don’t want a variable value to be changed once it is initialized. How to achieve this in Java? By declaring the variable as final. Now the variable becomes a constant and the compiler will complain if it is modified anywhere. There is one more concept of “final” in Java , which is…

  • How to deal with optional request parameters in Spring Boot?

    Let’s say you are creating a REST API in Spring Boot which accepts request parameters. You use the annotation @RequestParam to retrieve the request values. And if user does not supply the request parameter Spring will throw error. What if the request parameter is just an optional parameter? How to handle this in Spring ?…

  • How to download a Microsoft Word document from a Spring Boot REST API?

    Let’s say you want to create a REST API which returns a Microsoft Word Document. When a client calls that API the document gets automatically downloaded to their system. How do you do it? Here is the algorithm: STEP1: Create a REST API which returns a byte array STEP2: Configure the return type of the…

  • Are we using mutable objects too much?

    By default any object you create in java is mutable. And so we end up using mutable objects more often. But Java team themselves suggest to use Immutable objects more. From https://docs.oracle.com/javase/tutorial/essential/concurrency/immutable.html : Maximum reliance on immutable objects is widely accepted as a sound strategy for creating simple, reliable code One reason why developers wouldn’t…

  • How to load AWS Secrets automatically on application startup in Spring Boot?

    You can load AWS secrets from AWS Secrets Manager without writing any code in Spring Boot! And this happens automatically during application start up. This can be done by adding a single property in your application.yml file: spring.config.import Using bootstrap.yml file to do this has been deprecated and it is advisable to use this option…