Spring Boot 기초
아래 그림과 같이 appication.properties 에서 모든걸 control 한다.
Springframework에서 servlet-context 역할도 수행한다.
server.port 를 지정하여 localhost 주소값을 설정한다.
아래 그림과 같이 Sppring Boot App 을 이용하여 실행한다.
실행하면 console 창에 아래 그림과 같이 Spring 실행 문구가 발생한다
인터넷 url 창에 직접 localhost:포트번호 를 입력하여 확인해야 한다.
아래처럼 하얀창이 뜨면 일단 실행이 된 것이다.
예제 1
package boot.mvc.ex1;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication
@ComponentScan({"boot.test","hello.boot","my.info"}) // 패키징 등록하는것. 복수일때에는 반드시 {}안에 해줘야 한다.
public class SpringBootEx111Application {
public static void main(String[] args) {
SpringApplication.run(SpringBootEx111Application.class, args);
}
}
package my.info;
import java.util.HashMap;
import java.util.List;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ResponseBody;
@Controller
public class InfoController {
@GetMapping("/my/info")
@ResponseBody
public HashMap<String, String> info(){
HashMap<String, String> myinfo=new HashMap<>();
myinfo.put("name", "장순영");
myinfo.put("age", "23");
myinfo.put("addr", "서울시 강남구");
// 출력 순서는 random이다.
return myinfo;
}
}
package hello.boot;
import java.util.HashMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class HelloController {
@GetMapping("/sist/hello")
public HashMap<String, String> hello(){
HashMap<String, String> data=new HashMap<>();
data.put("message", "오늘은 스프링부트 배우는날");
return data;
}
}
package boot.mvc.ex1;
import lombok.Data;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;
//@Setter
//@Getter
//@ToString
@Data // 이거 하나면 @Setter,@Getter,@ToString 3개 하는것과 동일한 효과를 가져온다.
// lombok을 깔아서 Setter,Getter 생성을 따로 안해도 된다.
public class TestDto {
private String name;
private String addr;
}
package boot.test;
import java.util.ArrayList;
import java.util.List;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import boot.mvc.ex1.TestDto;
@RestController
public class TestController {
@GetMapping("/test")
public TestDto hell() {
TestDto dto=new TestDto();
dto.setName("뽀로로");
dto.setAddr("남극");
return dto;
}
}
package boot.test;
import java.util.ArrayList;
import java.util.List;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import boot.mvc.ex1.ShopDto;
@RestController
public class TestController {
@GetMapping("/shop/list")
public List<ShopDto> list(){
List<ShopDto> list=new ArrayList<>();
ShopDto d1=new ShopDto();
d1.setSang("초코파이");
d1.setSu(2);
d1.setDan(1000);
ShopDto d2=new ShopDto();
d2.setSang("메로나");
d2.setSu(3);
d2.setDan(1500);
list.add(d1);
list.add(d2);
return list;
}