本文介绍了为什么我一直在服务器上获取对象引用,而在本地系统上工作正常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我的类对象没有被转换成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') $ b $ 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& ) $ b {response:[commons chains)它给了我下面的json响应com.est.dxclient.common.ProductAlertsResponse@67f797,com.est.dxclient.common.ProductAlertsResponse@1e8f2a0,com.est.dxclient.common.ProductAlertsResponse @ c3d9ac,com.est.dxclient.common.ProductAlertsResponse@7d8bb] } $ b 注意:我正在使用groovy 1.8.0 更新添加服务器端代码 我的服务器端 类GetProductAlertsResponse implements命令{ // 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)+: + AppPro appletperties.get(username),AppProperties.get(password),oracle.jdbc.OracleDriver) 列表< ProductAlertsResponse> sqlResult = executeSelection(select ID,NAME,DESCRIPTION,ACTIVE,RELEASE_DATE from productinfo where ACTIVE ='A') $ b $ def json = new groovy.json.JsonBuilder([response:sqlResult]) / *我想在这里调试代码* / println sqlResult.size() sqlResult.each { println it.getName )} 字符串响应= json.toPrettyString() println内部命令$ response ctx.put(CSMContextParams.response,response )返回false; $ b code $ b $ p $在控制台上打印 $> p> 4 ABC7 ABC3 ABC1 ABC2 内部命令[ com.est.dxclient.common.ProductAlertsResponse@a63599,com.est.dxclient.common.ProductAlertsResponse@156f14c,com.est.dxclient.common.ProductAlertsResponse @ 9ba632,com.est.dxclient.common.ProductAlertsResponse@bc5245] 这个答案可能有帮助 - Grails JSONBuilder 。 也许您的服务器代码使用不同版本的Grails,或者Grails在本地环境和服务器环境中设置的不一样。 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 codeprivate def static sqlprivate 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.0Update adding server side codeMy server sideclass 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 prints4ABC7ABC3ABC1ABC2Inside 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"] 解决方案 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. 这篇关于为什么我一直在服务器上获取对象引用,而在本地系统上工作正常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云! 06-04 15:06