【问题标题】:Why do I keep getting Object reference on server while it works fine on a local system为什么我一直在服务器上获取对象引用,而它在本地系统上工作正常
【发布时间】:2012-01-23 09:37:07
【问题描述】:

我的类对象没有被转换成 json,我认为它返回了对象引用,虽然在本地执行它时,它执行得很好。

这里是代码

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

这给了我以下响应

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

在我的服务器(struts 和 commons 链)上运行它时,它会返回以下 json 响应(删除静态后)

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

注意:我使用的是 groovy 1.8.0

更新添加服务器端代码

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;

    }
}

在控制台上打印

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

【问题讨论】:

  • 嗨,有人知道吗?有人试过吗?
  • 你能显示你的服务器代码吗?
  • @Paul Grime:嗨,我添加了我的服务器端代码.. 有很多代码所以我只添加了命令的类GetProductAlertsResponse
  • String response=json.toPrettyString() 似乎有两行,对吗?
  • @Paul:对不起,其实我也在尝试其他方法,所以我不得不编写执行函数(复制/粘贴)

标签: java json groovy struts


【解决方案1】:

这个答案可能会有所帮助 - Grails JSONBuilder

也许您的服务器代码使用不同版本的 Grails,或者 Grails 在本地环境和服务器环境之间的设置不同。

【讨论】:

  • 我终于找到了问题并解决了它。这是由于 Groovy 库。在项目构建路径中,我一直在使用 groovy-all-1.8.0.jar,但在 Groovy Libraries 下的 Package Explorer 中,它有 groovy-all-1.8.4.jar 所以在服务器端我也将我的 lib 文件夹更改为 groovy-all-1.8.4.jar 而不是 groovy-all-1.8.0.jar
猜你喜欢
  • 1970-01-01
  • 2012-05-02
  • 2011-11-11
  • 1970-01-01
  • 2010-11-18
  • 2013-09-20
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多