ํ๋ก์ ํธ ์์ฑ
์ง๊ธ๊น์ง๋ ํญ์ start.spring.io๋ฅผ ์ด์ฉํด์๋๋ฐ, IntelliJ ์์์ ๋ฐ๋ก ํ๋ก์ ํธ๋ฅผ ์์ฑํ ์๋ ์๋ค!
์๋์ฒ๋ผ dependency๋ฅผ ์ถ๊ฐํ๋ฉด pom.xml์ ์๋์ผ๋ก ์ถ๊ฐ๋๋ค.
- Spring web → embedded tomcat ์คํ ๊ฐ๋ฅ
REST API ์ค๊ณ
HTTPS | API | Name |
GET | /users | retrieve all users |
POST | /users | create a user |
GET | /users/{id} | retrieve one user |
DELETE | /users/{id} | delete a user |
GET | /users/{id}/posts | retrieve all posts for a user |
POST | /users/{id}/posts | create a post for a user |
GET | /users/{id}/posts/{post_id} | retrieve details of a user |
๊ตฌ์กฐ ํ์ธ
- application.properties : ์คํ๋ง ๋ถํธ ํ๊ฒฝ ์ค์ ์ง์ (application.yml๋ก ๋ฐ๊พธ๋ ๊ฒ ๋ ํธํ๋ค)
- pom.xml : ์ ์ฒด ํ๋ก์ ํธ์ ํ์ํ maven ์ค์ ๊ด๋ฆฌ
Controller
RESTController ํด๋์ค๋ก ์๋ํจ(์ผ๋ฐ MVC Controller์๋ ๋ค๋ฆ)
@RestController
- Mapping
// GET
@RequestMapping(method=RequestMethod.GET, path="/hello-world")
@GetMapping(path = "/hello-world")/
Bean
์คํ๋ง์์ ์ ์ธ๋์ด ๊ด๋ฆฌ๋๋ ์ธ์คํด์ค ⇒ ์์กด์ฑ ์ฃผ์ ์ผ๋ก ๊ด๋ฆฌ๋จ
- BEAN ํ์ : ๊ธฐ๋ณธ์ ์ผ๋ก JSON ํ์์ผ๋ก ๋ณํ๋์ด ๋ฐํ๋๋ค
// Bean
@Data
@AllArgsConstructor -> ๋ชจ๋ ์์ฑ์
@NoArgsConstructor -> ๊ธฐ๋ณธ ์์ฑ์
DispatcherServlet๊ณผ ํ๋ก์ ํธ ๋์์ ์ดํด
๋์ ์๋ฆฌ
- application.yml → ์ค์ ์ด๋ฆ: ๊ฐ
- application.properties → ์ค์ ์ด๋ฆ=๊ฐ
server:
port: 8088
logging:
level:
org.springframework: DEBUG
DispatcherServlet → “/”
ํด๋ผ์ด์ธํธ์ ๋ชจ๋ ์์ฒญ์ ํ ๊ณณ์ผ๋ก ๋ฐ์์ ์ฒ๋ฆฌ
→ ์์ฒญ์ ๋ง๋ handler๋ก ์์ฒญ ์ ๋ฌ
→ handler์ ์คํ ๊ฒฐ๊ณผ๋ฅผ http response ํํ๋ก ๋ง๋ค์ด์ ๋ฐํ
์คํ ์์ : http request → dispatcher servlet → handler mapping → REST controller → http response
RestController
@RestController → @Controller + @RequestBody
View๋ฅผ ๊ฐ์ง ์๋ REST Data(JSON/XML) ๋ฐํ(์๋์ผ๋ก ResponseBody์ ๋ฃ์ด์ค)
Path Variable
์ด ๋ณ์๊ฐ ๊ฐ๋ณ ๋ฐ์ดํฐ๊ฐ ๋ ๊ฒ์์ ๋ช ์
@GetMaping(path="/intro/{name}")
public HelloWorldBean helloWorldBean(@PathVariable String name){
return new HelloWorldBean(String.format("Hello World, %s", name));
}
[๋ค๋ฅธ ๊ฐ์ ์ฌ์ฉํ๋ ๊ฒฝ์ฐ]
@GetMaping(path="/intro/{name}")
public HelloWorldBean helloWorldBean(@PathVariable(value="hey")String name){
return new HelloWorldBean(String.format("Hello World, %s", name));
}
'๐ฟ Spring' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[Spring Boot/RESTful] Validation API์ Internationalization (0) | 2022.02.18 |
---|---|
[Spring Boot/RESTful] User Service API ๊ตฌํ (0) | 2022.02.17 |
[Spring Boot/RESTful] Web Service & Web Application (0) | 2022.02.17 |
[Error] Spring Boot(2.5x)์์ data.sql ์คํ ์ค๋ฅ(H2 database) (0) | 2022.02.02 |
[Error] H2 ์ถ๊ฐ ๋ฐ์ดํฐ๋ฒ ์ด์ค ์์ฑ - IO Exception: null [90028-200] (0) | 2022.01.30 |
๋๊ธ