rest - Grails 2.4.3 : Consume a REST Service

标签 rest grails groovy basic-authentication

我如何在Grails 2.4.3中使用RESTful Web服务。我还需要使用基本身份验证。

您会认为这个问题已经有了很好的答案,但是我真的很难找到一个答案。许多答案都指向Grails rest插件的方向,我尝试了该插件,但无法为我工作。我想我可能只是在为文档而苦苦挣扎,并错误地使用了它。

最佳答案

我找到了REST Client Builder Plugin,该文档被更好地记录了,对我来说工作得更好。感谢Graeme Rocher!这是一个简单的示例,希望会对其他人有所帮助。

import grails.plugins.rest.client.RestBuilder
import grails.transaction.Transactional

@Transactional
class BatchInstanceService {

    def getBatch(String id) {
        String url = "https://foo.com/batch/$id"

        def resp = new RestBuilder().get(url) {
            header 'Authorization', 'Basic base64EncodedUsername&Password'
        }
    }
}

这是测试类(class)。
import grails.test.mixin.*

import org.apache.commons.httpclient.*
import org.apache.commons.httpclient.methods.*
import org.springframework.http.HttpStatus

import spock.lang.Specification

@TestFor(BatchInstanceService)
class BatchInstanceServiceSpec extends Specification {

    void "test get batch" () {
        when:
        def resp = service.restart('BI1234')

        then:
        resp.status == HttpStatus.OK.value
    }
}

返回的对象respResponseEntity类的实例。

我真的希望这会有所帮助。如果有更好的示例,请发布链接。谢谢!

关于rest - Grails 2.4.3 : Consume a REST Service,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25470866/

相关文章:

groovy - 在空 map 上调用每个失败

web-services - Backbone 模型和集合是否期望具有相同的项目表示?

node.js - Rest API Kafka 流

java - 使用 spring Rest 以自定义格式显示 json

maven - 如何避免错误 “javax.servlet-api-3.0.1.jar - jar not loaded” ?

grails - 如何使用Grails 2.4.3在Spring Security Rest中实现切换用户

spring - 使用Spring Security Rest插件时出现问题

groovy - 文件上传与 Groovy 融合?

grails - 访问 Controller 中的参数列表

java - JsonIdentityInfo 用于 hibernate 映射