Spring Boot 프로젝트를 생성하고, Controller 클래스와 View를 구현한다.
이후 이 실행 절차에 대해서 살펴본다.
server.port=8088
Spring Boot의 설정 파일
package net.skhu.dto;
public class Product {
String name;
int unitCost;
public Product(String name, int unitCost) {
this.name = name;
this.unitCost = unitCost;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getUnitCost() {
return unitCost;
}
public void setUnitCost(int unitCost) {
this.unitCost = unitCost;
}
}
package net.skhu.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import net.skhu.dto.Product;
@RestController
@RequestMapping("first")
public class FirstController{
@GetMapping("test1")
public String test1(){
return "안녕하세요";
}
@GetMapping("test2")
public String[] test2() {
return new String[] { "월", "화", "수", "목", "금", "토", "일" };
}
@GetMapping("test3")
public Product test3() {
return new Product("맥주", 2000);
}
@GetMapping("test4")
public Product[] test4() {
return new Product[] { new Product("맥주", 2000), new Product("우유", 1500)
};
}
}
Controller 클래스는 웹브라우저의 URL 요청을 받아서, 웹 서버에서 실행되는 클래스이다.
웹 브라우저가 웹 서버에 어떤 URL을 요청을 하면, 그 URL에 해당하는 컨트롤러의 액션 메소드가 자동으로 호출되어 실행된다.
컨트롤러의 클래스에는 어노테이션(@RestController, @controller)이 붙어있어야한다.