3

My class objects are not getting converted into json, I think it returns object references, though while executing it locally, it executes perfectly.

Here is the code

private def static sql

private static List<ProductAlertsResponse> executeSelection(String query)
{
    List<ProductAlertsResponse> prodAlerts=new ArrayList<ProductAlertsResponse>()
    sql.eachRow(query)
    {
        ProductAlertsResponse prodAlert=new ProductAlertsResponse((String)it.id,(String)it.name,(String)it.description,(String)it.active,(String)it.release_date)
        prodAlerts.add(prodAlert)
    }
    return prodAlerts
}

static main(args) {

    AppProperties.load()
    sql = Sql.newInstance("jdbc:oracle:thin:@"+AppProperties.get("hostname")+":"+AppProperties.get("port")+":"+AppProperties.get("service"), AppProperties.get("username"), AppProperties.get("password"),"oracle.jdbc.OracleDriver")

    List<ProductAlertsResponse> sqlResult=executeSelection("select ID,NAME,DESCRIPTION,ACTIVE,RELEASE_DATE from productinfo where ACTIVE='A'")

    def json = new groovy.json.JsonBuilder([response: sqlResult])

    String response=json.toPrettyString()
    println "$response"
}

This gives me back the following response

{
"response": [
    {
        "active": "A",
        "release_date": "2011-09-23 00:00:00.0",
        "id": "1",
        "description": "Test",
        "name": "ABC7"
    },
    {
        "active": "A",
        "release_date": "2012-01-19 00:00:00.0",
        "id": "5",
        "description": "Test1",
        "name": "ABC3"
    },
    {
        "active": "A",
        "release_date": "2011-09-23 00:00:00.0",
        "id": "3",
        "description": "Test",
        "name": "ABC1"
    },
    {
        "active": "A",
        "release_date": "2012-01-19 00:00:00.0",
        "id": "6",
        "description": "Test2",
        "name": "ABC2"
    }
]
}

While running it on my server (struts & commons chains) it gives me back the following json response (after removing static)

{
"response": [
    "com.est.dxclient.common.ProductAlertsResponse@67f797",
    "com.est.dxclient.common.ProductAlertsResponse@1e8f2a0",
    "com.est.dxclient.common.ProductAlertsResponse@c3d9ac",
    "com.est.dxclient.common.ProductAlertsResponse@7d8bb"
]
}

Note: I am using groovy 1.8.0

Update adding server side code

My server side

class GetProductAlertsResponse implements Command {
//Command is import org.apache.commons.chain.Command
    private def sql

    @Override
    public boolean execute(Context ctx) throws Exception 
    {
        AppProperties.load()
        sql = Sql.newInstance("jdbc:oracle:thin:@"+AppProperties.get("hostname")+":"+AppProperties.get("port")+":"+AppProperties.get("service"), AppProperties.get("username"), AppProperties.get("password"),"oracle.jdbc.OracleDriver")

        List<ProductAlertsResponse> sqlResult=executeSelection("select ID,NAME,DESCRIPTION,ACTIVE,RELEASE_DATE from productinfo where ACTIVE='A'")

        def json = new groovy.json.JsonBuilder([response: sqlResult])

        /*I am trying to debug the code here*/
        println sqlResult.size()

        sqlResult.each{
            println it.getName()
        }

        String response=json.toPrettyString()

        println "Inside commands $response"
        ctx.put(CSMContextParams.response,response)
        return false;

    }
}

On the console it prints

4
ABC7
ABC3
ABC1
ABC2
Inside commands [
    "com.est.dxclient.common.ProductAlertsResponse@a63599",
    "com.est.dxclient.common.ProductAlertsResponse@156f14c",
    "com.est.dxclient.common.ProductAlertsResponse@9ba632",
    "com.est.dxclient.common.ProductAlertsResponse@bc5245"
]
5
  • Hi, does any body have any idea?? Has anyone tried it? Commented Jan 23, 2012 at 12:01
  • Can you show your server code? Commented Jan 23, 2012 at 12:15
  • @Paul Grime: Hi, I added my server side code.. there is a lot of code so I only added the Command's class GetProductAlertsResponse Commented Jan 23, 2012 at 12:26
  • You seem to have two lines for String response=json.toPrettyString(), is this correct? Commented Jan 23, 2012 at 12:34
  • @Paul: I am sorry, actually I am trying other ways too so I had to write the execute function (copy/paste) Commented Jan 23, 2012 at 12:46

1 Answer 1

1

This answer might help - Grails JSONBuilder.

Maybe your server code uses a different version of Grails, or Grails isn't setup the same between local and server environments.

Sign up to request clarification or add additional context in comments.

1 Comment

I finally managed to find the problem & fix it. It was due to Groovy Library. In the projects build path I have been using groovy-all-1.8.0.jar but in my Package Explorer under Groovy Libraries it had groovy-all-1.8.4.jar so on the server side too i changed my lib folder to have groovy-all-1.8.4.jar instead of groovy-all-1.8.0.jar

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.