Spring Boot API that extracts birthdate and age from codice fiscale.
Table of contents
- 1. pom.xml (Maven Dependencies)
- 2. CodiceFiscaleApplication.java (Main Class)
- 3. CodiceFiscaleController.java (Controller)
- 4. CodiceFiscaleService.java (Service Layer)
- 5. CodiceFiscaleResponse.java (DTO)
- 6. application.properties (Configuration)
- 7. Swagger Access
- 8. Example Request & Response
- 9. Running the Project
Here is the complete Spring Boot project to expose a REST API that, given a tax ID code, returns the date of birth and age.
The API is also accessible via Swagger.
1. pom.xml
(Maven Dependencies)
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.example</groupId>
<artifactId>codicefiscale-api</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging>
<name>Codice Fiscale API</name>
<description>Spring Boot API for retrieving date of birth and age from Codice Fiscale</description>
<properties>
<java.version>17</java.version>
<spring.boot.version>3.1.3</spring.boot.version>
</properties>
<dependencies>
<!-- Spring Boot Starter Web -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<!-- Spring Boot Starter Actuator -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-actuator</artifactId>
</dependency>
<!-- Swagger (Springdoc OpenAPI) -->
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
<version>2.1.0</version>
</dependency>
<!-- Lombok -->
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<scope>provided</scope>
</dependency>
<!-- Spring Boot Test -->
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
2. CodiceFiscaleApplication.java
(Main Class)
package com.example.codicefiscale;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class CodiceFiscaleApplication {
public static void main(String[] args) {
SpringApplication.run(CodiceFiscaleApplication.class, args);
}
}
3. CodiceFiscaleController.java
(Controller)
package com.example.codicefiscale.controller;
import com.example.codicefiscale.service.CodiceFiscaleService;
import com.example.codicefiscale.dto.CodiceFiscaleResponse;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.web.bind.annotation.*;
@RestController
@RequestMapping("/api/v1/codice-fiscale")
public class CodiceFiscaleController {
private final CodiceFiscaleService codiceFiscaleService;
public CodiceFiscaleController(CodiceFiscaleService codiceFiscaleService) {
this.codiceFiscaleService = codiceFiscaleService;
}
@Operation(summary = "Retrieve birthdate and age from codice fiscale")
@GetMapping("/{codiceFiscale}")
public CodiceFiscaleResponse getDetails(@PathVariable String codiceFiscale) {
return codiceFiscaleService.getBirthdateAndAge(codiceFiscale);
}
}
4. CodiceFiscaleService.java
(Service Layer)
package com.example.codicefiscale.service;
import com.example.codicefiscale.dto.CodiceFiscaleResponse;
import org.springframework.stereotype.Service;
import java.time.LocalDate;
import java.time.Period;
import java.util.regex.Pattern;
@Service
public class CodiceFiscaleService {
public CodiceFiscaleResponse getBirthdateAndAge(String codiceFiscale) {
if (!isValidCodiceFiscale(codiceFiscale)) {
throw new IllegalArgumentException("Invalid Codice Fiscale format");
}
LocalDate birthdate = extractBirthDate(codiceFiscale);
int age = Period.between(birthdate, LocalDate.now()).getYears();
return new CodiceFiscaleResponse(codiceFiscale, birthdate, age);
}
private boolean isValidCodiceFiscale(String codiceFiscale) {
return Pattern.matches("^[A-Z]{6}\\d{2}[A-Z]\\d{2}[A-Z]\\d{3}[A-Z]$", codiceFiscale);
}
private LocalDate extractBirthDate(String codiceFiscale) {
int year = Integer.parseInt(codiceFiscale.substring(6, 8));
year += (year < 40) ? 2000 : 1900;
int month = "ABCDEHLMPRST".indexOf(codiceFiscale.charAt(8)) + 1;
int day = Integer.parseInt(codiceFiscale.substring(9, 11));
if (day > 31) {
day -= 40;
}
return LocalDate.of(year, month, day);
}
}
5. CodiceFiscaleResponse.java
(DTO)
package com.example.codicefiscale.dto;
import java.time.LocalDate;
public record CodiceFiscaleResponse(String codiceFiscale, LocalDate birthdate, int age) {}
6. application.properties
(Configuration)
server.port=8080
springdoc.swagger-ui.path=/swagger-ui.html
7. Swagger Access
Once the application is running, open:
http://localhost:8080/swagger-ui.html
You'll see an interactive API where you can input a codice fiscale
and get the birth date and age.
8. Example Request & Response
Request:
GET /api/v1/codice-fiscale/RSSMRA85M01H501Z
Response:
{
"codiceFiscale": "RSSMRA85M01H501Z",
"birthdate": "1985-08-01",
"age": 38
}
9. Running the Project
Run the following command in the project root:
mvn spring-boot:run
Now you have a complete Spring Boot API that extracts birthdate and age from codice fiscale.