Missing parameters with RESTful request when upgrading to Grails 2.3.0

拈花ヽ惹草 提交于 2019-12-23 10:16:19

问题


I am using Grails with RESTful to develop my web application. Everything works fine, till I upgrade my application to Grails 2.3. Here is my UrlMappings: I still send request, submit or do some other things normally, but in POST, PUT requests, the parameters are missing. Server just recognize only the parameters I put on the URL directly, but the remain I enclose in form or model when submit cannot be found in the "params" variable. He is my UrlMappings:

class UrlMappings {

    static mappings = {
        "/$controller/$action?/$id?"{ constraints {} }

        name apiSingle: "/api/$controller/$id"(parseRequest:true){
            action = [GET: "show", PUT: "update", DELETE: "delete"]
            constraints { id(matches:/\d+/) }
        }
        name apiCollection: "/api/$controller"(parseRequest:true){
            action = [GET: "list", POST: "save"]
        }

        name api2: "/api/$controller/$action"(parseRequest:true)
        name api3: "/api/$controller/$action/$id"(parseRequest:true)

        "/"(view:"/welcome")
        "500"(view:'/error')
    }
}

I have read the latest document of Grails 2.3, at http://grails.org/doc/latest/guide/theWebLayer.html#restfulMappings
but I think it is not clear. I have tried it follow the documentation but have no result. And there are no any sample about using Grails 2.3 with RESTful for me to refer.
How can I make it work normally as before, and can access all parameter values in REST request? Thank you so much!


回答1:


According to this http://grails.1312388.n4.nabble.com/Grails-2-3-and-parsing-json-td4649119.html parseRequest has no effect since Grails 2.3

If you use JSON as request body you can accees request params as request.JSON.paramName

As a workaround you can add a filter that will populate data from JSON to params:

class ParseRequestFilters {

    def filters = {
        remoteCalls(uri: "/remote/**") {
            before = {
                if (request.JSON) {
                    log.debug("Populating parsed json to params")
                    params << request.JSON
                }
            }
        }
    }
}



回答2:


Adding on to Kipriz's answer and cdeszaq's comment, you can write a recursive method to inject nested params. Something along these lines:

public void processNestedKeys(Map requestMap, String key) {
    if (getParameterValue(requestMap, key) instanceof JSONObject) {
        String nestedPrefix = key + ".";
        Map nestedMap = getParameterValue(requestMap, key)
        for (Map.Entry<String, Object> entry : nestedMap.entrySet()) {
            String newKey = nestedPrefix + entry.key;
            requestMap.put(newKey, getParameterValue(nestedMap, entry.key))
            processNestedKeys(requestMap, "${nestedPrefix + entry.key}");
        }
    }
}

public static Map populateParamsFromRequestJSON(def json) {
    Map requestParameters = json as ConcurrentHashMap
    for (Map.Entry<String, Object> entry : requestParameters.entrySet()) {
        processNestedKeys(requestParameters, entry.key)
    }

    return requestParameters
}


来源:https://stackoverflow.com/questions/18870003/missing-parameters-with-restful-request-when-upgrading-to-grails-2-3-0

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!