Java App. Extract Info From Tax Id .
Table of contents
- PART 1. App code and explanation
- 1. pom.xml
- 2. CodiceFiscaleApplication.java
- 3. CodiceFiscaleController.java
- 4. CodiceFiscaleService.java
- 5. CodiceFiscaleResponse.java
- 6. application.properties
- Explanation
- PART 2 . Explain libraries.
- 2.1. List of Packages and Imports Used in the Project
- Packages:
- Imports:
- 2.2. Explanation of Each Package and Import
- 1. package com.example.codicefiscale;
- 2. import org.springframework.boot.SpringApplication;
- 3. import org.springframework.boot.autoconfigure.SpringBootApplication;
- 4. import org.springframework.web.bind.annotation.*;
- 5. import org.springframework.stereotype.Service;
- 6. import java.time.LocalDate;
- 7. import java.time.Period;
- 8. import java.util.regex.Pattern;
- 2.3 Final Summary
PART 1. App code and explanation
1. pom.xml
<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://www.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>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
<version>2.1.0</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
</project>
Explanation
1. Project Definition
<groupId>com.example</groupId>
<artifactId>codicefiscale-api</artifactId>
<version>1.0.0</version>
<packaging>jar</packaging>
Defines the group ID, artifact ID, and version for the Maven project.
Specifies that this project will be packaged as a JAR file.
2. Java Version & Spring Boot Version
<properties>
<java.version>17</java.version>
<spring.boot.version>3.1.3</spring.boot.version>
</properties>
Ensures that Java 17 is used for compilation.
Specifies Spring Boot 3.1.3 as the version for dependencies.
3. Dependencies
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
- Adds
spring-boot-starter-web
, which allows the project to create REST APIs.
<dependency>
<groupId>org.springdoc</groupId>
<artifactId>springdoc-openapi-starter-webmvc-ui</artifactId>
<version>2.1.0</version>
</dependency>
- Integrates Swagger UI for API documentation.
4. Build Plugin
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
- Configures the Maven build to support Spring Boot execution.
2. CodiceFiscaleApplication.java
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);
}
}
Explanation
1. Package Declaration
package com.example.codicefiscale;
- Defines the package for this application.
2. Import Statements
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
SpringApplication
: Runs the Spring Boot application.@SpringBootApplication
: Marks this as a Spring Boot app.
3. Main Class
@SpringBootApplication
public class CodiceFiscaleApplication {
- Annotates the class as a Spring Boot application.
4. Main Method
public static void main(String[] args) {
SpringApplication.run(CodiceFiscaleApplication.class, args);
}
- Bootstraps the Spring Boot application.
3. CodiceFiscaleController.java
package com.example.codicefiscale.controller;
import com.example.codicefiscale.service.CodiceFiscaleService;
import com.example.codicefiscale.dto.CodiceFiscaleResponse;
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;
}
@GetMapping("/{codiceFiscale}")
public CodiceFiscaleResponse getDetails(@PathVariable String codiceFiscale) {
return codiceFiscaleService.getBirthdateAndAge(codiceFiscale);
}
}
Explanation
1. Controller Annotation
@RestController
@RequestMapping("/api/v1/codice-fiscale")
Marks this as a Spring REST Controller.
Defines the base URL for API calls.
2. Dependency Injection
private final CodiceFiscaleService codiceFiscaleService;
- Injects the service layer.
3. Constructor Injection
public CodiceFiscaleController(CodiceFiscaleService codiceFiscaleService) {
this.codiceFiscaleService = codiceFiscaleService;
}
- Initializes the service in the constructor.
4. API Endpoint
@GetMapping("/{codiceFiscale}")
public CodiceFiscaleResponse getDetails(@PathVariable String codiceFiscale) {
return codiceFiscaleService.getBirthdateAndAge(codiceFiscale);
}
- Exposes a GET API that takes a
codice fiscale
and returns birthdate and age.
4. CodiceFiscaleService.java
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 {
Explanation
1. Service Annotation
@Service
- Marks this class as a Spring Boot Service.
2. Validate Format
if (!isValidCodiceFiscale(codiceFiscale)) {
throw new IllegalArgumentException("Invalid Codice Fiscale format");
}
- Checks the validity of the codice fiscale using regex.
3. Extract Birthdate
LocalDate birthdate = extractBirthDate(codiceFiscale);
- Calls a method to extract the date of birth.
5. CodiceFiscaleResponse.java
package com.example.codicefiscale.dto;
import java.time.LocalDate;
public record CodiceFiscaleResponse(String codiceFiscale, LocalDate birthdate, int age) {}
Explanation
1. Record Declaration
public record CodiceFiscaleResponse(String codiceFiscale, LocalDate birthdate, int age) {}
- Defines an immutable response object.
6. application.properties
server.port=8080
springdoc.swagger-ui.path=/swagger-ui.html
Explanation
1. Server Port
server.port=8080
- Runs the app on port 8080.
2. Swagger UI Path
springdoc.swagger-ui.path=/swagger-ui.html
- Enables Swagger UI.
PART 2 . Explain libraries.
2.1. List of Packages and Imports Used in the Project
Packages:
package com.example.codicefiscale;
package com.example.codicefiscale.controller;
package com.example.codicefiscale.service;
package com.example.codicefiscale.dto;
Imports:
Spring Boot Core
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
Spring Boot Web (REST API)
import org.springframework.web.bind.annotation.*;
Spring Boot Service & Dependency Injection
import org.springframework.stereotype.Service;
Data Transfer Object (DTO)
import java.time.LocalDate;
Date & Time API
import java.time.Period;
Regex for Validation
import java.util.regex.Pattern;
2.2. Explanation of Each Package and Import
1. package com.example.codicefiscale;
Subpoint 1:
This declares the base package for the application. It ensures that all Java classes inside this directory structure belong to com.example.codicefiscale
, which is important for organizing the project logically and avoiding name conflicts with other packages.
Subpoint 2:
Java package naming follows a convention that starts with a reverse domain name (com.example
). This prevents naming collisions when integrating external libraries. The structure allows modularization, making it easier to manage dependencies and configurations.
2. import org.springframework.boot.SpringApplication;
Subpoint 1:
SpringApplication.run
()
is the entry point for a Spring Boot application. It starts the embedded web server, loads Spring Beans, and initializes configurations automatically without requiring a separate XML-based setup like older Spring versions.
Subpoint 2:
This class provides several static methods to configure the application before startup, such as setting profiles, enabling lazy initialization, and adding listeners. It makes deployment flexible, allowing modifications without needing to change the core application logic.
3. import org.springframework.boot.autoconfigure.SpringBootApplication;
Subpoint 1:
@SpringBootApplication
is a combination of three annotations: @Configuration
, @EnableAutoConfiguration
, and @ComponentScan
. It tells Spring Boot to automatically configure required dependencies, scan components, and register beans.
Subpoint 2:
This annotation simplifies the setup by reducing boilerplate code. It enables auto-detection of controllers, services, and repositories inside the base package, making manual configurations unnecessary and ensuring that all necessary beans are injected correctly.
4. import org.springframework.web.bind.annotation.*;
Subpoint 1:
This imports various annotations like @RestController
, @RequestMapping
, @GetMapping
, and @PathVariable
, which are essential for defining REST APIs. These annotations help handle incoming HTTP requests and map them to Java methods.
Subpoint 2:
Using @RestController
, the class is automatically converted into a JSON response without needing @ResponseBody
. The @GetMapping("/{id}")
annotation maps a URL path to a specific method, making it easy to define RESTful API endpoints.
5. import org.springframework.stereotype.Service;
Subpoint 1:
The @Service
annotation marks a class as a business logic layer component in the Spring Framework. It allows Spring to detect this class as a bean and manage its lifecycle, enabling automatic dependency injection.
Subpoint 2:
By marking a class with @Service
, it becomes part of the Spring container and can be injected into other components using @Autowired
or constructor injection. This promotes modularity, separation of concerns, and better testability.
6. import java.time.LocalDate;
Subpoint 1:
LocalDate
represents date-only values without time or timezone. It is useful for storing and manipulating dates such as birthdates or anniversaries while ensuring immutability, which helps prevent accidental modifications.
Subpoint 2:
Unlike java.util.Date
, LocalDate
follows the ISO-8601 standard and provides methods like now()
, of()
, and plusDays()
, making it easy to perform operations like adding/subtracting dates without dealing with time complexities.
7. import java.time.Period;
Subpoint 1:
Period
is used to calculate the difference between two dates in years, months, and days. This makes it ideal for determining a person’s age when given a birthdate, which is required in our API.
Subpoint 2:
Unlike Duration
, which deals with time-based values (hours, minutes, seconds), Period
strictly works with date-based calculations. The method Period.between(startDate, endDate).getYears()
efficiently computes a person's exact age.
8. import java.util.regex.Pattern;
Subpoint 1:
The Pattern
class from java.util.regex
is used to validate if a codice fiscale (Italian tax code) follows a specific format. This ensures that incorrect values do not proceed into the system, reducing data integrity issues.
Subpoint 2:
Pattern.matches("^[A-Z]{6}\\d{2}[A-Z]\\d{2}[A-Z]\\d{3}[A-Z]$", codiceFiscale);
checks that the given string follows the structure of 6 letters + 2 digits + 1 letter + 2 digits + 1 letter + 3 digits + 1 letter.
2.3 Final Summary
Import/Package | Purpose | Key Functionality |
package com.example.codicefiscale; | Defines the project structure | Helps organize different layers (Controller, Service, DTO) |
import org.springframework.boot.SpringApplication; | Starts the Spring Boot app | Initializes configurations, embedded Tomcat |
import org.springframework.boot.autoconfigure.SpringBootApplication; | Enables auto-configuration | Reduces boilerplate, enables component scanning |
import org.springframework.web.bind.annotation.*; | Defines REST API endpoints | Handles HTTP requests (@GetMapping , @PathVariable ) |
import org.springframework.stereotype.Service; | Marks a class as a service | Enables dependency injection and business logic separation |
import java.time.LocalDate; | Handles date-only values | Useful for birthdates, date manipulations |
import java.time.Period; | Calculates age differences | Computes years, months, days between two dates |
import java.util.regex.Pattern; | Validates codice fiscale format | Uses regex to check input structure |