티스토리 뷰
REST API 써서 포트폴리오도 만들어보고, 여기저기 구글링하며 많이 접해본 그 'REST'
하지만 막상 'RESTful이 뭐야?' 라고 물으면 말할 수 없는 비밀이 되버린다.
말할 수 없다면 안써본거나 진배없기에 정리를 해본다.
Learn REST
* 소프트웨어 아키텍처 : 간단히 말하면 설계
(아키텍처가 필요한 이유, 역할 등 조금 더 자세한 정보는 여기서 확인 가능)
▶ 인터넷 콘텐츠를 소비할 수 있는 장비나 프로그램이 다양해지면서(안드로이드, IOS, 크롬, 익스플로러 등등) 서버와 클라이언트가 1:N의 관계가 됨.
따라서 하나의 서버로 여러 클라이언트를 대응할 수 있도록 할 수 있는 아키텍처가 바로 REST
▶ RESTful하다 혹은 RESTful 형식이라고 하는 것들은 REST 규칙에 어긋나지 않도록 만들어진 것들을 일컬음.
* 정의 / 특징 / 규칙 등은 https://meetup.toast.com/posts/92 여기 잘 나와있음.
정의, 특징, 규칙을 다 봐도 여전히 뜬구름만 잡는 느낌이 든다.
그나마 좀 많이 해본게 Spring이라 관련된 예제를 찾아보니 스프링 공홈에 떡하니 예제가 있다!
RESTful Web Service Example
(Maven이나 Gradle 관련 설정은 스프링 사이트 참고)
src/main/java/com/hello/Greeting.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 | package com.hello; public class Greeting { private final long id; private final String content; public Greeting(long id, String content) { this.id = id; this.content = content; } public long getId() { return id; } public String getContent() { return content; } } | cs |
* Spring Boot 에는 Jackson이 있어서 HTTP의 요청에 자동으로 JSON형식으로 Response.
src/main/java/hello/GreetingController.java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | package com.hello; import java.util.concurrent.atomic.AtomicLong; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestParam; import org.springframework.web.bind.annotation.RestController; @RestController public class GreetingController { private static final String template = "Hello, %s!"; private final AtomicLong counter = new AtomicLong(); @RequestMapping("/greeting") public Greeting greeting(@RequestParam(value="name", defaultValue="World") String name) { return new Greeting(counter.incrementAndGet(), String.format(template, name)); } @RequestMapping("/") String home() { return "Hello!"; } } | cs |
* @RestController : @Controller + @ResponseBody
* AtomicLong
Atomic Operation은 중단되지 않는 연산, 즉, 두 개의 쓰레드에서 동시에 요청이 들어와도 요청이 정지되지 않도록 해주는 것(?)이다.
관련해서 조금 더 깊게 알아보고 싶다면 http://parkcheolu.tistory.com/33 여기를 참고하자
여기서 써준 이유는 AtomiceLong에 있는 incrementAndGet() 메소드를 활용하기 위해서 쓴듯
이 메소드는 요청이 올 때마다 +1이 된다. (DB에서 Auto_increment 시퀀스랑 비슷한 기능)
src/main/java/hello/Application.java
1 2 3 4 5 6 7 8 9 10 11 12 13 | package com; import org.springframework.boot.SpringApplication; import org.springframework.boot.autoconfigure.SpringBootApplication; @SpringBootApplication public class ResTfulTestApplication { public static void main(String[] args) { SpringApplication.run(ResTfulTestApplication.class, args); } } | cs |
이제 http://localhost:8080/greeting 로 접근해보면
{"id":1,"content":"Hello, World!"}
JSON 형식으로 출력되는 것을 확인할 수 있다.
http://localhost:8080/greeting?name=jay 이름을 GET방식으로 전송해도
{"id":2,"content":"Hello, jay!"}
여전히 잘 나온다.
스프링을 활용해 아주 간단한 RESTful WEB Service가 완성되었다!
conclusion
REST를 계기로 내가 제대로 아는 것이 많이 없겠다는 생각이 들었다.
구글링해서 사용은 했지만 그저 쓰기만 했던 것들을 돌아보며 개념과 원리 파악 좀 해나가야겠다.
Reference
https://meetup.toast.com/posts/92
https://nesoy.github.io/articles/2017-02/REST
- Total
- Today
- Yesterday
- 124 나라의 숫자
- 문자열 내 마음대로 정렬하기
- 쇠막대기
- 객체지향과 디자인패턴
- cgv
- AWS
- 스프링 부트
- 최솟값
- K번째수
- 알고리즘
- 맵 api
- codility
- 완주하지 못한 선수
- FrogJmp
- 카카오인턴
- 텔레그램
- 안드로이드 스튜디오
- 기능개발
- 자바
- 파이팅코리아
- json
- 다음 맵 api
- 프로그래머스
- API
- 타겟 넘버
- java
- 더 나은 내일
- Spring
- 예매 알림
- 노션트렐로광고X
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | ||||||
2 | 3 | 4 | 5 | 6 | 7 | 8 |
9 | 10 | 11 | 12 | 13 | 14 | 15 |
16 | 17 | 18 | 19 | 20 | 21 | 22 |
23 | 24 | 25 | 26 | 27 | 28 |