1
I have a Circularreferenceexception problem when I try to do this:
XStream xstream = new XStream(new JettisonMappedXmlDriver());
xstream.setMode(XStream.NO_REFERENCES);
xstream.alias("resultList", ResultList.class);
xstream.alias("resultRow", ResultRow.class);
xstream.alias("resultCol", ResultCol.class);
return xstream.toXML(lista);
I’m already aware of where the problem is, I have the resultRow class that has resultCol, inside resultCol has a resultRow, it got complicated but that’s it. And at the moment I cannot change this schema, I need something to make the stream.toXml ignore the resultRow attribute inside the resultCol to prevent the Circularreferenceexception, if information is missing in my question may request.
Code that mounts Resultlist :
protected ResultList listar(TelaSistemaConsultaUsuarioPreferencia pref, Target targetXml,
br.com.gwsistemas.eutil.consulta.Consulta consulta) throws SQLException {
ResultList retorno = new ResultList();
ResultRow row = null;
ResultCol rcol = null;
try {
StringBuilder sql = new StringBuilder();
sql.append("SELECT * FROM ").append(targetXml.getName());
sql.append(" ").append(pref.getCondicaoConsulta());
if (!pref.getOrdenacao().equals("")) {
sql.append(" ORDER BY ").append(pref.getOrdenacao());
}
sql.append(" OFFSET ")
.append(Apoio.parseInt(consulta.getPaginacao().getPaginaAtual() - 1) * pref.getLimiteResultados());
sql.append(" LIMIT ").append(pref.getLimiteResultados());
StringBuilder sqlQtdResultados = new StringBuilder();
sqlQtdResultados.append("SELECT count(*) as resultados FROM ").append(targetXml.getName());
sqlQtdResultados.append(" ").append(pref.getCondicaoConsulta());
prepSt = con.prepareStatement(sql.toString());
rs = prepSt.executeQuery();
while (rs.next()) {
row = new ResultRow();
for (int i = 1; i <= rs.getMetaData().getColumnCount(); i++) {
rcol = new ResultCol(row, rs.getObject(rs.getMetaData().getColumnName(i)),
rs.getMetaData().getColumnName(i), rs.getMetaData().getColumnTypeName(i),
rs.getMetaData().getColumnType(i));
if (targetXml != null) {
for (Column col : targetXml.getColumn()) {
if (col.getName().equals(rcol.getNome())) {
rcol.setFormat(col.getFormat());
rcol.setLabel(col.getLabel());
row.addColumn(rcol);
}
}
} else {
row.addColumn(rcol);
}
}
retorno.getRows().add(row);
}
if (consulta != null) {
prepSt = con.prepareStatement(sqlQtdResultados.toString());
rs = prepSt.executeQuery();
while (rs.next()) {
consulta.getPaginacao().setQtdResultados(rs.getInt("resultados"));
consulta.getPaginacao().setPaginas((int) Math
.ceil(new Double(consulta.getPaginacao().getQtdResultados() / pref.getLimiteResultados())));
}
}
return retorno;
} catch (SQLException e) {
if (prepSt != null) {
LOG.error("SQL_ERROR:" + prepSt);
}
LOG.error(e, e);
throw e;
} finally {
fecharConexao();
}
}
Stacktrace :
Caused by: javax.el.Elexception: Problems Calling Function 'cg:resultListToJson' at org.apache.el.parser.Astfunction.getValue(Astfunction.java:211) at org.apache.el.ValueExpressionImpl.getValue(Valueexpressionimpl.java:184) at org.apache.Jasper.runtime.Pagecontextimpl.proprietaryEvaluate(Pagecontextimpl.java:944) at org.apache.jsp.gwTrans.consulta.consulta_002dmercadoria_002ddeposito_jsp. _jspService(consulta_002dmercadoria_002ddeposito_jsp.java:595) at org.apache.Jasper.runtime.Httpjspbase.service(Httpjspbase.java:70) at javax.servlet.http.HttpServlet.service(Httpservlet.java:729) at org.apache.Jasper.servlet.Jspservletwrapper.service(Jspservletwrapper.java:443) ... 41 more Caused by: com.thoughtworks.xstream.core.TreeMarshaller$CircularReferenceException: at com.thoughtworks.Xstream.core.Treemarshaller.Convert(Treemarshaller.java:83) at com.thoughtworks.Xstream.core.Treemarshaller.convertAnother(Treemarshaller.java:78) at com.thoughtworks.Xstream.converters.Reflection.AbstractReflectionConverter.marshallField(Abstractreflectionconverter.java:157) at com.thoughtworks.Xstream.converters.Reflection.Abstractreflectionconverter$2.writeField(Abstractreflectionconverter.java:148) at com.thoughtworks.Xstream.converters.Reflection.Abstractreflectionconverter$2.visit(Abstractreflectionconverter.java:118) at com.thoughtworks.Xstream.converters.Reflection.PureJavaReflectionProvider.visitSerializableFields(Purejavareflectionprovider.java:129) at com.thoughtworks.Xstream.converters.Reflection.AbstractReflectionConverter.doMarshal(Abstractreflectionconverter.java:100) at com.thoughtworks.Xstream.converters.Reflection.AbstractReflectionConverter.marshal(Abstractreflectionconverter.java:58) at com.thoughtworks.Xstream.core.Treemarshaller.Convert(Treemarshaller.java:86))
Try within your class
ResultRow
in the attributeResultCol
and also in your classResultCol
in the attributeResultRow
the following annotation:@XStreamOmitField
– brow-joe
@XStreamOmitField
private ResultRow row;
continue the mistake, I made both– Mateus Veloso
Try to put in the class
ResultRow
the annotation@XStreamAlias("ResultRow")
, in classResultCol
the annotation@XStreamAlias("ResultCol")
and in classResultList
the annotation@XStreamAlias("ResultList")
and now yes in the attributes where the circular reference to annotation occurs@XStreamOmitField
– brow-joe
If it doesn’t work, I put a possible solution below
– brow-joe
Didn’t work, should I add something instead : 'Xstream.setMode(Xstream.NO_REFERENCES);'
– Mateus Veloso
You can post your code?
– brow-joe
In my first question is the code: [https://answall.com/questions/210304/erro-de-circularreferenceexception/210313#210313]
– Mateus Veloso
thanks, now just needed the code you ride the obj
lista
– brow-joe
Where can I post the code ? here in the comments or edit the question?
– Mateus Veloso
You can edit the question
– brow-joe
I just checked that by removing the resultRow from inside the resultCol and your get, even so the error continues, so I don’t think that’s where the Circularreferenceexception.
– Mateus Veloso
You can also post the
stacktrace
? only the last block– brow-joe
Yes, I’ll go for now.
– Mateus Veloso
Let’s go continue this discussion in chat.
– brow-joe
Thanks for the help! solved yes, from now on I will organize in javascript. until next!
– Mateus Veloso