2017-02-24 58 views
0

Groovy/Grails对我来说更多新手问题。尝试将域类序列化为JSON时出现Grails堆栈溢出错误

的Groovy版本2.4.8版本的Grails 2.5.1

我曾尝试多种方式来序列化我的领域类的一个实例或域类的实例的ArrayList。

当试图序列化单个实例时,出现堆栈溢出错误。

代码和堆栈跟踪如下所示

def getAdvisors(String keystrokes, String firm) { 
    def advisors = priceBlotterService.advisorsForKeystrokes(keystrokes, "", 30) 
    def a1 = advisors[0] 
    def json = JsonOutput.toJson(a1) 
} 

Caused by InvocationTargetException: null 
->> 198 | doFilter in grails.plugin.cache.web.filter.PageFragmentCachingFilter 
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
|  63 | doFilter in grails.plugin.cache.web.filter.AbstractFilter 
| 1145 | runWorker in java.util.concurrent.ThreadPoolExecutor 
| 615 | run  in java.util.concurrent.ThreadPoolExecutor$Worker 
^ 745 | run . . . in java.lang.Thread 
Caused by StackOverflowError: null 
->> 100 | invoke in org.codehaus.groovy.reflection.CachedMethod 
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - 
|  62 | getProperty in groovy.lang.MetaBeanProperty 
|  42 | getValue in groovy.lang.PropertyValue 
| 388 | getProperties in  org.codehaus.groovy.runtime.DefaultGroovyMethods 
| 290 | writeObject in groovy.json.JsonOutput 
| 329 | writeArray in  '' 
| 286 | writeObject in  '' 
| 424 | writeMap in  '' 
| 294 | writeObject in  '' 
| 329 | writeArray in  '' 
| 286 | writeObject in  '' 
| 424 | writeMap in  '' 

顾问,案例,与企业类:

class Advisor { 
    String firstName 
    String lastName 
    String fullName 
    String city 
    String state 
    Firm firm 
    static belongsTo = [Case, Firm] 
    static hasMany = [cases:Case] 
    static constraints = { 
    } 
} 


class Case { 
    String caseCode 
    String internalComment 
    String externalComment 
    Date dateCreated 
    String createdBy 
    Date dateUpdated 
    String updatedBy 

    static belongsTo = [owner:User, caseStatusType:CaseStatusType] 
    static hasMany = [advisor:Advisor] 
    static mapping = { 
     dateCreated sqlType: "date" 
     dateUpdated sqlType: "date" 
    } 
    static constraints = { 
     dateCreated(nullabe: false) 
     dateUpdated(nullable: false) 
    } 
} 

class Firm { 
    String name 
    static constraints = { 
    } 
} 

编辑:

我发现了一个根本性的问题,我的领域类/表格可能与此有关,需要解决。

我试图做一个简单的从用户表获得,我得到一个错误消息,指出没有ID字段。很难弄清楚发生了什么。一些细节如下。

行代码

User[] users = User.findAll() 

错误消息

org.springframework.jdbc.BadSqlGrammarException: Hibernate operation: could not extract ResultSet; bad SQL grammar [n/a]; nested exception is org.postgresql.util.PSQLException: ERROR: column this_.id does not exist Position: 8 

用户类

class User { 
    String firstName 
    String lastName 

    static constraints = { 
    } 
} 

DDL对于用户表

CREATE TABLE "user" 
(
    id BIGINT DEFAULT nextval('user_id_seq'::regclass) PRIMARY KEY NOT NULL, 
    first_name VARCHAR(30), 
    last_name VARCHAR(30), 
    version BIGINT 
); 
CREATE UNIQUE INDEX user_id_uindex ON "user" (id); 

编辑:

具有用户表/类的固定发行者。用户是Postresql中的关键字,因此我只是将其重构为EndUser。

+0

你可以发布您的案例与企业类的细节? – LeslieV

+0

@LeslieV Ok补充说,谢谢! – NewDev

回答

1

我怀疑您的数据结构存在问题,导致JSON构建器进入无限循环。

您可能要查看此对问题信息的日期:https://issues.apache.org/jira/browse/GROOVY-7682

这可能代替工作:

import grails.converters.JSON 
def json = new JSON(a1) 
+0

谢谢,仍然没有工作,我得到错误'抛出方法'java.lang.RuntimeException'异常。无法评估grails.converters.JSON.toString()'。我发现了一个不同的问题,并认为我应该关注这个问题,因为它非常重要。另一个错误是用户类/表错误。由于某些原因,我无法从桌子上拉出任何东西,我会将信息添加到原始问题中。 – NewDev