[[oktatas:web:back-end_framework:spring_boot|< Spring boot]] ====== Spring Boot kezdés ====== * **Szerző:** Sallai András * Copyright (c) Sallai András, 2021 * Licenc: [[https://creativecommons.org/licenses/by-sa/4.0/|CC Attribution-Share Alike 4.0 International]] * Web: https://szit.hu ===== A Spring Boot ===== Webhely: * https://spring.io/projects/spring-boot ===== Visual Studio Code ===== A következő bővítményre van szükségünk: * Spring Boot Extendsion Pack ===== Új projekt kezdése Visual Studio Code programmal===== Parancs panelon (F1): * Spring initializr: Create a Maven Project... * Specify Spring Boot version * 2.6.1 * Specify project language. * Java * Kotlin * Groovy Írjuk át a projekt tartománynevét: [ com.example ] [ lan.zold ] A projekt nevét: [ demo ] [ hello ] * Specify packaging type. * Jar * War * Specify Java version. * 11 * 17 * 8 Két függőséget "Enter" lenyomásával válasszuk ki, majd az első lehetőséggel menjünk tovább. * Search for dependencies. * ✔ Spring Boot DevTools * ✔ Spring Web ===== Indítás ===== A program belépési pontját a következő helyen találjuk: src/main/java/lan/zold/hello/HelloApplication.java A Run-ra kattintva elindíthatjuk a tomcat szervert. Az induló kód: package lan.zold.hello; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class HelloApplication { public static void main(String[] args) { SpringApplication.run(HelloApplication.class, args); } } Az eredmény: {{:oktatas:web:back-end_framework:spring_boot:spring_boot_start.png|}} ===== Helló ===== package lan.zold.hello; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @SpringBootApplication @RestController public class HelloApplication { public static void main(String[] args) { SpringApplication.run(HelloApplication.class, args); } @GetMapping public String hello() { return "Helló Világ"; } } Mentés után, ha frissítjük a böngészőt: {{:oktatas:web:back-end_framework:spring_boot:spring_boot_hello.png|}} ===== JSON válasz ===== @GetMapping public List hello() { return List.of("alma", "körte", "barack"); } {{:oktatas:web:back-end_framework:spring_boot:spring_boot_result_json.png|}} ===== Dolgozó employee ===== Készítsünk egy employee nevű csomagot, benne az Employee osztállyal: package lan.zold.hello.employee; public class Employee { String name; String city; double salary; public Employee(String name, String city, double salary) { this.name = name; this.city = city; this.salary = salary; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getCity() { return city; } public void setCity(String city) { this.city = city; } public double getSalary() { return salary; } public void setSalary(double salary) { this.salary = salary; } } Használjuk az új osztályt: @GetMapping public List hello() { return List.of( new Employee( "Parnter Ágnes", "Szeged", 2850000 ) ); } ===== Kontroller készítése ===== Készítsünk egy kontrollert, ami visszaad egy karakterláncot. Ehhez készítsünk a **src/main/java/lan/zold/hello/** könyvtárban egy controllers könyvtárat abban egy **EmployeesController.java** fájlt, benne az **EmployeeController** osztállyal. package lan.zold.hello.controllers; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RestController; @RestController public class EmployeesController { @GetMapping("/employees") public String getEmployees() { return "Nagy János, Pál Béla"; } } Mentés utána a böngészőbe írjuk be: http://localhost:8080/employees