DEV Community

Carlos A. Martinez
Carlos A. Martinez

Posted on

Small service using Maven and Spring Boot

Maven in 5 Minutes

mvn archetype:generate -DgroupId=com.parkingservices.app -DartifactId=parking-services -DarchetypeArtifactId=maven-archetype-quickstart -DarchetypeVersion=1.4 -DinteractiveMode=false

Image description

Open with IntelliJ and delete src folder

Image description

We need change couple things on pom.xml

<?xml version="1.0" encoding="UTF-8"?>

<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.parkingservices.app</groupId>
  <artifactId>parking-services</artifactId>
  <packaging>pom</packaging>
  <version>1.0-SNAPSHOT</version>

  <modules>
    <module>vehicle</module>
  </modules>

  <name>parking-services</name>
  <url>https://www.example.com</url>

  <properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <maven.compiler.source>17</maven.compiler.source>
    <maven.compiler.target>17</maven.compiler.target>
    <spring.boot.maven.plugin.version>2.5.7</spring.boot.maven.plugin.version>
    <spring.boot.dependencies.version>2.5.7</spring.boot.dependencies.version>
  </properties>

  <dependencyManagement>
    <dependencies>
      <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-dependencies</artifactId>
        <version>${spring.boot.dependencies.version}</version>
        <scope>import</scope>
        <type>pom</type>
      </dependency>
    </dependencies>
  </dependencyManagement>

  <dependencies>
    <dependency>
      <groupId>org.projectlombok</groupId>
      <artifactId>lombok</artifactId>
      <version>1.18.22</version>
    </dependency>
    <dependency>
      <groupId>org.springframework.boot</groupId>
      <artifactId>spring-boot-starter-test</artifactId>
    </dependency>
  </dependencies>

  <build>
    <pluginManagement>
      <plugins>
        <plugin>
          <groupId>org.springframework.boot</groupId>
          <artifactId>spring-boot-maven-plugin</artifactId>
          <version>${spring.boot.maven.plugin.version}</version>
        </plugin>
      </plugins>
    </pluginManagement>
  </build>
</project>
Enter fullscreen mode Exit fullscreen mode

Now we need add new module

right click on project name > New > Module

Image description

Image description

Image description

Image description

Create banner spring boot
Online Spring Boot Banner Generator

Now run application

Image description

pom vehicle

<?xml version="1.0" encoding="UTF-8"?>
<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">
    <parent>
        <groupId>com.parkingservices.app</groupId>
        <artifactId>parking-services</artifactId>
        <version>1.0-SNAPSHOT</version>
    </parent>

    <modelVersion>4.0.0</modelVersion>

    <artifactId>vehicle</artifactId>

    <properties>
        <maven.compiler.source>17</maven.compiler.source>
        <maven.compiler.target>17</maven.compiler.target>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>mysql</groupId>
            <artifactId>mysql-connector-java</artifactId>
            <scope>runtime</scope>
        </dependency>
    </dependencies>

</project>
Enter fullscreen mode Exit fullscreen mode
  • VehicleApplication.java
@SpringBootApplication
public class VehicleApplication {
    public static void main(String[] args) {
        SpringApplication.run(VehicleApplication.class);
    }
}
Enter fullscreen mode Exit fullscreen mode
  • Vehicle.java
@Data
@Builder
@AllArgsConstructor
@NoArgsConstructor
@Entity
public class Vehicle {
    @Id
    @SequenceGenerator(name = "vehicle_id_sequence", sequenceName = "vehicle_id_sequence")
    @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "vehicle_id_sequence")
    private Integer id;
    private String plate;
    @Column(name = "parking_entry_date", nullable = false)
    private LocalDateTime vehicleEntryDate;

    @PrePersist
    public void prePersist() {
        vehicleEntryDate = LocalDateTime.now();
    }
}
Enter fullscreen mode Exit fullscreen mode
  • VehicleController.java
@Slf4j
@RestController
@RequestMapping("api/v1/vehicles")
public record VehicleController(VehicleService vehicleService) {
    @PostMapping
    public void registerVehicle(@RequestBody VehicleRegistrationRequest vehicleRegistrationRequest) {
        log.info("New register vehicle {}", vehicleRegistrationRequest);
        vehicleService.registerVehicle(vehicleRegistrationRequest);
    }
}
Enter fullscreen mode Exit fullscreen mode
  • VehicleRequest.java
public record VehicleRegistrationRequest(String plate) {
}
Enter fullscreen mode Exit fullscreen mode
  • VehicleRepository.java
public interface VehicleRepository extends JpaRepository<Vehicle, Integer> {
}
Enter fullscreen mode Exit fullscreen mode
  • VehicleService.java
@Service
public record VehicleService(VehicleRepository vehicleRepository) {
    public void registerVehicle(VehicleRegistrationRequest request) {
        Vehicle vehicle = Vehicle.builder()
                .plate(request.plate())
                .build();
        vehicleRepository.save(vehicle);
    }
}
Enter fullscreen mode Exit fullscreen mode

Top comments (0)