Visar inlägg med etikett REST. Visa alla inlägg
Visar inlägg med etikett REST. Visa alla inlägg

onsdag 17 september 2014

RestService with Springboot and Groovy

To test this you have to install GVM follow the instruction for your environment, I use Cygwin and GVM, when you have GVM up and running, install Springboot on GVM:
gvm install springboot

       
//Place this groovy-file in /cygwin/home/your_username
//To kick on the service open cygwin or another terminal and write: 

//spring run your_file_name.groovy
//Now the service is started, open a browser and test the service, f.e: http://localhost:8080/greeting/John
@Grab("org.grails:gorm-hibernate4-spring-boot:1.0.0.RC1") @Grab("com.h2database:h2:1.3.173") import grails.persistence.* import org.springframework.http.* import org.springframework.web.bind.annotation.* import static org.springframework.web.bind.annotation.RequestMethod.* @RestController class Application { @RequestMapping("/greeting") public String greeting(@RequestParam(value="name", required=false, defaultValue="World") String name) { return "greeting "+name } @RequestMapping("/greeting/{name}") public String greeting2(@PathVariable("name") String name) { return "greeting "+name } @RequestMapping(value = '/person/add/{birthDate}/{firstName}/{lastName}', method = GET) ResponseEntity addPerson(@PathVariable("birthDate") String birthDate, @PathVariable("firstName") String firstName, @PathVariable("lastName") String lastName) { Person.withTransaction { def p = new Person(firstName: firstName, lastName:lastName,birthDate:birthDate).save() if(p) { return new ResponseEntity("OK", HttpStatus.CREATED ) } else { return new ResponseEntity("BAD",HttpStatus.BAD_REQUEST) } } } @RequestMapping(value="/person/findByFirstName/{lang}/{value}", method = GET) String findByFirstName(@PathVariable("lang") String lang, @PathVariable("value") String value) { def salutation = getSalutation(lang) def p = Person.findByFirstName(value) return p ? "${salutation} ${p.firstName} ${p.lastName} !" : "Person not found" } private String getSalutation(String lang) { def salutation = 'Hello' if(lang.equals("sv")) { salutation = 'Hej' } else if (lang.equals("it")) { salutation = 'Salve' } return salutation } } @Entity class Person { String firstName String lastName String birthDate }

fredag 14 mars 2014

Groovy HTTPBuilder POST: JIRA API

How to create a Rest client to
       


import groovyx.net.http.HttpResponseDecorator;
import org.apache.http.HttpRequest;
import org.apache.http.protocol.HttpContext;
import org.apache.http.HttpRequestInterceptor;
import org.apache.http.entity.StringEntity;
import groovy.json.JsonSlurper;
import static groovyx.net.http.Method.*
import static groovyx.net.http.ContentType.*


@Grapes([
@Grab(group = 'org.codehaus.groovy.modules.http-builder', 
      module = 'http-builder', version = '0.5.1'),
@GrabExclude('org.codehaus.groovy:groovy')
])


//Your Jira api url
def jiraApiUrl = 'localhost:8989/rest/api/2/'
//Your user and password
def basic = 'Basic ' + 
'user:password'.bytes.encodeBase64().toString()



 //New issue to add 
  
 def msg= '''
{
 
  "fields": {
    
    "project": {
      "id": "10000"
    },
    "summary": "something's test www",
    "issuetype": {
      "id": "10000"
    },
    "assignee": {
      "name": "luigi"
    },
    
  
    "labels": [
      "bugfix",
      "blitz_test"
    ],
    "timetracking": {
      "originalEstimate": "10",
      "remainingEstimate": "5"
    },
   
   
    "environment": "environment",
    "description": "description",
    "duedate": "2011-03-11",  
   
 "customfield_10007": "Epic Name is required."
  }
}
 
'''
  
 def bodyMap= new JsonSlurper().parseText(msg)
 println bodyMap

                                     
//Url and path                                  
def myClient = new groovyx.net.http.HTTPBuilder('http://'+jiraApiUrl+'issue/')

//Proxy to use Fiddler
myClient.setProxy('localhost', 8888, 'http')

results = myClient.request(POST, JSON) { req ->
     
    req.addHeader('Authorization', basic)  
    req.addHeader('Content-Type' ,'application/json')   
    req.addHeader("accept", "application/json");

    requestContentType  = JSON  
    body = bodyMap

    println req.getAllHeaders()

    response.success = { resp, reader ->
        println "SUCCESS! ${resp.statusLine}"
    }

    response.failure = { resp ->
        println "FAILURE! ${resp.properties}"
    }
}

       
 

Groovy RestClient GET; JIRA API

How to create a Rest client to
       

import groovyx.net.http.RESTClient;
import groovyx.net.http.HttpResponseDecorator;
import org.apache.http.HttpRequest;
import org.apache.http.protocol.HttpContext;
import org.apache.http.HttpRequestInterceptor;
import groovy.json.JsonSlurper;
import static groovyx.net.http.Method.*
import static groovyx.net.http.ContentType.*

@Grapes([
@Grab(group = 'org.codehaus.groovy.modules.http-builder', 
      module = 'http-builder', version = '0.5.1'),
@GrabExclude('org.codehaus.groovy:groovy')
])



def jiraApiUrl = 'http://localhost:8989/rest/api/2/'
def jiraClient = new RESTClient(jiraApiUrl)
def basic = 'Basic ' + 
'user:password'.bytes.encodeBase64().toString()
jiraClient.client.addRequestInterceptor(
  new HttpRequestInterceptor() {
    void process(HttpRequest httpRequest, 
                 HttpContext httpContext) {
      httpRequest.addHeader('Authorization', basic)
  }
})

def serverInfo = jiraClient.get(path: 'issue/SPACE-4')
println serverInfo.data

def slurp = 
  new JsonSlurper().parseText(serverInfo.data.toString())
  
  println slurp.fields.description