Server/SpringBoot

[์›น๊ฐœ๋ฐœ์˜ ๋ด„, Spring] 4์ฃผ์ฐจ ๊ฐœ๋ฐœ์ผ์ง€(1) - ๋„ค์ด๋ฒ„ ์‡ผํ•‘ API ์ด์šฉํ•˜๊ธฐ

meeeeejin 2021. 7. 16. 15:48

 

๊ฐœ๋ฐœ์˜ ํ•ต์‹ฌ ์ค‘ ํ•˜๋‚˜๋Š” ๋ถ„์—…๊ณผ ๋Š์Šจํ•œ ๊ฒฐํ•ฉ์ด๋‹ค. ๋ถ„์—…์„ ํ†ตํ•ด ๊ธฐ๋Šฅ์„ ์˜จ์ „ํžˆ ์ž‘๋™ํ•˜๊ฒŒ ํ•˜๊ณ , ๋Š์Šจํžˆ ๊ฒฐํ•ฉํ•จ์œผ๋กœ์จ ์œ ์—ฐ์„ฑ๊ณผ ํ™•์žฅ์„ฑ์„ ๊ฐ€์งˆ ์ˆ˜ ์žˆ๋‹ค. ์Šคํ”„๋ง์˜ ์ฒ™์ถ”๋ผ ํ•  ์ˆ˜ ์žˆ๋Š” Controller, Service, Repository 3๊ณ„์ธต์€ ๋ถ„์—…๊ณผ ๋Š์Šจํ•œ ๊ฒฐํ•ฉ์˜ ๋Œ€ํ‘œ์ ์ธ ์˜ˆ์‹œ์ด๋‹ค. 

 

๋Š์Šจํ•œ ๊ฒฐํ•ฉ์˜ ๋˜ ๋‹ค๋ฅธ ์˜ˆ์‹œ๊ฐ€ ๋ฐ”๋กœ API์ด๋‹ค. ๋‚ด๊ฐ€ API์˜ ๋‚ด๋ถ€ ๋กœ์ง์ด ์–ด๋–ป๊ฒŒ ์งœ์—ฌ์กŒ๋Š”์ง€ ๋ชจ๋ฅด๋Š” ์ƒํ™ฉ์—์„œ๋„, ๊ณต์‹ ๋ฌธ์„œ์— ๋‚˜์˜จ ์ •ํ•ด์ง„ ์•ฝ์†๋Œ€๋กœ ์š”๊ตฌ๋ฅผ ํ•˜๋ฉด ์ •ํ•ด์ง„ ๊ฒฐ๊ณผ๋ฅผ ๋ฐ›์„ ์ˆ˜ ์žˆ๋‹ค. ๋”ฐ๋ผ์„œ API๋ฅผ "์‚ฌ์šฉ"ํ•  ์ค„ ์•„๋Š” ๊ฒƒ์ด ์ค‘์š”ํ•˜๋‹ค. 

 

์ด๋ฒˆ ์ฃผ์ฐจ ์ˆ˜์—… ๋‚ด์šฉ์€ ์Šคํ”„๋ง์˜ 3๊ณ„์ธต๊ณผ API handling ์—ฐ์Šต์— ์ค‘์ ์„ ๋’€๋‹ค. ์ด๋ฒˆ์— ๋งŒ๋“ค ๊ฒƒ์€ <๋‚˜๋งŒ์˜ ์…€๋ ‰์ƒต>์ด๋‹ค. ์š”๊ตฌ ๊ธฐ๋Šฅ์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. 

  • ์ƒํ’ˆ๋ช…์— ๋”ฐ๋ฅธ ๊ฒ€์ƒ‰
  • ๊ด€์‹ฌ ์ƒํ’ˆ ๋“ฑ๋ก & ์กฐํšŒ
  • ๊ด€์‹ฌ ์ƒํ’ˆ์— ๋Œ€ํ•œ ์ตœ์ €๊ฐ€ ๋“ฑ๋กํ•˜๊ธฐ

์•„๋ž˜ ์ฃผ์†Œ์—์„œ ๋ฐ๋ชจ๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค. 

http://spring.spartacodingclub.kr/

 

 

ํ”„๋กœ์ ํŠธ ์„ค๊ณ„ํ•˜๊ธฐ

๐Ÿ’ก API ์„ค๊ณ„ํ•˜๊ธฐ

์ž!!!! ์ด์ œ API๋ฅผ ์„ค๊ณ„ํ•  ์‹œ๊ฐ„์ด๋‹ค. ์ฒ˜์Œ์— ๋‚˜๋Š” ์ƒํ’ˆ๋ช…์— ๋”ฐ๋ฅธ ๊ฒ€์ƒ‰๊ณผ ๊ด€์‹ฌ ์ƒํ’ˆ ๋“ฑ๋ก์˜ url์„ ๊ตฌ๋ถ„ํ•˜์ง€ ์•Š๊ณ  ํ•˜๋ คํ•ด์„œ ๋ณต์žกํ•ด์งˆ ๋ป” ํ–ˆ๋Š”๋ฐ, ๊ฐ•์˜๋ฅผ ๋ณด๋‹ˆ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ๊น”๋”ํ•˜๊ฒŒ ์ •๋ฆฌํ•  ์ˆ˜ ์žˆ์—ˆ๋‹ค.

 

๊ธฐ๋Šฅ Method URL return
ํ‚ค์›Œ๋“œ๋กœ ์ƒํ’ˆ ๊ฒ€์ƒ‰ํ•˜๊ณ  ๊ทธ ๊ฒฐ๊ณผ๋ฅผ ๋ชฉ๋ก์œผ๋กœ ๋ณด์—ฌ์ฃผ๊ธฐ GET /api/search?query=๊ฒ€์ƒ‰์–ด List<ItemDto>
๊ด€์‹ฌ ์ƒํ’ˆ ๋“ฑ๋กํ•˜๊ธฐ POST /api/products Product
๊ด€์‹ฌ ์ƒํ’ˆ ์กฐํšŒํ•˜๊ธฐ GET /api/products List<Product>
๊ด€์‹ฌ ์ƒํ’ˆ์— ๊ด€์‹ฌ ๊ฐ€๊ฒฉ ๋“ฑ๋กํ•˜๊ณ , ์ƒํ’ˆ ๊ฐ€๊ฒฉ์ด ๋” ๋‚ฎ์€ ๊ฒฝ์šฐ ํ‘œ์‹œํ•˜๊ธฐ PUT /api/products/{id} id

 

 

๐Ÿ’ก 3๊ณ„์ธต ์„ค๊ณ„ํ•˜๊ธฐ

1. Controller

  • ProductRestController: ๊ด€์‹ฌ ์ƒํ’ˆ ๊ด€๋ จ ์ปจํŠธ๋กค๋Ÿฌ
  • SearchRequestContorller: ๊ฒ€์ƒ‰ ๊ด€๋ จ ์ปจํŠธ๋กค๋Ÿฌ

2. Service

  • ProductService: ๊ด€์‹ฌ ์ƒํ’ˆ ๊ฐ€๊ฒฉ ๋ณ€๊ฒฝ

3. Repository

  • Product: ๊ด€์‹ฌ ์ƒํ’ˆ ํ…Œ์ด๋ธ”
  • ProductRepository: ๊ด€์‹ฌ ์ƒํ’ˆ ์กฐํšŒ, ์ €์žฅ
  • ProductRequestDto: ๊ด€์‹ฌ ์ƒํ’ˆ ๋“ฑ๋กํ•˜๊ธฐ
  • ProductMypriceRequestDto: ๊ด€์‹ฌ ๊ฐ€๊ฒฉ ๋ณ€๊ฒฝํ•˜๊ธฐ
  • ItemDto: ๊ฒ€์ƒ‰ ๊ฒฐ๊ณผ ์ฃผ๊ณ ๋ฐ›๊ธฐ (DB์— ์ €์žฅํ•  ํ•„์š”X)

 

์ด์ œ "์ƒํ’ˆ๋ช…์— ๋”ฐ๋ฅธ ๊ฒ€์ƒ‰" ๊ธฐ๋Šฅ์„ ๋งŒ๋“ค๊ธฐ ์œ„ํ•ด ๋„ค์ด๋ฒ„ ์‡ผํ•‘ API๋ฅผ ์ด์šฉํ•ด๋ณด์ž~~

 

 

 

 

๋„ค์ด๋ฒ„ ์‡ผํ•‘ API ์ด์šฉํ•˜๊ธฐ

๐Ÿ’ก API ์ด์šฉ ์‹ ์ฒญํ•˜๊ธฐ

  1. https://developers.naver.com/docs/search/shopping/์— ์ ‘์†ํ•œ๋‹ค. 
  2. "์˜คํ”ˆ API ์ด์šฉ ์‹ ์ฒญ" ๋ฒ„ํŠผ์„ ํด๋ฆญํ•œ๋‹ค. 
  3. ๋„ค์ด๋ฒ„ ๋กœ๊ทธ์ธ์„ ์ง„ํ–‰ํ•œ๋‹ค. 
  4. ์• ํ”Œ๋ฆฌ์ผ€์ด์…˜ ์ด๋ฆ„๊ณผ ์„œ๋น„์Šค ํ™˜๊ฒฝ์„ ์ถ”๊ฐ€ํ•œ ๋’ค "๋“ฑ๋กํ•˜๊ธฐ"๋ฅผ ํด๋ฆญํ•œ๋‹ค. 
  5. Client ID์™€ Client Secret์„ ํ™•์ธํ•œ๋‹ค. 

 

 

๐Ÿ’ก API ์ด์šฉ ๋ฐฉ๋ฒ•

๋„ค์ด๋ฒ„ ์‡ผํ•‘ ๊ฒ€์ƒ‰ API์— ๋Œ€ํ•œ ์„ค๋ช…์€ https://developers.naver.com/docs/search/shopping/์— ๋‚˜์™€์žˆ๋‹ค. 

JSON ํ˜•์‹์œผ๋กœ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฐ›๊ณ ์‹ถ๋‹ค๋ฉด, https://openapi.naver.com/v1/search/shop.json์— GET ๋ฐฉ์‹์œผ๋กœ ์š”์ฒญ์„ ๋ณด๋‚ด๋ฉด ๋œ๋‹ค. 

์š”์ฒญ ๋ณ€์ˆ˜ query์— ๊ฒ€์ƒ‰์„ ์›ํ•˜๋Š” ๋ฌธ์ž์—ด์„ ๋‹ด์•„์„œ ์š”์ฒญ์„ ๋ณด๋‚ด๋ฉด ๋œ๋‹ค. ์ถœ๋ ฅ ๊ฒฐ๊ณผ items์— ์ƒํ’ˆ๋“ค์— ๋Œ€ํ•œ ์ •๋ณด๊ฐ€ JSON ํ˜•์‹์œผ๋กœ ์ „๋‹ฌ๋œ๋‹ค. 

์š”์ฒญ์„ ๋ณด๋‚ผ ๋•Œ๋Š” ๋ฐœ๊ธ‰๋ฐ›์€ Client ID์™€ Client Secret ์ •๋ณด๋ฅผ ํ•จ๊ป˜ ๋„˜๊ฒจ์ค˜์•ผ ํ•œ๋‹ค. 

 

 

๐Ÿ’ก API ์ด์šฉ ํ•˜๊ธฐ

์ •๋ง ํŽธ๋ฆฌํ•˜๊ฒŒ๋„ ARC์—์„œ ์š”์ฒญ Method, URL, Header(Client ID, Client Secret ํฌํ•จ) ์ •๋ณด๋ฅผ ์ž…๋ ฅํ•˜๋ฉด ํ•ด๋‹น ์š”์ฒญ์˜ ์Šคํ”„๋ง ์ฝ”๋“œ๋ฅผ ํ™•์ธํ•  ์ˆ˜ ์žˆ๋‹ค. 

RestTemplate rest = new RestTemplate();
HttpHeaders headers = new HttpHeaders();
headers.add("X-Naver-Client-Id", "๋ฐœ๊ธ‰๋ฐ›์€ Client ID");
headers.add("X-Naver-Client-Secret", "๋ฐœ๊ธ‰๋ฐ›์€ Client Secret");
String body = "";

HttpEntity<String> requestEntity = new HttpEntity<String>(body, headers);
ResponseEntity<String> responseEntity = rest.exchange("https://openapi.naver.com/v1/search/shop.json?query=adidas", HttpMethod.GET, requestEntity, String.class);
HttpStatus httpStatus = responseEntity.getStatusCode();
int status = httpStatus.value();
String response = responseEntity.getBody();
System.out.println("Response status: " + status);
System.out.println(response);

return response;

 

์œ„ ์ฝ”๋“œ ์˜ˆ์‹œ๋Š” adidas๋ผ๋Š” ํ‚ค์›Œ๋“œ๋กœ ๊ฒ€์ƒ‰์„ ์š”์ฒญํ•œ ๊ฒฝ์šฐ์ด๋‹ค.

 

 

๐Ÿ’ก Naver Shop Search

์ด์ œ ๊ฒ€์ƒ‰์„ ์ง„ํ–‰ํ•  ํด๋ž˜์Šค๋ฅผ ์ƒ์„ฑํ•˜์„œ ์ฝ”๋“œ๋ฅผ ์ž‘์„ฑํ•ด์•ผ ํ•œ๋‹ค. 

  1. ํ”„๋กœ์ ํŠธ์— utils ํŒจํ‚ค์ง€๋ฅผ ๋งŒ๋“ ๋‹ค. 
  2. utils ํŒจํ‚ค์ง€ ์•ˆ์— NaverShopSearch ํด๋ž˜์Šค๋ฅผ ์ƒ์„ฑํ•œ๋‹ค. 
  3. public์œผ๋กœ search ํ•จ์ˆ˜๋ฅผ ๋งŒ๋“ค๊ณ  ์œ„ ์ฝ”๋“œ๋ฅผ ๋ถ™์—ฌ๋„ฃ๋Š”๋‹ค. 

 

์•„์ง ๋ถ€์กฑํ•œ๊ฒŒ ์žˆ๋‹ค.๐Ÿค” ๋‚ด๊ฐ€ ์›ํ•˜๋Š” ๊ฒƒ์€ ํ‚ค์›Œ๋“œ๋ฅผ ๋„˜๊ฒจ์ฃผ๋ฉด ํ•ด๋‹น ํ‚ค์›Œ๋“œ์— ๋Œ€ํ•œ ๊ฒ€์ƒ‰์„ ์ง„ํ–‰ํ•˜๋Š” ๊ฒƒ์ด๋‹ค. ๋”ฐ๋ผ์„œ search ํ•จ์ˆ˜์—์„œ ํ‚ค์›Œ๋“œ๋ฅผ ๋ฐ›๋„๋ก ํ•˜๊ณ , responseEntity๋ฅผ ์ƒ์„ฑํ•  ๋•Œ ํ•ด๋‹น ํ‚ค์›Œ๋“œ๋ฅผ query ๊ฐ’์œผ๋กœ ๋„˜๊ฒจ์ฃผ๋„๋ก ์ˆ˜์ •ํ•ด์•ผ ํ•œ๋‹ค. 

public class NaverShopSearch {
    public String search(String query) {
        RestTemplate rest = new RestTemplate();
        HttpHeaders headers = new HttpHeaders();
        headers.add("X-Naver-Client-Id", "๋ฐœ๊ธ‰๋ฐ›์€ Client ID");
        headers.add("X-Naver-Client-Secret", "๋ฐœ๊ธ‰๋ฐ›์€ Client Secret");
        String body = "";

        HttpEntity<String> requestEntity = new HttpEntity<String>(body, headers);
        // ๋„˜๊ฒจ๋ฐ›์€ query๋กœ ๊ฒ€์ƒ‰ ์š”์ฒญ
        ResponseEntity<String> responseEntity = rest.exchange("https://openapi.naver.com/v1/search/shop.json?query=" + query, HttpMethod.GET, requestEntity, String.class);
        HttpStatus httpStatus = responseEntity.getStatusCode();
        int status = httpStatus.value();
        String response = responseEntity.getBody();
        System.out.println("Response status: " + status);
        System.out.println(response);

        return response;
    }
}

 

 

๐Ÿ’ก Item DTO

๋˜๋˜๋˜ ๋ถ€์กฑํ•œ๊ฒŒ ์žˆ๋‹ค.๐Ÿค” ๋‚˜๋Š” ๊ฒ€์ƒ‰ ๊ฒฐ๊ณผ๋ฅผ ๋ฌธ์ž์—ด์ด ์•„๋‹Œ, ItemDto๋กœ ๋ฐ›๊ณ  ์‹ถ๋‹ค. ItemDto๋Š” ๊ฒ€์ƒ‰ ๊ฒฐ๊ณผ๋กœ ์˜ค๋Š” ์ƒํ’ˆ๋“ค์— ๋Œ€ํ•œ ๋ฐ์ดํ„ฐ๋ฅผ ๋ฌผ๊ณ  ๋‹ค๋‹ˆ๋Š” ๊ฐ์ฒด์ด๋‹ค. ItemDto ํด๋ž˜์Šค๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค. 

@Getter
public class ItemDto {
    private String title;
    private String link;
    private String image;
    private int lprice;

    public ItemDto(JSONObject itemJson) {
        this.title = itemJson.getString("title");
        this.link = itemJson.getString("link");
        this.image = itemJson.getString("image");
        this.lprice = itemJson.getInt("lprice");
    }
}

 

 

response๋ฅผ ItemDto๋กœ ๋ฐ”๊พธ๊ธฐ ์œ„ํ•ด์„  ๋‹ค์Œ ๊ณผ์ •์ด ํ•„์š”ํ•˜๋‹ค. 

  1. ๋ฌธ์ž์—ด ์ •๋ณด๋ฅผ JSONObject๋กœ ๋ฐ”๊พธ๊ธฐ
  2. JSONObject์—์„œ items ๋ฐฐ์—ด ๊บผ๋‚ด๊ธฐ
  3. items ๋ฐฐ์—ด์ด ๋‹ด๊ธด JSONArray์˜ ์›์†Œ๋ฅผ ํ•˜๋‚˜์”ฉ JSONObject๋กœ ๋ฐ›๊ธฐ
  4. ๋ฐ›์€ JSONObject๋กœ ItemDto ์ƒ์„ฑ ํ•˜๊ธฐ

JSONObject์™€ JSONArray๋ฅผ ์ฒ˜๋ฆฌํ•˜๊ธฐ ์œ„ํ•ด https://mvnrepository.com/์—์„œ JSON In Java ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ gradle์˜ dependencies์— ์ถ”๊ฐ€ํ•ด์•ผ ํ•œ๋‹ค. 

 

๊ทธ ๋‹ค์Œ, NaverShopSearch์— fromJSONtoItems ํ•จ์ˆ˜๋ฅผ ์ถ”๊ฐ€ํ•œ๋‹ค. 

public List<ItemDto> fromJSONtoItems(String result) {
    JSONObject rjson = new JSONObject(result);
    JSONArray items  = rjson.getJSONArray("items");
    List<ItemDto> ret = new ArrayList<>();
    for (int i=0; i<items.length(); i++) {
        JSONObject itemJson = items.getJSONObject(i);
        ItemDto itemDto = new ItemDto(itemJson);
        ret.add(itemDto);
    }
    return ret;
}

 

 

 

๐Ÿ’ก Search Request Controller

๋˜๋˜๋˜๋˜๋˜ ๋ถ€์กฑํ•œ ์ ์ด ์žˆ๋‹ค.๐Ÿค” NaverShopSearch๋ฅผ ์‚ฌ์šฉํ•˜๊ธฐ ์œ„ํ•ด ๋งค์šฐ ๋งค์šฐ ์ค‘์š”ํ•œ ์ž‘์—…์ด ๋‚จ์•˜๋‹ค. ํ”„๋กœ์ ํŠธ ์š”๊ตฌ ์กฐ๊ฑด์€ ๋‹ค์Œ๊ณผ ๊ฐ™๋‹ค.

  1. ์‚ฌ์šฉ์ž๊ฐ€ ๊ฒ€์ƒ‰์–ด๋ฅผ ์ž…๋ ฅํ•˜๋ฉด ์ปจํŠธ๋กค๋Ÿฌ๊ฐ€ ๊ทธ๊ฒƒ์„ ์ „๋‹ฌ ๋ฐ›๋Š”๋‹ค. 
  2. ์ „๋‹ฌ๋ฐ›์€ ๊ฒ€์ƒ‰์–ด๋กœ ๋„ค์ด๋ฒ„ API์— ์š”์ฒญํ•˜๊ณ , ๊ทธ ๊ฒฐ๊ณผ๋ฅผ ์‚ฌ์šฉ์ž์—๊ฒŒ ์‘๋‹ตํ•œ๋‹ค. 

๋”ฐ๋ผ์„œ Controller๊ฐ€ NaverShopSearch๋ฅผ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์–ด์•ผ ํ•œ๋‹ค. ์‚ฌ์šฉ์ž๊ฐ€ ์š”์ฒญ์„ ํ•˜๋ฉด NaverShopSearch ํด๋ž˜์Šค๋ฅผ ์ž๋™์œผ๋กœ ๋งŒ๋“ค์–ด ์ฃผ๋ฉด ์ •๋ง ์ •๋ง ์ข‹์„ ํ…๋ฐ... ์ด๊ฒŒ ์Šคํ”„๋ง์—์„œ๋Š” ๊ฐ€๋Šฅํ•˜๋‹ค..! ์Šคํ”„๋ง์ด ์ž๋™์œผ๋กœ ํ•„์š”ํ•œ ํด๋ž˜์Šค๋ฅผ ํ•„์š”ํ•œ ๊ณณ์— ์ƒ์„ฑํ•˜๋„๋ก ํ•˜๋ ค๋ฉด "๋ชฉ๋ก"์— ํ•ด๋‹น ํด๋ž˜์Šค๋ฅผ ๋„ฃ์–ด์ฃผ๋ฉด ๋œ๋‹ค. ๊ทธ ๋ชฉ๋ก์— ๋“ฑ๋กํ•˜๋Š” ๊ฐ„๋‹จํ•œ ๋ฐฉ๋ฒ•์ด ๋ฐ”๋กœ ์ปดํฌ๋„ŒํŠธ ๋“ฑ๋ก์ด๋‹ค. 

 

ํด๋ž˜์Šค ์•ž์— @Component๋ฅผ ๋ถ™์—ฌ์ฃผ๊ธฐ๋งŒ ํ•˜๋ฉด ๋œ๋‹ค. 

@Component // @RequiredArgsConstructor ์™€ ํ•จ๊ป˜ ์‚ฌ์šฉํ•  ๊ฒฝ์šฐ ์Šคํ”„๋ง์ด ์ž๋™์œผ๋กœ ์ƒ์„ฑ
public class NaverShopSearch {

 

๋”๋ณด๊ธฐ

๊ฐ‘์ž๊ธฐ ๋“  ์˜๋ฌธ,, ๊ทธ๋Ÿผ ์ง€๊ธˆ๊นŒ์ง€ ์ผ๋˜ ์„œ๋น„์Šค๋‚˜ ๋ฆฌํฌ์ง€ํ† ๋ฆฌ๋Š” ์–ด๋–ป๊ฒŒ ๊ฐ€์ ธ๋‹ค ์“ด๊ฑฐ์ง€,,? ๋ถ„๋ช… Controller์—๋งŒ @RequiredArgsConstructor๋ฅผ ์ถ”๊ฐ€ํ•˜๊ณ , ์„œ๋น„์Šค๋‚˜ ๋ฆฌํฌ์ง€ํ† ๋ฆฌ์—๋Š” ์ปดํฌ๋„ŒํŠธ ๋“ฑ๋ก์„ ์•ˆ ํ•ด์คฌ๋Š”๋ฐ,,๋ผ๊ณ  ์ƒ๊ฐํ–ˆ๋˜ ๋‚ด๊ฐ€ ๋ฐ”๋ณด์˜€๋‹ค~~

https://namocom.tistory.com/421 ๊ธ€์„ ์ฐธ๊ณ ํ•˜์ž๋ฉด, @Component์˜ ๊ตฌ์ฒดํ™”๋œ ํ˜•ํƒœ๋กœ @Repository, @Service, @Controller๊ฐ€ ์žˆ๋Š” ๊ฒƒ์ด๋‹ค. 

 

๊ทธ๋ ‡๋‹ค๋ฉด ์ด์ œ ๋‚จ์€ ๊ฒƒ์€ ์‚ฌ์šฉ์ž์˜ ์š”์ฒญ์„ ๋ฐ›์„ Controller๋ฅผ ๋งŒ๋“œ๋Š” ๊ฒƒ์ด๋‹ค. ๊ฒ€์ƒ‰ ํ•จ์ˆ˜๋ฅผ ๋‹ค ๋งŒ๋“ค์—ˆ๊ธฐ ๋•Œ๋ฌธ์— GetMapping์—์„œ ์‚ฌ์šฉ๋งŒ ํ•˜๋ฉด๋˜๋ฏ€๋กœ ๊ฐ„๋‹จํ•˜๋‹ค. 

@RequiredArgsConstructor // final ๋กœ ์„ ์–ธ๋œ ํด๋ž˜์Šค๋ฅผ ์ž๋™์œผ๋กœ ์ƒ์„ฑ
@RestController // JSON์œผ๋กœ ์‘๋‹ตํ•จ์„ ์„ ์–ธ
public class SearchRequestController {

    private final NaverShopSearch naverShopSearch;

    @GetMapping("/api/search")
    public List<ItemDto> getItems(@RequestParam String query) {
        String resultString = naverShopSearch.search(query);
        return naverShopSearch.fromJSONtoItems(resultString);
    }
}

 

์ด๋กœ์จ <๋‚˜๋งŒ์˜ ์…€๋ ‰์ƒต>์˜ ์ฒซ๋ฒˆ์งธ ์š”๊ตฌ ๊ธฐ๋Šฅ์„ ์™„์„ฑํ–ˆ๋‹ค!!!

  • ์ƒํ’ˆ๋ช…์— ๋”ฐ๋ฅธ ๊ฒ€์ƒ‰
  • ๊ด€์‹ฌ ์ƒํ’ˆ ๋“ฑ๋ก & ์กฐํšŒ
  • ๊ด€์‹ฌ ์ƒํ’ˆ์— ๋Œ€ํ•œ ์ตœ์ €๊ฐ€ ๋“ฑ๋กํ•˜๊ธฐ

 

 

 

 

 

 

์ฐธ๊ณ  ์ž๋ฃŒ: ์ŠคํŒŒ๋ฅดํƒ€์ฝ”๋”ฉํด๋Ÿฝ ์›น๊ฐœ๋ฐœ์˜ ๋ด„, Spring 4์ฃผ์ฐจ ๊ฐ•์˜์ž๋ฃŒ

 

 

 

728x90