간단하게 실행
되며, 프로덕션 제품 수준실제로 상품이 되어서 사용자에게 제공 될 수 있는 수준
의 어플리케이션을 쉽게 만들 수 있다.주요 목표
Spring 보다 개발 자체가 굉장히 간단해짐
간단하게 웹 어플리케이션 서버를 만들 수 있게 됨
Build Tool
Name | version |
Maven | 3.3+ |
Gradle | 4.x(4.4 and later) and 5.x |
Servlet Containers
Name | servlet version |
Tomcat 9.x | 3.3 |
Jetty 9.4 | 3.1 |
Undertow 2.0 | 4.0 |
Netty | - |
인텔리제이에서는 https://startsping.io 를 통해 옵션들을 인텔리제이 안으로 plugin해 화면에 보여주는 역할을 하기 때문에 sprig initializr를 통하지 않더라도 동일한 옵션들을 선택해서 프로젝트를 생성할 수 있음
핵심
jar파일로 패키징이 되어서, java가 설치되어 있는 어떤 곳이든 java/jar라는 명령어로 jar파일을 java 어플리케이션을 실행시킴으로써 간단하게 웹 서버 어플리케이션 실행가능
Q. 개발을 할 때, Get 방식 이외에 POST, PUT, DELETE 같은 HTTP Method를 작성을 해야하는데, 어떻게 테스트를 하느냐?
A. 테스트할 수 있는 tool을 제공해준다.
크롬 웹 스토어
브라우저에 설치
-> Talend API Tester -> Chrome에 추가 -> 확장 프로그램에 추가
package com.example.hello.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController // 해당 Class는 REST API를 처리하는 Controller
@RequestMapping(value = "/api") // RequestMapping URI를 지정해주는 Annotation
public class ApiController {
@GetMapping(value = "/hello") // http://localhost:8080/api/hello
public String hello(){
return "hello spring boot!";
}
}
Spring에서 Controller를 작동시키기 위해서는 class를 만든 다음에 위에 @RestController라는 Annotation을 붙여주고 주소로 할당해주기 위해서는 @RequestMapping을 사용해야한다.
마찬가지로 Get 방식으로 API를 열어주기 위해 @GetMapping을 사용하고 항상 괄호 안에는 주소가 들어간다.
→ @GetMapping을 통해서 주소를 할당
기본적으로 Java 개발하듯 method 하나 작성한 다음에 내가 return 하고 싶은 내용을 작성하면 SpringBoot가 알아서 해당 Message들을 web에서 내려가는 http response로 만들어서 return 시켜줌
강의에서는 한달동안 무료로 이용할 수 있는 Ultimate 버전으로 수업을 진행하시지만, 이미 Community 버전을 설치했었고 30일 무료를 지금 쓰긴 아까워서😵 Community 버전으로 계속해서 사용하려고 한다!
@GetMapping()
HTTP GET 요청을 처리하는 메서드를 맵핑(@RequestMapping) 하는 어노테이션
실제로 프로젝트에서 Get을 따로 떼어서 보진 않음 ! 실습을 위해 !
package com.example.hello.controller;
import org.springframework.web.bind.annotation.*;
@RestController // 주소 부여
@RequestMapping("/api/get")
public class GetApiController {
@GetMapping(path="/hello")
public String getHello(){ // http://localhost:8080/api/get/hello
return "get Hello";
}
@RequestMapping(path = "/hi", method = RequestMethod.GET) // get http://localhost:8080/api/get/hi
public String hi(){
return "hi";
}
// http://localhost:8080/api/get/path-variable/{name}
// http://localhost:8080/api/get/path-variable/{g2} ...
@GetMapping("/path-variable/{name}")
public String pathVariable(@PathVariable String name){
System.out.println("PathVariable : "+name);
return name;
}
}
📝 변수의 name과 @GetMapping의 {name}이 동일해야 한다.
("/path-variable/{name}")을 해두었지만 변수에 이름을 다르게 설정해야 할 때 / 이름을 일치 시킬 수 없을 때
📝 다음과 같이 변수에 이름을 matching 시켜줄 때 변수에 이름을 다르게 선언하되, @PathVariable의 해당 name의 속성을 맞춰준다.
@GetMapping("/path-variable/{name}")
public String pathVariable(@PathVariable(name = "name") String pathName){
System.out.println("PathVariable : "+pathName);
return pathName;
}
검색을 할 때 여러 매개변수들의 인자를 말함
http://localhost:8080/api/get/query-param?user=g2&email=hon9g2@gmail.com&age=30
?key=value&key2=value2
? : 처음 시작 시 붙임 -> 그 다음 key1=value1 -> 그 다음에 key,value를 연결하기 위해선 &연산자-> 그 다음 key2=value2
@GetMapping(path = "query-param")
public String queryParam(@RequestParam Map<String,String> queryParam){
StringBuilder sb = new StringBuilder();
queryParam.entrySet().forEach(entry -> {
System.out.println(entry.getKey());
System.out.println(entry.getValue());
System.out.println("\n");
sb.append(entry.getKey()+" = "+entry.getValue()+"\n");
});
return sb.toString();
}
@GetMapping("query-param02")
public String queryParam02(
@RequestParam String name,
@RequestParam String email,
@RequestParam int age
){
System.out.println(name);
System.out.println(email);
System.out.println(age);
return name+" "+email+" "+age;
}
@GetMapping("query-param03")
public String queryParam03(UserRequest userRequest){
System.out.println(userRequest.getName());
System.out.println(userRequest.getEmail());
System.out.println(userRequest.getAge());
return userRequest.toString();
}