Annotation or method to ignore field (Xstream 1.3.1)

Asked

Viewed 201 times

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 attribute ResultCol and also in your class ResultCol in the attribute ResultRow the following annotation: @XStreamOmitField

  • @XStreamOmitField&#xA;private ResultRow row; continue the mistake, I made both

  • Try to put in the class ResultRow the annotation @XStreamAlias("ResultRow") , in class ResultCol the annotation @XStreamAlias("ResultCol") and in class ResultList the annotation @XStreamAlias("ResultList") and now yes in the attributes where the circular reference to annotation occurs @XStreamOmitField

  • If it doesn’t work, I put a possible solution below

  • Didn’t work, should I add something instead : 'Xstream.setMode(Xstream.NO_REFERENCES);'

  • You can post your code?

  • In my first question is the code: [https://answall.com/questions/210304/erro-de-circularreferenceexception/210313#210313]

  • thanks, now just needed the code you ride the obj lista

  • Where can I post the code ? here in the comments or edit the question?

  • You can edit the question

  • 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.

  • You can also post the stacktrace ? only the last block

  • Yes, I’ll go for now.

  • 1

    Thanks for the help! solved yes, from now on I will organize in javascript. until next!

Show 10 more comments

1 answer

1

See if it solves:

XStream xstream = new XStream(new DomDriver()) {
    protected MapperWrapper wrapMapper(MapperWrapper next) {
        return new MapperWrapper(next) {
            public boolean shouldSerializeMember(Class definedIn, String fieldName) {
                try {
                    return definedIn != Object.class || realClass(fieldName) != null;
                } catch (CannotResolveClassException cnrce) {
                    return false;
                }
            }
        };
    }
};

xstream.setMode(XStream.NO_REFERENCES);
xstream.alias("resultList", ResultList.class);
xstream.alias("resultRow", ResultRow.class);
xstream.alias("resultCol", ResultCol.class);

return XML.toJSONObject(xstream.toXML(lista));

Don’t forget to put the note @XStreamOmitField where the circular reference occurs and in the classes that will turn XML the annotation @XStreamAlias('nomeDoObj')

  • I didn’t succeed in trying that way.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.