Javascript函数返回JsonMappingException:无限递归(StackOverflowError)

时间:2018-06-13 07:04:15

标签: javascript jackson thymeleaf

我是javascript的初学者。

我有一个带有瞬态方法的类,它包含另一个类的列表

@Entity
@Data
@Audited
public class Class1 implements Serializable {

    ...

    @Transient  
    private List<Class2> class2; 
}

Class2域名:

@Data
@Entity
@Audited
public class Varie implements Serializable{

    private static final long serialVersionUID = -4198681874842831567L;

    @ManyToOne(optional=false)
    private Class1 class1;

    @NotNull
    private Integer number;

    @NotNull
    @Enumerated(EnumType.STRING)
    private VarieEnum varieEnum;

}

在表单控制器中,我设置瞬态值,以便将其用于动态构建表。

html页面的摘录:

    <th:block th:each="v, vInd : *{varie}" > 
        <td scope="col" >
            <input type="number" th:id="number[+${vInd.index}+]+${prgStat.index}" th:name="class3[+${prgStat.index}+].v.varieEnum" class="text-right form-control-plaintext" readonly="readonly" min="0" th:max="*{intValue}" th:value="${v.number}"></input>
        </td>
   </th:block> 

我有一个按钮,用于添加另一个可编辑的行,这个按钮调用一个javascript函数。这是违规代码的摘录:

var varieStr;
var varList = [[${class1.class2}]];

for (i = 0; i < varList.lenght; i++) {
      varieStr += '<td scope="col" ><input type="number" id="varie'+varList[i]+'"  name="class3['+index+'].v.varieEnum" class="form-control text-right" min="0" max="'+varList[i].number+'"></input></td>';
            }

但是这段代码会产生错误:

  

引起:org.thymeleaf.exceptions.TemplateOutputException:模板渲染期间发生错误       at org.thymeleaf.engine.OutputTemplateHandler.handleText(OutputTemplateHandler.java:75)〜[thymeleaf-3.0.8.RELEASE.jar:3.0.8.RELEASE]       at org.thymeleaf.engine.Text.beHandled(Text.java:97)〜[thymeleaf-3.0.8.RELEASE.jar:3.0.8.RELEASE]       在org.thymeleaf.engine.Model.process(Model.java:282)〜[thymeleaf-3.0.8.RELEASE.jar:3.0.8.RELEASE]       at org.thymeleaf.engine.ProcessorTemplateHandler.handleOpenElement(ProcessorTemplateHandler.java:1587)~ [thymeleaf-3.0.8.RELEASE.jar:3.0.8.RELEASE]       在org.thymeleaf.engine.OpenElementTag.beHandled(OpenElementTag.java:205)〜[thymeleaf-3.0.8.RELEASE.jar:3.0.8.RELEASE]       在org.thymeleaf.engine.TemplateModel.process(TemplateModel.java:136)〜[thymeleaf-3.0.8.RELEASE.jar:3.0.8.RELEASE]       在org.thymeleaf.engine.TemplateManager.process(TemplateManager.java:519)〜[thymeleaf-3.0.8.RELEASE.jar:3.0.8.RELEASE]       at org.thymeleaf.util.LazyProcessingCharSequence.writeUnresolved(LazyProcessingCharSequence.java:85)〜[thymeleaf-3.0.8.RELEASE.jar:3.0.8.RELEASE]       at org.thymeleaf.util.AbstractLazyCharSequence.write(AbstractLazyCharSequence.java:103)〜[thymeleaf-3.0.8.RELEASE.jar:3.0.8.RELEASE]       at org.thymeleaf.engine.AbstractTextualTemplateEvent.writeContent(AbstractTextualTemplateEvent.java:224)〜[thymeleaf-3.0.8.RELEASE.jar:3.0.8.RELEASE]       at org.thymeleaf.engine.Text.write(Text.java:78)〜[thymeleaf-3.0.8.RELEASE.jar:3.0.8.RELEASE]       at org.thymeleaf.engine.OutputTemplateHandler.handleText(OutputTemplateHandler.java:71)〜[thymeleaf-3.0.8.RELEASE.jar:3.0.8.RELEASE]       ...省略了126个常用帧   引起:org.thymeleaf.exceptions.TemplateProcessingException:尝试使用Jackson将对象序列化为JavaScript时引发异常       at org.thymeleaf.standard.serializer.StandardJavaScriptSerializer $ JacksonStandardJavaScriptSerializer.serializeValue(StandardJavaScriptSerializer.java:189)~ [thymeleaf-3.0.8.RELEASE.jar:3.0.8.RELEASE]       at org.thymeleaf.standard.serializer.StandardJavaScriptSerializer.serializeValue(StandardJavaScriptSerializer.java:139)~ [thymeleaf-3.0.8.RELEASE.jar:3.0.8.RELEASE]       at org.thymeleaf.util.LazyEscapingCharSequence.produceEscapedOutput(LazyEscapingCharSequence.java:135)~ [thymeleaf-3.0.8.RELEASE.jar:3.0.8.RELEASE]       at org.thymeleaf.util.LazyEscapingCharSequence.writeUnresolved(LazyEscapingCharSequence.java:94)〜[thymeleaf-3.0.8.RELEASE.jar:3.0.8.RELEASE]       at org.thymeleaf.util.AbstractLazyCharSequence.write(AbstractLazyCharSequence.java:103)〜[thymeleaf-3.0.8.RELEASE.jar:3.0.8.RELEASE]       at org.thymeleaf.engine.AbstractTextualTemplateEvent.writeContent(AbstractTextualTemplateEvent.java:224)〜[thymeleaf-3.0.8.RELEASE.jar:3.0.8.RELEASE]       at org.thymeleaf.engine.Text.write(Text.java:78)〜[thymeleaf-3.0.8.RELEASE.jar:3.0.8.RELEASE]       at org.thymeleaf.engine.OutputTemplateHandler.handleText(OutputTemplateHandler.java:71)〜[thymeleaf-3.0.8.RELEASE.jar:3.0.8.RELEASE]       ...省略了137个常用帧   引起:com.fasterxml.jackson.databind.JsonMappingException:无限递归(StackOverflowError)

该错误只能在class2中设置null class1的值:

List<Class2> varieList = varieService.findByClass1(class1);
    class1.setVarie(varieList);
    int varie = 0;
    if(!varieList.isEmpty()) {
        for (Class2 v: varieList) {
            varie+=v.getNumber();
            v.setClass2(null);
        }
    }

这是解决问题的唯一方法,或者我的javascript逻辑中存在错误?

0 个答案:

没有答案