Spring Boot MVC download image or binary file

Sometime we want our user to download some file from our web application that is developed in Spring MVC or Spring Boot. To achieve this the main trick is to send the Content-Disposition http header before sending the data. Here is an example of a Controller class with just one method that take the name of a jpg image file from url, read that file from a predefined location, set the appropriate http header and send the data to the client.

Spring Boot MVC Controller send image or binary data

In web server application, sending image data to client browser is quite common job. This can be easily achieved in Spring or Spring boot MVC Controller. Key things here is to specify proper Content-Type such as image/png or whatever and then return the raw binary data of the image. Here is a sample code to demonstrate the same. @Controller public class ImageController { @RequestMapping("/images/{id}") public ResponseEntity<byte[]> getImage(@PathVariable("id") String id) throws IOException { String filename = "D:/images/" + id + ".

Spring Boot + Spring JPA with PostgreSQL or MySQL or Oracle or SQL Server database and Thymeleaf using Gradle

Here we will see how to build a sample full stack web application using Spring Boot, Spring JPA with database like PostgreSQL, MySQL or Oracle Thymeleaf and Gradle. This example is very similar with my other example regarding Spring boot with H2 database: Spring Boot H2 data source with Thymeleaf using Gradle The only exception here, we will use some production database like postgres as database. So there are changes around Spring JPA configuration and Gradle build file.

Spring Boot RESTful web service JSON example

Spring Boot comes with all the needed components for creating RESTful web services. Here we will see how easily we can build a simple Spring Boot application which will provide web services for CRUD operations i.e. Create, Retrieve, Update and Delete operations using HTTP POST, GET, PUT and DELETE method respectively. To keep the application simple, we will not use any database. Instead just use a HashMap to store the data objects through a Service layer class.

Spring Boot JSON format date using JsonSerialize and JsonFormat

While providing JSON data through Spring Boot or Spring web services, we might have situation where the JSON data contains Date data. By default, the dates are not formatted in human readable format. However, there are ways to format the dates as we want. Here we will see use of two annotations: JsonSerialize JsonFormat Let us see the source code how we can use these annotations. 1. build.