Missing parameters with RESTful request when upgra

2019-06-22 15:39发布

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!

2条回答
Luminary・发光体
2楼-- · 2019-06-22 16:13

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
}
查看更多
我命由我不由天
3楼-- · 2019-06-22 16:26

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
                }
            }
        }
    }
}
查看更多
登录 后发表回答