• Stars
    star
    65
  • Rank 456,646 (Top 10 %)
  • Language
    Groovy
  • License
    Apache License 2.0
  • Created about 12 years ago
  • Updated almost 6 years ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

REST client plugin that uses Spring's RestTemplate

Build Status

Rest Client Builder Grails Plugin

Notice

The code for this plugin has moved to a subproject of the Grails Data. Please submit any pull requests there.

Installation

Edit BuildConfig.groovy and add the following dependency:

compile ":rest-client-builder:2.0.0"

For Grails 3.x this plugin is no longer necessary and you should instead just declare a dependency on the core Grails Data library:

 compile 'org.grails:grails-datastore-rest-client'

Basic Usage

For API-style documentation refer to the Groovydocs

The main entry point is the grails.plugins.rest.client.RestBuilder class. Construct and use one of the REST "verbs".

A GET request:

 def resp = rest.get("http://grails.org/api/v1.0/plugin/acegi/")

The response is a Spring ResponseEntity.

There are convenience methods for obtaining JSON:

  resp.json instanceof JSONObject
  resp.json.name == 'acegi'

And XML:

  resp.xml instanceof GPathResult
  resp.xml.name == 'acegi'

POST and PUT requests

POST and PUT requests can be issued with the post and put methods respectively:

        def resp = rest.put("http://repo.grails.org/grails/api/security/groups/test-group"){
            auth System.getProperty("artifactory.user"), System.getProperty("artifactory.pass")
            contentType "application/vnd.org.jfrog.artifactory.security.Group+json"
            json {
                name = "test-group"
                description = "A temporary test group"
            }
        }

In the example above the auth method performs HTTP basic auth, the contentType method sets the content type, and the json method constructs a JSON body.

A POST with URL parameters

        def resp = rest.post('http://someServer/foo/bar?username={username}&password={password}') {
            urlVariables [username:"someDude", password:"abc#123"]
        }

Please note the above url is not a GString with String interpolation but a String that uses the format/convention specified in the RestTemplate javadoc.

http://docs.spring.io/spring/docs/3.2.x/javadoc-api/org/springframework/web/client/RestTemplate.html

Exchanging JSON and XML content

As demonstrated in the previous example you can send JSON data using the json method which accepts the same syntax as Grails' JSONBuilder. Alternatively you can provide a String, or any other valid Java object that is convertable to JSON. The following are also valid examples:

        def resp = rest.put("http://repo.grails.org/grails/api/security/groups/test-group"){
            ...
            contentType "application/vnd.org.jfrog.artifactory.security.Group+json"
            json name: "test-group", description: "A temporary test group"
        }

        def resp = rest.put("http://repo.grails.org/grails/api/security/groups/test-group"){
            ...
            contentType "application/vnd.org.jfrog.artifactory.security.Group+json"
            json '{ name: "test-group", description: "A temporary test group" }'
        }

If you don't explicitly set the contentType like is done above then the default content type for the json method is "application/json".

XML is very similar, there is an xml method that takes a closure to build XML, and object which is convertable to XML or a String of XML.

The response object has xml or json properties for reading the response of the request as demonstratd in the "Basic Usage" section.

Sending custom headers

You can include in your request any additional header:

def resp = rest.put("http://example.org/api/login") {
    header 'X-Auth-Token', '1a2b3c4d5e6f7g8h'
}

Testing

Testing can be done with Spring's RestTemplate mocking APIs. See the tests for RestBuilder itself for an example.

Multipart Requests

Multipart requests are possible by setting properties of the request body to File, URL, byte[] or InputStream instances:

    def resp = rest.post(url) {
        contentType "multipart/form-data"
        zip = new File(pluginPackage)
        pom = new File(pomFile)
        xml = new File(pluginXmlFile)
    }

Connection/Proxy Configuration

Connection and proxy configuration can be specified in the constructor to RestBuilder:

def rest = new RestBuilder(connectTimeout:1000, readTimeout:20000, proxy:['localhost':8888])

The proxy setting can either be map containing the key for the host name and a value for the port or an instance of java.net.Proxy.

More Repositories

1

grails-resources

Resources framework for Grails 2.0
Groovy
60
star
2

grails-rabbitmq

Grails RabbitMQ Plugin
Groovy
33
star
3

grails-standalone

Java
20
star
4

grails-cloud-foundry

Groovy
16
star
5

grails-release

The Release plugin for Grails
Groovy
16
star
6

grails-spring-security-ldap

JavaScript
14
star
7

grails-spring-security-openid

Groovy
14
star
8

grails-spring-security-shiro

Groovy
13
star
9

grails-spring-security-oauth-provider

Grails Spring Security OAuth Provider
Groovy
12
star
10

grails-rxjava

A plugin that integrates Grails with RxJava
Groovy
11
star
11

grails-paypal

Integrates Grails with Paypal APN
Groovy
10
star
12

grails-dropwizard-metrics

Groovy
10
star
13

gorm-logical-delete

GORM Logical Delete For Grails 3
Groovy
10
star
14

grails-angular-scaffolding

Groovy
9
star
15

grails-coffeescript

CoffeeScript Integration For Grails
Groovy
9
star
16

grails-scaffolding

Groovy
8
star
17

grails-cache-ehcache

Grails Cache Ehcache Plugin
Shell
8
star
18

grails-java8

A plugin to support Java 8 specific functionality
Groovy
7
star
19

grails-freemarker

FreeMarker Plugin For Grails
Groovy
7
star
20

grails-cache-redis

Grails Cache Redis Plugin
Groovy
7
star
21

grails-logback

Groovy
7
star
22

grails-atomikos

2PC/XA plugin implemented with the Atomikos library
Groovy
6
star
23

grails-blazeds

BlazeDS Plugin
Groovy
6
star
24

grails-gorm-jpa

GORM implementation over JPA
Groovy
5
star
25

grails-heroku

Groovy
5
star
26

grails-jetty

Grails Jetty Plugin
Shell
4
star
27

grails-spring-security-oauth2-provider

Groovy
4
star
28

grails-gemfire

GemFire Integration For Grails
Groovy
4
star
29

grails-cloud-foundry-ui

JavaScript
3
star
30

grails-spring-security-kerberos

Groovy
3
star
31

grails-git

Git integration for Grails
Groovy
3
star
32

grails-spring-security-appinfo

Groovy
3
star
33

grails-spring-security-oauth-consumer

Grails Spring Security OAuth Consumer
Groovy
3
star
34

grails-flex

Grails Flex Plugin
JavaScript
3
star
35

grails-hibernate4-plugin

Groovy
2
star
36

clojure

Groovy
2
star
37

grails-jbossas

Groovy
2
star
38

grails-cache-gemfire

Groovy
2
star
39

grails-cloud-support

Groovy
1
star
40

open-jpa-provider

Groovy
1
star
41

hibernate-jpa-provider

Groovy
1
star
42

grails-fsm

Finite State Machine behaviour for domain classes
Groovy
1
star
43

grails-memcached

Groovy
1
star
44

grails-filters

Groovy
1
star
45

grails-svn

Subversion plugin for Grails
Groovy
1
star