본문 바로가기

Script/Groovy

17. Rest

이번에는 Groovy 에서 제공하는 Rest 기능에 대해 알아보자.
 
 
 
HTTP Verbs
 
 
 
 
Request And Response
 
 
 
 
HTTP Status Code
 
 
 
 
 
 
 
 
 
 
 
 
HTTP Builder
 
HTTP Builder 가 있으며 Groovy 에선 이 클래스를 이용하면 쉽게 Rest Call 을 할 수 있다.
 
아래 웹 사이트는 Rest Call 테스트를 위한 페이지를 제공한다. 
 
 
이를 HTTP Builder 에서 제공해 주는 다양한 API 와 함께 테스트 해 볼수 있다.
 
 
관련 Dependency 는 아래 Repository 를 이용하면 된다.
 
 
예를 들어 아래와 같이 Get 메서드를 테스트 해 보았다.
 
import groovyx.net.http.ContentType
import groovyx.net.http.RESTClient
 
@Grapes(
@Grab(group='org.codehaus.groovy.modules.http-builder', module='http-builder', version='0.7')
)
 
String base = 'http://api.icndb.com'
 
def chuck = new RESTClient(base)
chuck.contentType = ContentType.JSON
 
chuck.get(path:'/jokes/random') { response, json ->
  println response.status
  println json
}
 
def params = [firstName:"Dan", lastname:"Vega"]
chuck.get(path:'/jokes/random', query: params) { response, json ->
  println response.status
  println json
}
200
[type:success, value:[id:606, joke:Chuck Norris once pissed in a gas tank of a semi truck as a joke - that truck is now know as Optimus Prime., categories:[]]]
 
200
[type:success, value:[id:217, joke:Crime does not pay - unless you are an undertaker following Walker, Texas Ranger, on a routine patrol., categories:[]]]
 
 
관련해 필요시 좀 더 확인이 필요하다.
 
 
 
 
 
 
 
 
 
 
 
 
 
 

'Script > Groovy' 카테고리의 다른 글

16. Builder  (0) 2020.01.21
15. MOP - Compile Time  (0) 2020.01.21
14. MOP - Runtime  (0) 2020.01.21
13. OOP  (0) 2020.01.21
12. Exception  (0) 2020.01.21