3

If I use 'web-send-json-response="true"' while calling any service, it returns json response using sendJsonResponse(Object responseObj) method of WebFacadeImpl.groovy. Where It adds 'errors' to json response if 'eci.getMessage().hasError()' true. But, Is there any reason why messages are not added in Json Response?

As I am willing to have messages in json response, I added few lines in sendJsonResponse() method. The code block where I added few lines to add messages to json response:

} else if (responseObj != null) {
    responseObj.put("messages", eci.message.messages)
    JsonBuilder jb = new JsonBuilder()
    jb.call(responseObj)
    jsonStr = jb.toString()
    response.setStatus(HttpServletResponse.SC_OK)
} else {
    jsonStr = ""
    if (eci.message.messages) {
        responseObj.put("messages", eci.message.messages)
        JsonBuilder jb = new JsonBuilder()
        jb.call(responseObj)
        jsonStr = jb.toString()
    }
    response.setStatus(HttpServletResponse.SC_OK)
}

It works fine and I am getting messages in json response.

  • 1
    The current code in the moqui/moqui GitHub repo has some modifications to do something similar, though only adds messages if the the responseObj is a Map and does not have an entry with the key "messages", or if responseObj is null then it will create a Map for the response. The current code uses the ec.message.getMessagesString() so that the messages field is a single String like the errors field as opposed to a List. – David E. Jones Feb 28 '15 at 11:48

0 Answers0