Mapping Properties To POJO

Learn the concept of configuration properties in spring boot and how to validate the properties using the JSR 380 bean validation framework.

Introduction

Every application above play size requires some parameters at startup. These parameters may, for example, define which database to connect to, which locale to support, or which logging level to apply.

Configuration allows the same code to be run with different configurations in a different environment. In the same way, our application has a database property and a property related to forbes400.

Property injection

For any property defined in application.properties to be useful, it must be available in Java code. Spring boot provides two ways to do this.

1. Injecting using @Value

From our previous example, we have a couple of properties defined for forbes400 API

forbes400.billionaire.api=https://forbes400.herokuapp.com/api/forbes400
forbes400.billionaire.maxrecord=50

We can bring the corresponding configuration in Java classes using the below code.

@Value("${forbes400.billionaire.api}")
private String
...