Rest服务看不到Grails Rest客户端生成器中的参数
因此,我有一个简单的Grails UI,它包含几个字段。。表中的名字、姓氏等。控制器调用服务方法,然后使用Rest客户端生成器插件调用Rest服务 但是,rest服务无法识别这些参数 下面是简单的rest调用Rest服务看不到Grails Rest客户端生成器中的参数,rest,grails,Rest,Grails,因此,我有一个简单的Grails UI,它包含几个字段。。表中的名字、姓氏等。控制器调用服务方法,然后使用Rest客户端生成器插件调用Rest服务 但是,rest服务无法识别这些参数 下面是简单的rest调用 def resp = rest.post(baseUrl, params) { header 'Accept', 'application/json' contentType "applicat
def resp = rest.post(baseUrl, params)
{
header 'Accept', 'application/json'
contentType "application/x-www-form-urlencoded"
}
使用插件的2.0.1版
params看起来像
[firstName:Kas, action:index, format:null, controller:myController, max:10]
POST /idm/employees HTTP/1.1
Host: <ip>:<url>
Accept: application/json
firstName: Kas
Cache-Control: no-cache
Content-Type: application/x-www-form-urlencoded
Rest服务方法看起来像
@POST
@Path("/employees")
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
@Consumes({MediaType.APPLICATION_FORM_URLENCODED})
public IdmResult createNewEmployee(@FormParam("firstName") String firstName) {
try {
if(firstName == null) return constructFailedIdmResult("First Name is a required field");
// Do some other stuff
}
}
服务响应“名字是必填字段”
当我提交邮递员的邮件时,效果很好。来自邮递员的成功请求看起来像
[firstName:Kas, action:index, format:null, controller:myController, max:10]
POST /idm/employees HTTP/1.1
Host: <ip>:<url>
Accept: application/json
firstName: Kas
Cache-Control: no-cache
Content-Type: application/x-www-form-urlencoded
POST/idm/employees HTTP/1.1
主持人:
接受:application/json
名字:卡斯
缓存控制:没有缓存
内容类型:application/x-www-form-urlencoded
我想知道如何查看插件正在构建的请求,以便比较差异,但最终我只需要知道如何正确地从插件发送请求,以便Rest服务识别表单参数。Rest客户端应该使用请求正文发布:
def resp = rest.post(baseUrl) {
header 'Accept', 'application/json'
contentType "application/x-www-form-urlencoded"
json {
firstName = "Kas"
}
}
或者简单地说
def resp = rest.post(baseUrl) {
header 'Accept', 'application/json'
contentType "application/x-www-form-urlencoded"
json firstName: "Kas"
}
有关详细信息,请参阅
更新:
由于producer希望请求参数作为大查询字符串而不是JSON,因此您可能会转而执行以下操作:
def queryString = params.collect { k, v -> "$k=$v" }.join(/&/)
def resp = rest.post("$baseUrl?$queryString") {
header 'Accept', 'application/json'
contentType "application/x-www-form-urlencoded"
}
或者干脆
def resp=rest.post($baseUrl?$queryString”)
rest客户端应该使用请求主体来发布:
def resp = rest.post(baseUrl) {
header 'Accept', 'application/json'
contentType "application/x-www-form-urlencoded"
json {
firstName = "Kas"
}
}
或者简单地说
def resp = rest.post(baseUrl) {
header 'Accept', 'application/json'
contentType "application/x-www-form-urlencoded"
json firstName: "Kas"
}
有关详细信息,请参阅
更新:
由于producer希望请求参数作为大查询字符串而不是JSON,因此您可能会转而执行以下操作:
def queryString = params.collect { k, v -> "$k=$v" }.join(/&/)
def resp = rest.post("$baseUrl?$queryString") {
header 'Accept', 'application/json'
contentType "application/x-www-form-urlencoded"
}
或者只需
def resp=rest.post($baseUrl?$queryString”)
要在请求正文中干净地传递值,请根据此答案使用多值映射和(未记录,从我看到的)'body()'方法 若要在请求正文中干净地传递值,请使用多值映射和(我看到的未记录的)'body()'方法,如下面的回答所示 有没有一种方法可以在不单独指定每个参数的情况下干净地传递请求正文中的params对象?您可以使用json params
。但是为什么要传递生产者不需要的请求元素呢。像动作
和控制器
。它们与制作人无关。表单将有更多可能的字段,而不是名字。更新表单将有20多个可更新的可能字段…如果每次表单字段更改时不需要重新编码服务调用,那么它将更干净。在这种情况下,我将完成从参数到新映射的映射,或者只是删除那些不需要的
,然后将其发送过来。不管怎样,使用json参数
works?第一次尝试给出了相同的错误。。。我想看看实际的请求。。。我想,应该让这个问题变得显而易见……有没有一种方法可以在不单独指定每个参数的情况下干净地传递请求正文中的params对象?您可以使用json params
。但是为什么要传递生产者不需要的请求元素呢。像动作
和控制器
。它们与制作人无关。表单将有更多可能的字段,而不是名字。更新表单将有20多个可更新的可能字段…如果每次表单字段更改时不需要重新编码服务调用,那么它将更干净。在这种情况下,我将完成从参数到新映射的映射,或者只是删除那些不需要的
,然后将其发送过来。不管怎样,使用json参数
works?第一次尝试给出了相同的错误。。。我想看看实际的请求。。。我想应该把问题弄清楚。。。