gpt4 book ai didi

json - Grails RestBuilder发送奇怪的JSON

转载 作者:行者123 更新时间:2023-12-02 14:56:21 26 4
gpt4 key购买 nike

我最近将我的项目的grails版本从2.3.6升级到了2.5.0,以便使用与Java 8兼容的版本。我升级了许多插件版本以与升级的Grails版本一起使用。但是RestBuilder表现得很时髦。而不是发送以前的常规JSON对象,而是将其包装在实际对象(即{"target": {"var1":"value", "var2":"value"}, "prettyPrint": true})中。

这是我的依赖项:

    plugins {
//runtime ":hibernate4:4.3.1.2"
runtime ":hibernate:3.6.10.14"
runtime ":resources:1.2.14"
runtime ":database-migration:1.4.0"
runtime ":httplogger:1.1"

// Uncomment these (or add new ones) to enable additional resources capabilities
//runtime ":zipped-resources:1.0"
//runtime ":cached-resources:1.0"
//runtime ":yui-minify-resources:0.1.5"

build ":tomcat:7.0.52.1"

compile ":spring-security-core:2.0-RC5"
compile ":rest-client-builder:2.1.1"
}

这是代码:
def createCustomer(Customer customer, String customerId) {

def enrollmentResponse = new CustomerEnrollmentResponse()
def customerJson = customer as JSON
def partnerKey = springSecurityService.getCurrentUser().partnerKey
def baseUrl = grailsApplication.config.rest.baseUrl
String url = "${baseUrl}${customerApiContextUrl}${partnerKey}/${customerId}"
def resp = new RestBuilder().post(url) {
contentType: "application/json"
json: customerJson
}
enrollmentResponse.responseCode = resp.status
if (enrollmentResponse.responseCode != 200) {
enrollmentResponse.error = new APIError(resp.json)
}
return enrollmentResponse
}

最佳答案

不要使用“AS JSON”

改为使用

JsonBuilder json = new groovy.json.JsonBuilder(data)

json.toString()

这将为您提供确切的JSON值

关于json - Grails RestBuilder发送奇怪的JSON,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34402270/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com