Maven과 Spring Boot를 사용한 소규모 서비스
mvn archetype:generate -DgroupId=com.parkingservices.app -DartifactId=parking-services -DarchetypeArtifactId=maven-archetype-quickstart -DarchetypeVersion=1.4 -DinteractiveMode=false
IntelliJ로 열고 src 폴더 삭제
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>
이제 새 모듈을 추가해야 합니다.
프로젝트명 우클릭 > New > Module
배너 스프링 부트 만들기
Online Spring Boot Banner Generator
이제 응용 프로그램을 실행
폼 차량
<?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>
@SpringBootApplication
public class VehicleApplication {
public static void main(String[] args) {
SpringApplication.run(VehicleApplication.class);
}
}
@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();
}
}
@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);
}
}
public record VehicleRegistrationRequest(String plate) {
}
public interface VehicleRepository extends JpaRepository<Vehicle, Integer> {
}
@Service
public record VehicleService(VehicleRepository vehicleRepository) {
public void registerVehicle(VehicleRegistrationRequest request) {
Vehicle vehicle = Vehicle.builder()
.plate(request.plate())
.build();
vehicleRepository.save(vehicle);
}
}
Reference
이 문제에 관하여(Maven과 Spring Boot를 사용한 소규모 서비스), 우리는 이곳에서 더 많은 자료를 발견하고 링크를 클릭하여 보았다 https://dev.to/c4rlosc7/small-service-using-maven-and-spring-boot-51ik텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
우수한 개발자 콘텐츠 발견에 전념 (Collection and Share based on the CC Protocol.)