sirin05137 / CSE364_Project

2 stars 0 forks source link

RestAPI Integration #33

Closed yuujinleee closed 3 years ago

yuujinleee commented 3 years ago

๐Ÿ““ ์œ ์ง„์ด์˜ ๋…ธํŠธ

yujin_restapi branch ์—์„œ ํ˜„์žฌ ์ž‘์—… ์ค‘ ...

์™„๋ฃŒ์‚ฌํ•ญ โœ…


ํ˜„์žฌ ์ง„ํ–‰ ์ค‘ โ—โ—โ—โ—โ—โ—

1์ˆœ์œ„

๊ทธ ์™ธ ์ ๊ฒ€ ์‚ฌํ•ญ (์•ˆ ์ค‘์š”)

yuujinleee commented 3 years ago

Useful links for RESTapi development ๐Ÿ”—

๋‚ด๊ฐ€ ์ž์ฃผ ์“ฐ๋Š” ๊ฑฐ

Postman (sw) - GET test ์‰ฝ๊ฒŒ ํ•˜๊ธฐ

  1. Instantly parse JSON into any language - https://app.quicktype.io/
  2. URL decoder/encoder - https://meyerweb.com/eric/tools/dencoder/

    Helpful Documentations

    ์ฝ”๋“œ ์งœ๋ฉด์„œ ๋ดค๋˜ ๊ฑฐ ์ค‘ ์ค‘์š”ํ•œ ๋งํฌ๋“ค

  3. Instruction ์— ์žˆ๋Š” ๋งํฌ

A good reference to get familiar with Spring and REST APIs can be found at https://spring.io/guides/gs/rest-service and https://spring.io/guides/tutorials/rest/.

  1. Spring Boot 2 && Junit 5 (Jupiter) ์…‹์—…

์Šคํ”„๋ง ๋ถ€ํŠธ 2์—์„œ JUnit 5๋ฅผ ์‚ฌ์šฉํ•˜๋Š” ๋ฐฉ๋ฒ•์„ ์ •๋ฆฌํ•œ๋‹ค. ๋จผ์ € pom.xml ํŒŒ์ผ์„ ๋‹ค์Œ๊ณผ ๊ฐ™์ด ์ˆ˜์ •ํ•œ๋‹ค.

  1. RESTful API ์„ค๊ณ„ ๊ฐ€์ด๋“œ

  2. The Spring @Controller and @RestController Annotations https://www.baeldung.com/spring-controller-vs-restcontroller @RestController ๋Š” @Controller and @ResponseBody ๋‘ ๊ฐœ ํ•ฉ์ณ์ ธ ์žˆ๋Š” annotation. ๋” ํŽธ๋ฆฌ.

  3. Spring @RequestParam Annotation ์‚ฌ์šฉ๋ฒ• https://www.baeldung.com/spring-request-param

    // required value ์ง€์ • <- Required String parameter '์ธ์ž' is not present error ๋ฐฉ์ง€ ?
    // Parameter ๊ฐ’์„ ๋ฐ›์•„์˜ฌ ๋•Œ null์ด๊ฑฐ๋‚˜ Type์ด ๋งž์ง€ ์•Š๋Š” ๊ฒฝ์šฐ 400 ์—๋Ÿฌ ๋ฐœ์ƒ
    @GetMapping("/api/foos")
    @ResponseBody
    public String getFoos(@RequestParam(required = false) String id) { 
    return "ID: " + id;
    }
    // ๋””ํดํŠธ value
    @RequestParam(defaultValue = "test") String id
  4. Serialization & Deserialization ๊ด€๋ จ library ๋น„๊ต (Jackson vs. Gson vs. JSON-B vs. JSON-P vs. org.JSON vs. Jsonpath)

    Json (string) -> Object : Serialization //// Object -> Json (string) : Deserialization ๋‚˜๋Š” ์•„๋ž˜ ์ž๋ฃŒ ์ฐธ์กฐํ•ด์„œ jackson ์œผ๋กœ ์‚ฌ์šฉ ์ค‘

Maven dependancy - com.fasterxml.jackson.core https://itsallbinary.com/jackson-vs-gson-vs-json-b-vs-json-p-vs-org-json-vs-jsonpath-java-json-libraries-features-comparison/

  1. JSON array export examples How to create a JSON array using Jackson - https://attacomsian.com/blog/jackson-create-json-array jackson-databind - https://github.com/FasterXML/jackson-databind/#get-it

  2. URL encode issue

    //X
    GET /spring-mvc-basics-4/products/get2?product={"id": 1,"name": "Asus Zenbook","price": 800}
    //O
    GET /spring-mvc-basics-4/products/get2?product=%7B%22id%22%3A%201%2C%22name%22%3A%20%22Asus%20Zenbook%22%2C%22price%22%3A%20800%7D
  3. etc The following guides illustrate how to use some features concretely: Building a RESTful Web Service Serving Web Content with Spring MVC Building REST services with Spring

yuujinleee commented 3 years ago

1) To Run spring-boot ,
(in your local)

yujinlee@Yujins-MacBook-Pro CSE364_Project % ./mvnw spring-boot:run

2) To Build maven && Run spring-boot at the same time,

yujinlee@Yujins-MacBook-Pro CSE364_Project % mvn clean install spring-boot:run -e

image

yuujinleee commented 3 years ago

RestAPI ๊ด€๋ จ Piazza ๋‹ต๋ณ€ ๋ชจ์Œ

  1. To process www form url, you need to use @RequestParam annotation instead of @RequestBody. In this milestone, please ignore www form URL input type. So, it won't be wrong even though you get 400 error in a browser.ย 
  2. limit parameter
    • limit parameter optional and integer type. That is, if limit is given from input, the program must take it as an integer. If it is not given at all, then the program takes as a default number 10.ย Changed BB accordingly.ย 
    • Given the size of our dataset (3883 movies, 6040 users, and 1000209 ratings), your program should support at least top 700, which is approximately 20% of total movies assuming that the ratings between 1 and 5 are evenly distributed.
  3. "genres" or "genre" ? we won't deduct points for using "genre".
yuujinleee commented 3 years ago

milestone2.java ์˜ค๋ฅ˜ ํ•ด๊ฒฐ

yuujinleee commented 3 years ago

README ์ž‘์„ฑ์šฉ / ์ด๊ฒƒ์ €๊ฒƒ

์นด์นด์˜ค ๋กœ๊ทธ์ธ REST API - https://developers.kakao.com/docs/latest/ko/kakaologin/rest-api

URL

GET /oauth/authorize?client_id={REST_API_KEY}&redirect_uri={REDIRECT_URI}&response_type=code HTTP/1.1
Host: kauth.kakao.com

image image

yuujinleee commented 3 years ago

https://pjh3749.tistory.com/273

Error handling

์„ฑ๊ณต/์‹คํŒจ์— ๋Œ€ํ•œ HTTP Status Code ๋ฅผ ํด๋ผ์ด์–ธํŠธ๋กœ ๋ณด๋‚ด์ฃผ๋ฉด์„œ ํ•ด๋‹น ๋ฉ”์‹œ์ง€๋„ ๊ฐ™์ด ๋ณด๋‚ด์ฃผ๋ฉด ๋  ๊ฒƒ ๊ฐ™๋„ค์š”. Spring์„ ์‚ฌ์šฉํ•˜์‹ ๋‹ค๋ฉด @ControllerAdvice๋ฅผ ์ด์šฉํ•œ Exception Handling์„ ํ•˜์‹œ๋ฉด ๋  ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค. ํ•ด๋‹น ํ•ธ๋“ค๋Ÿฌ๋Š” ResponseEntityExceptionHandler ๋ฅผ ์ƒ์†๋ฐ›์•„ ์‚ฌ์šฉํ•˜์‹œ๋ฉด ๋  ๊ฑฐ์—์š”.

HTTP ์ƒํƒœ ์ฝ”๋“œ

100-level (์ •๋ณด): ์„œ๋ฒ„๊ฐ€ ์š”์ฒญ์„ ์•Œ์•„ ๋“ค์—ˆ์Šต๋‹ˆ๋‹ค. 200-level(์„ฑ๊ณต): ์„œ๋ฒ„๊ฐ€ ์š”์ฒญ์„ ์˜ˆ์ƒํ•œ๋Œ€๋กœ ์™„๋ฃŒํ–ˆ์Šต๋‹ˆ๋‹ค. 300-level(๋ฆฌ๋‹ค์ด๋ ‰์…˜): ์š”์ฒญ์„ ์™„๋ฃŒํ•˜๋ ค๋ฉด ํด๋ผ์ด์–ธํŠธ์˜ ์ถ”๊ฐ€์ž‘์—…์ด ํ•„์š”ํ•ฉ๋‹ˆ๋‹ค. 400-level(ํด๋ผ์ด์–ธํŠธ ์—๋Ÿฌ): ํด๋ผ์ด์–ธํŠธ๊ฐ€ ์œ ํšจํ•˜์ง€์•Š์€ ์š”์ฒญ์„ ํ–ˆ์Šต๋‹ˆ๋‹ค. 500-level(์„œ๋ฒ„ ์—๋Ÿฌ): ์„œ๋ฒ„๊ฐ€ ์„œ๋ฒ„ ์—๋Ÿฌ ๋•Œ๋ฌธ์— ์œ ํšจํ•œ ์š”์ฒญ์„ ์ˆ˜ํ–‰ํ•˜์ง€ ๋ชปํ–ˆ์Šต๋‹ˆ๋‹ค.

์—๋Ÿฌ ํ•ธ๋“ค๋ง

์—๋Ÿฌ๋ฅผ ์ฒ˜๋ฆฌํ•˜๋Š” ๊ฐ€์žฅ ๊ฐ„๋‹จํ•œ ๋ฐฉ๋ฒ•์€ ์ ์ ˆํ•œ ์‘๋‹ต ์ฝ”๋“œ๋ฅผ ๋‚ด๋ ค์ฃผ๋Š” ๊ฒƒ์ž…๋‹ˆ๋‹ค.

์ž์ฃผ ๋‚˜์˜ค๋Š” ์‘๋‹ต ์ฝ”๋“œ๋“ค์€ ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

400 Bad Request - ํด๋ผ์ด์–ธํŠธ๊ฐ€ ์œ ํšจํ•˜์ง€ ์•Š์€ ์š”์ฒญ์„ ๋ณด๋‚ธ ๊ฒฝ์šฐ (request body๋‚˜ ํŒŒ๋ผ๋ฏธํ„ฐ๋ฅผ ๋นผ๋จน์€ ๊ฒฝ์šฐ๊ฐ™์ด) 401 Unauthorized - ํ•ด๋‹น ์„œ๋ฒ„์— ํด๋ผ์ด์–ธํŠธ ์ธ์ฆ์ด ์‹คํŒจํ•œ ๊ฒฝ์šฐ 403 Forbidden - ํด๋ผ์ด์–ธํŠธ๊ฐ€ ์ธ์ฆ์€ ๋์ง€๋งŒ ์š”์ฒญํ•œ ์ž์›์— ๋Œ€ํ•œ ๊ถŒํ•œ์€ ์—†๋Š” ๊ฒฝ์šฐ (์˜ˆ๋ฅผ ๋“ค์–ด ๋กœ๊ทธ์ธ๋œ ์‚ฌ์šฉ์ž๊ฐ€ ๊ด€๋ฆฌ์ž ํŽ˜์ด์ง€์— ์ ‘๊ทผํ•˜๋Š” ๊ฒฝ์šฐ ๊ฒ ์ฃ ?) 404 Not Found - ์š”์ฒญํ•œ ์ž์›์ด ์กด์žฌํ•˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ 412 Precondition Failed - Request Header ํ•„๋“œ ์ค‘ ํ•œ ๊ฐœ ์ด์ƒ์˜ ๊ฐ’์ด ์ž˜๋ชป ๋œ ๊ฒฝ์šฐ 500 Internal Server Error - ์„œ๋ฒ„์—์„œ ๋ฐœ์ƒ๋œ ์ผ๋ฐ˜์ ์ธ ์—๋Ÿฌ 503 Service Unavailable - ์š”์ฒญ๋œ ์„œ๋น„์Šค๊ฐ€ ์ด์šฉ๊ฐ€๋Šฅํ•˜์ง€ ์•Š๋Š” ๊ฒฝ์šฐ

[1] https://www.baeldung.com/exception-handling-for-rest-with-spring [2] ์˜ค๋ฅ˜ ์‹œ ๋ฉ”์‹œ์ง€ ์ปค์Šคํ„ฐ๋งˆ์ด์ง• http://www.baeldung.com/global-error-handler-in-a-spring-rest-api

yuujinleee commented 3 years ago

ํด๋ผ์ด์–ธํŠธ๋‹จ ๋ฐ์ดํ„ฐ ์˜ค๋ฅ˜ => 400

image

yuujinleee commented 3 years ago

Part 1 Error Handling

// format
{
    "error": "InputInvalidError",
    "message": [
        "Entered gender input (ABCD) is invalid.",
        " Entered age input (EFG) is invalid.",
        " Entered occupation input (HIJK) is invalid.",
        " Entered genre input (LMN|OP) is invalid."
    ]
}

image

1 - requestbody ์ชฝ error handling

occupation ์ด ์—†๊ฑฐ๋‚˜ || ๋‹ค๋ฅธ ๋ฒจ๋ฅ˜ ๋ผ์–ด๋“ค์–ด์™€๋„ ๋””ํดํŠธ ์ž๋ฐ”์ธํ’‹ "" "" "" ๋กœ ์‹คํ–‰

image

2 ~ 6 - (์•„๋ž˜ ์‚ฌ์ง„ ์ฐธ์กฐ)

1๊ฐœ ์˜ค๋ฅ˜

image

์—ฌ๋Ÿฌ๊ฐœ ์˜ค๋ฅ˜

image

7 - ์ฒ˜๋ฆฌํ•  ํ•„์š” X. (args 3๊ฐœ์ธ ์ผ€์ด์Šค๋กœ fall) 8~9 - ์ฒ˜๋ฆฌํ•  ํ•„์š” X. ์• ์ดˆ์— ํ•ด๋‹นํ•˜๋Š” ์ธํ’‹ set์ด ์—†๋Š” ๋ฐ ํ˜น์‹œ ๋ชจ๋ฅผ fallback ๋•Œ๋ฌธ์— ๋„ฃ์—ˆ๋˜ ์˜ค๋ฅ˜๋ผ.

yuujinleee commented 3 years ago

Movie (Part2 / Milestone3.java) ์—๋Ÿฌ ๊ฒฝ์šฐ

  1. args.length๊ฐ€ 1๋˜๋Š” 2๊ฐ€ ์•„๋‹ˆ๋ฉด ์—๋Ÿฌ ์„œ๋ฒ„์—์„œ ๋ฌด์กฐ๊ฑด String [2] ๋กœ java์— ๋ณด๋‚ด์คŒ args.length 1์ธ ๊ฒฝ์šฐ ์ž…๋ ฅ์€ -> "Toy Story" "10" (๋””ํดํŠธ) ๋กœ ๋ณด๋‚ด์คŒ.

  2. limit ์ž…๋ ฅ์ด 0์ด๊ฑฐ๋‚˜ ์ˆซ์ž๊ฐ€ ์•„๋‹๋•Œ ์—๋Ÿฌ - check_age_validity ์„œ๋ฒ„์ฒ˜๋ฆฌํ•„์š”

  3. Title์ด ์ œ๋Œ€๋กœ ์ž…๋ ฅ ์•ˆ๋˜๋ฉด ์—๋Ÿฌ - check_title_validity ์„œ๋ฒ„์ฒ˜๋ฆฌํ•„์š” Title ์ด ๊ณต๋ฐฑ์ผ ๊ฒฝ์šฐ -> ์•ˆ ๋ผ!

  4. NoDB Error - ์ผ์–ด๋‚  ์ผ X

2, 3 ๋งŒ ์ฒ˜๋ฆฌํ•˜๋ฉด ๋Œ .. ?


Toy Story (1995) ๊ฐ€๋Šฅ ToyStory(1995) ๊ฐ€๋Šฅ toy stoRy (1995) ๊ฐ€๋Šฅ Toy Story ๋ถˆ๊ฐ€๋Šฅ

yuujinleee commented 3 years ago

Movie error ์ฒ˜๋ฆฌ ์˜ˆ์‹œ

Valid

image

Invalid title

image

Invalid limit (-5, 0 ๋‘˜๋‹ค ์•„๋ž˜๋กœ ๋‚˜์˜ด)

image

๋‘˜ ๋‹ค Invalid

image

yuujinleee commented 3 years ago

์˜ํ™” ํ‰๊ฐ€ ์ˆ˜๊ฐ€ 20๊ฐœ ์ด์ƒ์ด๋ฉด if๋ฌธ ์•„๋‹ˆ๋ฉด else๋ฌธ