Rest服务没有看到来自Grails Rest Client Builder的参数 [英] Rest Service not seeing parameters from Grails Rest Client Builder

查看:74
本文介绍了Rest服务没有看到来自Grails Rest Client Builder的参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有一个简单的Grails用户界面,它需要几个字段.. firstName,lastName等等。控制器调用一个服务方法,然后使用Rest Client Builder插件调用一个REST服务。



其余服务不识别参数。



以下是简单的休息调用。

  def resp = rest.post(baseUrl, params)
{
header'Accept','application / json'
contentTypeapplication / x-www-form-urlencoded
}

使用插件的2.0.1版。



params看起来像

p>

  [firstName:Kas,action:index,format:null,controller:myController,max:10] 

Rest服务方法看起来像...

 <$ ({MediaType.APPLICATION_JSON,MediaType.APPLICATION_XML})
@Consumes({MediaType.APPLICATION_FORM_URLENCODED})$ b($ / $)$ b $ $ b public IdmResult createNewEmployee(@FormParam(firstName )String firstName){
try {
if(firstName == null)return constructFailedIdmResult(First Name is a required field);

//做一些其他的东西
}
}

服务以名字是必填字段作为响应。



当我从邮递员提交邮寄时,它工作正常。 Postman的成功请求看起来像

  POST / idm / employees HTTP / 1.1 
主机:< ip>:< ; URL>
接受:application / json
firstName:Kas
Cache-Control:no-cache
Content-Type:application / x-www-form-urlencoded

想要了解如何查看插件构建的请求,以便比较差异,但最终我只需要知道如何从插件中正确发送请求,以便Rest服务识别表单参数。 请求正文POST:

  def resp = rest.post(baseUrl){
header'Accept','application / json'
contentTypeapplication / x-www-form-urlencoded
json {
firstName =Kas
}
}

或简单地说,

  def resp = rest.post(baseUrl){
header'Accept','application / json'
contentTypeapplication / x-www-form-urlencoded
json firstName:Kas

请参阅 docs 以获得详细信息。

更新:



由于生产者期望请求参数是一个大的查询字符串而不是JSON,所以您最终可能会这样做:

  def queryString = params.collect {k,v  - > $ k = $ v} .join(/& /)

def resp = rest.post($ baseUrl?$ queryString){
header'Accept',' application / json'
contentTypeapplication / x-www-form-urlencoded
}

或者 def resp = rest.post($ baseUrl?$ queryString)


So I have a simple Grails UI which takes a few fields .. firstName, lastName etc. in a form. The controller calls a service method which then uses the Rest Client Builder plugin to call a REST service.

The rest service is not recognizing the parameters however.

Here is the simple rest call.

    def resp = rest.post(baseUrl, params)
            {
                header 'Accept', 'application/json'
                contentType "application/x-www-form-urlencoded"
            }

Using version 2.0.1 of the plugin.

params looks like

[firstName:Kas, action:index, format:null, controller:myController, max:10]

Rest Service Method looks like ...

@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
    }
 }

Service responds with "First Name is a required field"

When I submit the Post from Postman it works fine. Successful request from Postman looks like

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

Would like to figure how I can see the request that the plugin is constructing so I can compare differences, but ultimately I just need to know how to properly send the request from the plugin so that the Rest Service recognizes the form parameters.

解决方案

Rest client should be using request body to POST:

def resp = rest.post(baseUrl) {
    header 'Accept', 'application/json'
    contentType "application/x-www-form-urlencoded"
    json {
        firstName = "Kas"
    }
}

or simply,

def resp = rest.post(baseUrl) {
    header 'Accept', 'application/json'
    contentType "application/x-www-form-urlencoded"
    json firstName: "Kas"
}

Refer docs for detail.

UPDATE:

Since producer is expecting request params as big query string instead of JSON, you might end up doing this instead:

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"
}

or just def resp = rest.post("$baseUrl?$queryString")

这篇关于Rest服务没有看到来自Grails Rest Client Builder的参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆