【问题标题】:HTTP Post from Gradle task - Groovy来自 Gradle 任务的 HTTP 发布 - Groovy
【发布时间】:2019-03-02 09:44:46
【问题描述】:

我正在使用插件io.github.http-builder-ng.http-plugin 拨打Http Post。下面是我的build.gradle

plugins {
     id "io.github.http-builder-ng.http-plugin" version "0.1.1"
}

import groovyx.net.http.HttpBuilder

task makeRESTCall () {
onlyIf {
    !dependencyList.empty
}
doFirst {
    println dependencyList
    def http = HttpBuilder.configure {
        request.uri = 'http://localhost:8080'
        request.contentType = 'application/json'
        request.uri.path = '/api/v1/fosscomponents/bulkvalidate'
    }
    http.post {
        request.body=dependencyList
        response.success {
            println "Success"
        }
    }
}

}

这是我在request.body 中发送的dependencyList 参数

[

{
    "groupId":"org.hibernate",
    "artifactId":"hibernate-validator",
    "version":"5.3.6.Final"
}, 
{
    "groupId":"org.projectlombok",
    "artifactId":"lombok",
    "version":"1.16.22"
}, 
{
    "groupId":"io.springfox",
    "artifactId":"springfox-swagger-ui",
    "version":"2.8.0"
}
]

我的后端 API 中的 java 代码(用 Spring Boot 编写)只是以这种格式打印

incomingLists.stream().forEach(
            obj -> System.out.println(obj.getGroupId()+" **** "+obj.getArtifactId()+" **** "+obj.getVersion())
        );

当我从邮递员那里点击这个 API 时,响应是正确的 - 请参阅下面的代码打印

org.hibernate **** hibernate-validator **** 5.3.6.Final
org.projectlombok **** lombok **** 1.16.22

而当我从我的 gradle 任务中访问相同的 API 时,我的 java 代码会以这种方式打印它..

null **** null **** null
null **** null **** null

不知道是否遗漏了什么

【问题讨论】:

    标签: spring-boot gradle groovy build.gradle


    【解决方案1】:

    我解决了这个!上面显示的request.body 只是将dependencyList 作为列表对象本身发送,而不是API 预期的String

    所以我将其更改为以下方式,它按预期工作:)

    http.post {
            request.body=dependencyList.toString()
            response.success {
                println "Success"
            }
     }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-11-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多