1
resposta

Erro ao tentar recuperar XML de EmpresasService

@WebServlet("/empresas")
public class EmpresasService extends HttpServlet {
    private static final long serialVersionUID = 1L;


    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

        List <Empresa> empresas = new Banco().getEmpresas();


        XStream xstream = new XStream();
        xstream.alias("empresa", Empresa.class);
        String xml = xstream.toXML(xstream);

        response.setContentType("application/xml");
        response.getWriter().print(xml);


//        Gson gson = new Gson();
//        
//        String json = gson.toJson(empresas);
//        
//        response.setContentType("application/json");
//        response.getWriter().print(json);

    }

}

Jul 11, 2021 9:13:11 PM org.apache.catalina.core.StandardWrapperValve invoke GRAVE: Servlet.service() for servlet [br.com.alura.gerenciador.servlet.EmpresasService] in context with path [/gerenciador] threw exception com.thoughtworks.xstream.converters.ConversionException: Cannot marshal the XStream instance in action

---- Debugging information ----

at com.thoughtworks.xstream.converters.reflection.SelfStreamingInstanceChecker.marshal(SelfStreamingInstanceChecker.java:34)
at com.thoughtworks.xstream.core.AbstractReferenceMarshaller.convert(AbstractReferenceMarshaller.java:55)
at com.thoughtworks.xstream.core.TreeMarshaller.convertAnother(TreeMarshaller.java:50)
at com.thoughtworks.xstream.core.TreeMarshaller.start(TreeMarshaller.java:73)
at com.thoughtworks.xstream.core.ReferenceByXPathMarshallingStrategy.marshal(ReferenceByXPathMarshallingStrategy.java:34)
at com.thoughtworks.xstream.XStream.marshal(XStream.java:765)
at com.thoughtworks.xstream.XStream.marshal(XStream.java:754)
at com.thoughtworks.xstream.XStream.toXML(XStream.java:735)
at com.thoughtworks.xstream.XStream.toXML(XStream.java:725)
at br.com.alura.gerenciador.servlet.EmpresasService.service(EmpresasService.java:32)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:733)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:227)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:53)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:189)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:162)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:202)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:97)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:542)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:143)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:92)
at org.apache.catalina.valves.AbstractAccessLogValve.invoke(AbstractAccessLogValve.java:687)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:78)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:357)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:374)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:65)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:893)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1707)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1149)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:624)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:748)
1 resposta

Oie Luciano, tudo bem contigo?

Perdão pela demora!

Seu código apresenta um erro nessa linha aqui:

        String xml = xstream.toXML(xstream);

O correto seria:

        String xml = xstream.toXML(empresas);

Espero ter ajudado!

Se tiver qualquer outra dúvida estarei por aqui :)