Chapter Chapter 신한대학교 2015 봄학기 클라이언트/서버 프로그래밍 고덕윤 maniara.k@gmail.com .
RESTful Web Service
더 나은 웹의 세상으로.
API
• Application Programming Interface
• 라이브러리가 제공하는 기능을 쓸 수 있게 만든 인터페이스.
01
println(“Hello World”); System.println(String) 화면에 인자를 출력하는 코드 API 호출Web 에서도 API를 써보자
• 웹 기반 애플리케이션에서 서비스를 제공하기 위한 명세
• 버스 도착 알림 API
• 구글 지도 API
02
해당지역의 날씨를 제공해주는 서비스 Web API 모바일 클라이언트 웹 클라이언트 모바일 웹 클라이언트 웨어러블 클라이언트 기타 IoT 클라이언트 요청 응답Servlet의 Web API
• Servlet 으로 학생정보 관리 url을 만들면
• 쓰기/읽기/수정/삭제(CRUD) 당 각각 하나의 servlet을 제작
• web.xml에 각 servlet에 관한 url을 매핑
• 인자(parameter)가 필요하면 별도의 url을 제작
• http://hostname/service/update.jsp?model=A&id=100
• 이렇게 된다면 얼마나 좋을까?
• 하나의 서블릿 안에서 CRUD를 모두 정의할 수 있다면?
• 호출 url로 간단히 인자를 보낼 수 있다면
• DELETE http://hostname/service/A/100/
• 클라이언트의 객체를 한꺼번에 보낼 수 있다면?
• Student s = new Student(이름,학번,……)
• PUT http://xxx.com/student/s
• 다른 사람도 쓸 수 있는 web api로 서비스를 해보자(open API)
REST란 무엇인가?
• Representational State Transfer : 표현 가능한 상태 전송
• URI기반으로 자원에 접근하는 기술
• Resource 자체가 URI로 접근
• URI의 구성
• 문서/디렉토리/컨트롤러/파라메터
• 일반적 servlet의 get/post와는 다른 의미로 사용함
• GET : 조회(read)
• POST : 생성(create)
• PUT : 교체(update)
• DELETE : 삭제(delete)
04
REST 사용 예
05
@Path("/UserAccount")
public class UserAccountService {
}
@POST
public Response createUser(UserAccount account)
{…} @GET
@Path("/{accountKey}")
public UserAccount getUserAccount(@PathParam("accountKey") String accountKey)
{…} @PUT
public Response updateUserAccount(UserAccount inputAccount)
{…} @DELETE
@Path("/{accountKey}")
public Response deleteUserAccount(@PathParam("accountKey") String accountKey)
{…}
POST http://xxx.appsspot.com/UserAccount
GET http://xxx.appsspot.com/UserAccount/12345
PUT http://xxx.appsspot.com/UserAccount
JSON의 사용
• JavaScript Object Notation
• 경량형 DATA 교환방식
• XML의 무거운 부가정보의 한계를 극복하기 위해 등장
• REST에서 객체를 주고 받는데 사용
JSON사용의 예
07
[JAVA Object] key : 1535 account:abc@abc.com nicName:Batman password:P@ssw0rd {“account”:”abc@abc.com”, “nicName”:”Batman”, “Password”:”P@ssw0rd”} Object to JSON [C# Object] key : 1535 account:abc@abc.com nicName:Batman password:P@ssw0rd JSON to Object[Servlet JAVA Object] key : 1535 account:abc@abc.com nicName:Batman password:P@ssw0rd JSON Obj. Obj.