๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
๐ŸŒฟ Spring/โœ… RESTful Web Service

1. Spring Boot๋กœ ๊ฐœ๋ฐœํ•˜๋Š” RESTful Service

by nitronium102 2023. 6. 20.

ํ”„๋กœ์ ํŠธ ์ƒ์„ฑ

dependency ⇒ pom.xml

  • JPA
  • Spring web → embedded tomcat ์‹คํ–‰ ๊ฐ€๋Šฅ

REST API ์„ค๊ณ„

User → Posts

 

๊ตฌ์กฐ ํ™•์ธ

application.properties → 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๊ณผ ํ”„๋กœ์ ํŠธ ๋™์ž‘์˜ ์ดํ•ด

๋™์ž‘ ์›๋ฆฌ

1) application.yml → ์„ค์ •์ด๋ฆ„: ๊ฐ’

2) 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 > โœ… RESTful Web Service' ์นดํ…Œ๊ณ ๋ฆฌ์˜ ๋‹ค๋ฅธ ๊ธ€

5. Java Persistence API ์‚ฌ์šฉ  (0) 2023.06.20
4. Spring Boot API ์‚ฌ์šฉ  (0) 2023.06.20
3. RESTful Service ๊ธฐ๋Šฅ ํ™•์žฅ  (0) 2023.06.20
2. User Service API ๊ตฌํ˜„  (0) 2023.06.20
0. Web Service & Web Application  (0) 2023.06.20

๋Œ“๊ธ€