Closed yuujinleee closed 3 years ago
๋ด๊ฐ ์์ฃผ ์ฐ๋ ๊ฑฐ
Postman (sw) - GET test ์ฝ๊ฒ ํ๊ธฐ
์ฝ๋ ์ง๋ฉด์ ๋ดค๋ ๊ฑฐ ์ค ์ค์ํ ๋งํฌ๋ค
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/.
์คํ๋ง ๋ถํธ 2์์ JUnit 5๋ฅผ ์ฌ์ฉํ๋ ๋ฐฉ๋ฒ์ ์ ๋ฆฌํ๋ค. ๋จผ์ pom.xml ํ์ผ์ ๋ค์๊ณผ ๊ฐ์ด ์์ ํ๋ค.
The Spring @Controller and @RestController Annotations https://www.baeldung.com/spring-controller-vs-restcontroller @RestController ๋ @Controller and @ResponseBody ๋ ๊ฐ ํฉ์ณ์ ธ ์๋ annotation. ๋ ํธ๋ฆฌ.
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
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/
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
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
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
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
๊ฐ์ ๋ฆฌ์คํธ ๋ฐ๋ณตํด์ ๋์ค๋ ๋ฌธ์ ํฌํจ
public class milestone2 {
// Object
private static ArrayList<Classified_by_vote> classified_table = null;
// Setter
public static ArrayList<Classified_by_vote> make_classified_table(ArrayList<Movie_data_node> movie_rating_matrix, double C, int m) throws IOException{
ArrayList<Classified_by_vote> classified_table = new ArrayList<>();
~~~
milestone2.classified_table = classified_table; //์ฌ๊ธฐ ๋ผ์ธ์ด ์ค์, setter ์ญํ ํจ
return classified_table;
}
// Getter
public String get_classified_table(int index){
~~~
}
URL
GET /oauth/authorize?client_id={REST_API_KEY}&redirect_uri={REDIRECT_URI}&response_type=code HTTP/1.1
Host: kauth.kakao.com
https://pjh3749.tistory.com/273
์ฑ๊ณต/์คํจ์ ๋ํ HTTP Status Code ๋ฅผ ํด๋ผ์ด์ธํธ๋ก ๋ณด๋ด์ฃผ๋ฉด์ ํด๋น ๋ฉ์์ง๋ ๊ฐ์ด ๋ณด๋ด์ฃผ๋ฉด ๋ ๊ฒ ๊ฐ๋ค์. Spring์ ์ฌ์ฉํ์ ๋ค๋ฉด @ControllerAdvice๋ฅผ ์ด์ฉํ Exception Handling์ ํ์๋ฉด ๋ ๊ฒ ๊ฐ์ต๋๋ค. ํด๋น ํธ๋ค๋ฌ๋ ResponseEntityExceptionHandler ๋ฅผ ์์๋ฐ์ ์ฌ์ฉํ์๋ฉด ๋ ๊ฑฐ์์.
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
ํด๋ผ์ด์ธํธ๋จ ๋ฐ์ดํฐ ์ค๋ฅ => 400
// 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."
]
}
1 - requestbody ์ชฝ error handling
2 ~ 6 - (์๋ ์ฌ์ง ์ฐธ์กฐ)
7 - ์ฒ๋ฆฌํ ํ์ X. (args 3๊ฐ์ธ ์ผ์ด์ค๋ก fall) 8~9 - ์ฒ๋ฆฌํ ํ์ X. ์ ์ด์ ํด๋นํ๋ ์ธํ set์ด ์๋ ๋ฐ ํน์ ๋ชจ๋ฅผ fallback ๋๋ฌธ์ ๋ฃ์๋ ์ค๋ฅ๋ผ.
args.length๊ฐ 1๋๋ 2๊ฐ ์๋๋ฉด ์๋ฌ
์๋ฒ์์ ๋ฌด์กฐ๊ฑด String [2] ๋ก java์ ๋ณด๋ด์ค
args.length 1์ธ ๊ฒฝ์ฐ ์
๋ ฅ์ -> "Toy Story" "10" (๋ํดํธ) ๋ก ๋ณด๋ด์ค.
limit ์ ๋ ฅ์ด 0์ด๊ฑฐ๋ ์ซ์๊ฐ ์๋๋ ์๋ฌ - check_age_validity ์๋ฒ์ฒ๋ฆฌํ์
Title์ด ์ ๋๋ก ์ ๋ ฅ ์๋๋ฉด ์๋ฌ - check_title_validity ์๋ฒ์ฒ๋ฆฌํ์ Title ์ด ๊ณต๋ฐฑ์ผ ๊ฒฝ์ฐ -> ์ ๋ผ!
NoDB Error - ์ผ์ด๋ ์ผ X
Toy Story (1995) ๊ฐ๋ฅ ToyStory(1995) ๊ฐ๋ฅ toy stoRy (1995) ๊ฐ๋ฅ Toy Story ๋ถ๊ฐ๋ฅ
์ํ ํ๊ฐ ์๊ฐ 20๊ฐ ์ด์์ด๋ฉด if๋ฌธ ์๋๋ฉด else๋ฌธ
๐ ์ ์ง์ด์ ๋ ธํธ
์๋ฃ์ฌํญ โ
์ฅ๋ฅด์กฐํฉ๋ณ ์ํ ๊ฐ์ ์์ ํ์ผ ์ฌ๋ ค๋์ (![image](https://user-images.githubusercontent.com/38070937/118489016-4e61c480-b757-11eb-8087-625f437e957e.png)
scripts/movie_num_per_genre.xlsx
)pom.xml , gitignore ์์
milestone 2 junit ํ ์คํธ๋ ํ์ฌ ์ฃผ์์ฒ๋ฆฌ (๋๋ฌด ์ค๋๊ฑธ๋ ค์..)
RestAPI ์ด๊ธฐ ์ธํ ์๋ฃ
PART 1
PART 2
ํ์ฌ ์งํ ์ค โโโโโโ
1์์
Part 1 ์๋ฃPart 2์๋ฃjunit testing
Readme ์์ฑ
๊ทธ ์ธ ์ ๊ฒ ์ฌํญ (์ ์ค์)
./mvnw spring-boot:run
) run.sh ์ ํฌํจ?maven dependancy ์์ org.json ์์ฐ์ด๋ฉด ์ญ์ ํ๊ธฐ(์ญ์ ์๋ฃ)Notes
2.0.2.RELEASE
5.0.6.RELEASE
8.5.31
2.12.0
2.4.5
๊ฐ Junit 5 (Junit Jupiter)์ ํธํ์ด ์ ์๋๋ ๋ฏ. (์๋ ์ฐธ๊ณ 2 ๋งํฌ) ํ์ฌ pom.xml ์๋2.0.2.RELEASE
๋ฒ์ .๋ง์ฝ ์์ ๋ฒ์ ์ด๋ JUnit 5๋ ํธํ ๋๋ฉด ์ ๊ธํ๊ฒ์ด.<- ๋ถ๊ฐ๋ฅ