๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
  • ์žฅ์›์ต ๊ธฐ์ˆ ๋ธ”๋กœ๊ทธ
๐Ÿ’Š Java & Kotlin & Spring/- spring framework +

์Šคํ”„๋ง๋ถ€ํŠธ ์™€ REST API

by Wonit 2019. 11. 23.

Spring boot ์™€ REST api

์ดˆ๊ธฐ spring ํ”„๋กœ์ ํŠธ๋Š” ์Šคํ”„๋ง ๊ตฌ์„ฑ์— ํ•„์š”ํ•œ XML ์ฝ”๋“œ๊ฐ€ ๋งŽ์•˜๋‹ค. ์ด๋ฅผ ๊ทน๋ณตํ•˜๋ ค๊ณ  ์Šคํ”„๋ง 2.5๋Š” ์• ๋„ˆํ…Œ์ด์…˜ (Annotation) ๊ธฐ๋ฐ˜์œผ๋กœ ์ปดํฌ๋„ŒํŠธ ๊ฒ€์ƒ‰ ๊ธฐ๋Šฅ์„ ๋„์ž…ํ–ˆ๊ณ  ๋งŽ์€ ๋…ธ๋ ฅ์—๋„ ๋ถˆ๊ตฌํ•˜๊ณ  ๋ณต์žกํ•œ ๊ตฌ์„ฑ์—์„œ ๋ฒ—์–ด๋‚˜์ง€ ๋ชปํ•˜์˜€๊ณ  ํŠธ๋žœ์žญ์…˜ ๊ด€๋ฆฌ์™€ ์Šคํ”ผ๋ง mvc๊ฐ™์€ ๊ธฐ๋Šฅ์„ ์‚ฌ์šฉํ•˜๋ ค๋ฉด ๋ช…์‹œ์ ์ธ XML ์ด๋‚˜ ์ž๋ฐ” ๊ตฌ์„ฑ์ด ํ•„์š”ํ–ˆ๋‹ค. ์ด๋Ÿฌํ•œ ๊ฒƒ์„ Spring boot๊ฐ€ ํ•ด๊ฒฐํ•ด์ค€๋‹ค.

์Šคํ”„๋ง๋ถ€ํŠธ์˜ ๊ธฐ๋Šฅ

  • Create stand-alone Spring applications
  • ๋‹จ๋…์‹คํ–‰๊ฐ€๋Šฅํ•œ ์Šคํ”„๋ง์• ํ”Œ๋ฆฌ์ผ€์ด์…˜์„ ์ƒ์„ฑํ•œ๋‹ค.
  • Embed Tomcat, Jetty or Undertow directly (no need to deploy WAR files)
  • ๋‚ด์žฅํ˜• ํ†ฐ์บฃ, ์ œํ‹ฐ ํ˜น์€ ์–ธ๋”ํ† ์šฐ๋ฅผ ๋‚ด์žฅํ•œ๋‹ค.
  • Provide opinionated 'starter' component to simplify your build configuration
  • ๊ธฐ๋ณธ์„ค์ •๋˜์–ด์žˆ๋Š” 'starter' ์ปดํฌ๋„ŒํŠธ๋“ค์„ ์‰ฝ๊ฒŒ ๋นŒ๋“œ์— ์ถ”๊ฐ€ํ•  ์ˆ˜ ์žˆ๋‹ค.
  • Automatically configure Spring whenever possible
  • ๊ฐ€๋Šฅํ•œ ์ž๋™์„ค์ • ๋˜์–ด์žˆ๋‹ค.
  • Provide production-ready features such as metrics, health checks and externalized configuration
  • ์ƒ์šฉํ™”์— ํ•„์š”ํ•œ ํ†ต๊ณ„ ๋ฐ ์ ๊ฒ€, ์™ธ๋ถ€์ƒํƒœ๋ฅผ ์ œ๊ณตํ•œ๋‹ค.
  • Absolutely no code generation and no requirement for XML configuration
  • ์„ค์ •์„ ์œ„ํ•œ XML ์ฝ”๋“œ๋ฅผ ์ƒ์„ฑํ•˜๊ฑฐ๋‚˜ ์š”๊ตฌํ•˜์ง€ ์•Š๋Š”๋‹ค.

Rest API

Repressentational State Transfer์˜ ์•ฝ์–ด๋กœ ํ•˜๋‚˜์˜ URL์€ ํ•˜๋‚˜์˜ ๊ณ ์œ ํ•œ ๋ฆฌ์†Œ์Šค๋ฅผ ๋Œ€ํ‘œํ•˜๋„๋ก ์„ค๊ณ„๋œ๋‹ค ๋ผ๋Š” ๊ฐœ๋…

HTTP -GET method (Controller of MVC)

  • ์ฃผ์†Œ์ฐฝ์— ํŒŒ๋ผ๋ฏธํ„ฐ๊ฐ€ ๋…ธ์ถœ๋œ๋‹ค.
    www.localhost:8080/search?id=account&password=1234
  • ๋ธŒ๋ผ์šฐ์ €์—์„œ ์ฃผ์†Œ์— ๋Œ€ํ•œ ์บ์‹œ๊ฐ€ ์ด๋ฃจ์–ด์ง€๋ฏ€๋กœ, ์ •๋ณด๋ฅผ ์–ป์„ ๋•Œ ์‚ฌ์šฉ ํ•œ๋‹ค.

์‚ฌ์šฉ์ž๋กœ๋ถ€ํ„ฐ ์ ‘์†์„ ๋ฐ›๊ธฐ ์œ„ํ•ด์„œ ์ฃผ์†Œ๋ฅผ ์„ค์ •ํ•ด์•ผ ํ•˜๋Š”๋ฐ ๊ทธ ์ฃผ์†Œ์˜ ๋ฌถ์Œ์„ Controller๋ผ๊ณ  ๋ถ€๋ฅธ๋‹ค.

์šฐ๋ฆฌ๋Š” Controller ํŒจํ‚ค์ง€๋ฅผ ๋งŒ๋“ค๊ณ  ๋งคํ•‘๋œ ์ฃผ์†Œ์— ๋”ฐ๋ผ์„œ ๋‹ค๋ฅธ ๊ธฐ๋Šฅ์„ ํ•  ์ปจํŠธ๋กค๋Ÿฌ ํด๋ž˜์Šค๋ฅผ ๋งŒ๋“ค์–ด ๋ณด์ž

๊ฒฝ๋กœ: com.test.study.controller/GetController.java/

package com.test.study.controller;

import org.springframework.web.bind.annotition.RequestMapping
import org.springframework.web.bind.annotition.RequestMethod
import org.springframework.web.bind.annotition.RestController

@RestController
/* 
@RestController ์€ @Controller ์–ด๋…ธํ…Œ์ด์…˜๊ณผ @ResponseBody ์–ด๋…ธํ…Œ์ด์…˜์„
ํ•ฉ์ณ๋†“์€ ์–ด๋…ธํ…Œ์ด์…˜์œผ๋กœ ํด๋ž˜์Šค ์ƒ๋‹จ์— @RestController ์–ด๋…ธํ…Œ์ด์…˜์„ ์„ ์„ ํ•˜๋ฉด Method๋งˆ๋‹ค 
@ResponseBody ๋ฅผ ๋ถ™์—ฌ์ฃผ์ง€ ์•Š์•„๋„ ๋œ๋‹ค.
*/
@RequestMapping("/api"); // URL์„ ์ปจํŠธ๋กค๋Ÿฌ์˜ ๋ฉ”์„œ๋“œ์™€ ๋งคํ•‘ํ•  ๋•Œ ์‚ฌ์šฉํ•˜๋Š” ์Šคํ”„๋ง์˜ ์–ด๋…ธํ…Œ์ด์…˜
public class GetController {

    /*
        Method๋Š” ์–ด๋– ํ•œ ํƒ€์ž…์œผ๋กœ ๋ฐ›์„ ๊ฒƒ์ธ์ง€ (Post,GET,...) ์–ด๋…ธํ…Œ์ด์…˜ ์ธ์ž 1์—์„œ ๊ฒฐ์ •ํ•˜๊ณ 
        path๋Š” ์–ด๋– ํ•œ ์ฃผ์†Œ๋กœ ๋ฐ›๊ฒ ๋‹ค.๋ฅผ ๋œปํ•œ๋‹ค.
    */
    @RequestMapping(method = RequestMethod.GET, path = "/getMethod") // -> localhost:8080/api/getMethod
    public String getRequest() {

        return "Hi getMethod";

    }

}

์‚ฌ์šฉ์ž์—๊ฒŒ ์ง€์ •๋œ URL์„ ํ†ตํ•ด์„œ "Hi getMethod ๋ฅผ ํ‘œ์‹œํ•ด์ฃผ์—ˆ๋‹ค." ํ•˜์ง€๋งŒ ์‚ฌ์šฉ์ž์˜ ์š”์ฒญ์„ ๋ฐ›์„ ๋ฐฉ๋ฒ•์ด ์—†๊ธฐ ๋•Œ๋ฌธ์—
๊ทธ ๋ณด์™„์„ ํ•ด๋ณด์ž

package com.test.study.controller;

import org.springframework.web.bind.annotition.RequestMapping

@RestController
@RequestMapping("/api"); // URL์„ ์ปจํŠธ๋กค๋Ÿฌ์˜ ๋ฉ”์„œ๋“œ์™€ ๋งคํ•‘ํ•  ๋•Œ ์‚ฌ์šฉํ•˜๋Š” ์Šคํ”„๋ง์˜ ์–ด๋…ธํ…Œ์ด์…˜
public class GetController {


    @RequestMapping(method = RequestMethod.GET, path = "/getMethod"))
    public String getRequest() {

        return "Hi getMethod";

    }

    @Getmapping("/getParameter") //getMapping ์œผ๋กœ Parameter์„ ๋ฐ›์•„์ค€๋‹ค.
    public String getParam(@RequestParam String id, @RequestParam String password){
        return id+password;
    }
}

์ด๋ ‡๊ฒŒ ์‚ฌ์šฉ์ž์—๊ฒŒ ์ •๋ณด๋ฅผ ๋ณด์—ฌ์ค„ ๋ฟ ์•„๋‹ˆ๋ผ ์‚ฌ์šฉ์ž๊ฐ€ ์ž…๋ ฅํ•œ , ์š”์ฒญํ•œ ์ •๋ณด๋ฅผ ๋ฐ›์„ ์ˆ˜ ์žˆ๊ฒŒ ๋˜์—ˆ๋‹ค.

ํ•˜์ง€๋งŒ ๋ณ€์ˆ˜๊ฐ€ ๋ฐš์•„์ง„๋‹ค๋ฉด ์–ด๋–ป๊ฒŒ ๋ ๊นŒ?

์ง€๊ธˆ ์œ„์—์„œ๋Š” ํŒŒ๋ผ๋ฏธํ„ฐ๋กœ id์™€ password๋งŒ ๋ฐ›์ง€๋งŒ ๋งŒ์•ฝ (Email, Address, name, id, password)์„ ๋ฐ›๋Š”๋‹ค๊ณ  ์น˜์ž.


@Getmapping("/getParameter") //getMapping ์œผ๋กœ Parameter์„ ๋ฐ›์•„์ค€๋‹ค.
public String getParam(@RequestParam String id, 
                        @RequestParam String email,
                        @RequestParam String Address,
                        @RequestParam String name,
                        @RequestParam String id,
                        @RequestParam String password,
                        ){
    return id+password;
}

์ด๋Ÿฐ์‹์œผ๋กœ ์‚ฌ์šฉ์ž์—๊ฒŒ ๋ฐ›์•„์•ผํ•  Data๊ฐ€ ๋งŽ์•„์ง„๋‹ค๋ฉด ๊ฐ์ฒด๋ฅผ ํ†ตํ•ด์„œ ํ•˜๋‚˜์˜ ๋ฌถ์Œ์œผ๋กœ ๋ฐ›๋Š”๊ฒƒ์ด ํ›จ์”ฌ ํŽธํ•  ๊ฒƒ์ด๋‹ค.

์ด์ œ mvc์˜ model์— ๋Œ€ํ•ด์„œ ๋“ค์–ด๊ฐ€๋ณด์ž.


Model (Model of MVC)


๊ฒฝ๋กœ: com.test.study.model/SearchParam.java

์•„๊นŒ controller์„ ์…œ์น˜ํ•œ ๊ฒฝ๋กœ์— model์ด๋ผ๋Š” ํŒจํ‚ค์ง€๋ฅผ ์ƒ์„ฑํ•˜๊ณ  SearchParam ์ด๋ผ๋Š” ํด๋ž˜์Šค๋ฅผ ์ƒ์„ฑํ•œ๋‹ค.

//SearchParam.java

public class Searchparam {
    private String email;
    private String address;
    private String name;
    private String id;
    private String password;

    public String getEmail(){
        return email;
    }

    public void setEmail(String email){
        this.email = email;
    }
    ... getter/setter ๊ฒŒ์† ์ž‘์„ฑ
}

ํ›„์— ๋‹ค์‹œ controller ํŒจํ‚ค์ง€๋กœ ๋Œ์•„์™€์„œ ๋งคํ•‘์„ ์‹œ์ผœ์ค€๋‹ค.

ํ•˜์ง€๋งŒ ์ด๋ฒˆ์—๋Š” @RequestParam ์–ด๋…ธํ…Œ์ด์…˜์œผ๋กœ ๋งคํ•‘ํ•˜๋Š”๊ฒŒ ์•„๋‹ˆ๋ผ ๊ฐ์ฒด ํ˜•์‹์œผ๋กœ ๋งคํ•‘์„ ์‹œ์ผœ์ค€๋‹ค.

@GetMapping("/getMultParam")
public String getMultP(SearchParam searchparam) {
    return "ok";
}

ํ•˜์ง€๋งŒ ์šฐ๋ฆฌ๋Š” ๋„คํŠธ์›Œํฌ ํ†ต์‹ ์„ ํ•  ๋•Œ JSON ํ˜•์‹์œผ๋กœ Data๊ฐ€ ํ‘œํ˜„๋œ๋‹ค๋Š” ๊ฒƒ์„ ์•Œ๊ณ  ์žˆ๋‹ค ๊ทธ๋Ÿฌ๋‚˜ Servlet,jsp

์—์„œ๋Š” JSON ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์‚ฌ์šฉํ–ˆ์ง€๋งŒ ์Šคํ”„๋ง์€ return ๊ฐ’์„ ๊ฐ์ฒด๋กœ ์ฃผ๋ฉด ๋œ๋‹ค.

์ง€๊ธˆ์€ String ํ˜•์‹์œผ๋กœ Data๋ฅผ ๋ฐ›์•˜๊ธฐ ๋•Œ๋ฌธ์— ๋ฐ์ดํ„ฐ๋ฅผ ๊ฐ€๊ณตํ•˜๋Š”๋ฐ ์–ด๋ ค์›€์ด ์žˆ์œผ๋ฏ€๋กœ JSON ํ˜•์‹์œผ๋กœ

๋ฐ›์•„๋ณด์ž.

@GetMapping("/getMultParam")
public SearchParam getMultP(SearchParam searchparam) {
    return searchParam;
}

์œผ๋กœ ์ง€์ •ํ•ด์ค€๋‹ค๋ฉด JSON ํƒ€์ž…์œผ๋กœ ๋ฐ›์„ ์ˆ˜ ์žˆ๊ฒŒ ๋œ๋‹ค.


HTTP -POST method (Controller of MVC)


  • ์ฃผ์†Œ์ฐฝ์— ํŒŒ๋ผ๋ฏธํ„ฐ๊ฐ€ ๋…ธ์ถœ๋˜์ง€ ์•Š๋Š”๋‹ค
    www.localhost:8080/search
  • ์ฃผ์†Œ ์ฐฝ์— ์‚ฌ์šฉ์ž์˜ ์š”์ฒญ ์‚ฌํ•ญ์ด ๋…ธ์ถœ๋˜์ง€ ์•Š๋Š”๋‹ค.
  • GET ๋ฐฉ์‹์—์„œ๋Š” ์ฃผ์†Œ ๊ธธ์ด ์ œํ•œ์ด ์žˆ์ง€๋งŒ POST๋Š” ๊ทธ๋ณด๋‹ค ๊ธธ๊ฒŒ ์‚ฌ์šฉ ๊ฐ€๋Šฅํ•˜๋‹ค (์ œํ•œ ์กด์žฌ)
  • ๋ธŒ๋ผ์šฐ์ €๊ฐ€ ์ฃผ์†Œ ์บ์‹œ๋ฅผ ํ•˜์ง€๋ชปํ•˜๋Š” ํŠน์„ฑ์ด ์žˆ๋‹ค.

package com.test.study.controller;

import org.springframework.web.bind.annotition.RequestMapping
import org.springframework.web.bind.annotition.RequestMethod
import org.springframework.web.bind.annotition.RestController

@RestController
@RequestMapping("/api"); // URL์„ ์ปจํŠธ๋กค๋Ÿฌ์˜ ๋ฉ”์„œ๋“œ์™€ ๋งคํ•‘ํ•  ๋•Œ ์‚ฌ์šฉํ•˜๋Š” ์Šคํ”„๋ง์˜ ์–ด๋…ธํ…Œ์ด์…˜
public class GetController {

    @PostMappint(value = "/postMethod")
    public SearchParam postMehod(SearchParam searchParam) {

        return "searchParam";

    }

}


HTTP - PUT/PATCH/DELETE

  • PUT/PATCH ๋Š” POST์™€ ๋งˆ์ฐฌ๊ฐ€์ง€๋กœ BODY์— ๋ฐ์ดํ„ฐ๊ฐ€ ๋“ค์–ด์žˆ์œผ๋ฉฐ, ์ฃผ๋กœ ์—…๋ฐ์ดํŠธ์— ์‚ฌ์šฉ๋œ๋‹ค.

๊ฒฐ๋ก 

REST API์˜ ๊ฐœ๋…

  • Http ํ”„๋กœํ† ์ฝœ์— ์žˆ๋Š” Method๋ฅผ ํ™œ์šฉํ•œ ์•„ํ‚คํ…์ฒ˜ ์Šคํƒ€์ผ์ด๋‹ค.
  • Http ๋ฉ”์„œ๋“œ๋ฅผ ํ†ตํ•ด์„œ Resource๋ฅผ ์ฒ˜๋ฆฌํ•œ๋‹ค.
  • CRUD๋ฅผ ํ†ตํ•œ Resource๋ฅผ ์กฐ์ž‘ํ•  ๋•Œ ์‚ฌ์šฉํ•œ๋‹ค.

๋Œ“๊ธ€