xml vazio

14 views
Skip to first unread message

Carl Edwin Antonio Nascimento

unread,
Mar 13, 2014, 7:45:20 PM3/13/14
to soa-ap...@googlegroups.com
Olá, estou tentando realizar o teste de impressão do xml e o mesmo trás lista vazia. poderia me auxiliar?O meio de comunicação sobre o livro pode ser este ou você indica outro?ESTE Servlet não exibe o xml populado:package com.rest.servlet.http;import java.io.IOException;import java.io.PrintWriter;import java.util.ArrayList;import java.util.Collection;import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.xml.bind.JAXBContext;import javax.xml.bind.JAXBException;import javax.xml.bind.Marshaller;import com.rest.servlet.http.xml.Automoveis;import com.rest.servlet.repository.Estoque;@WebServlet("/automoveisXml/*")public class AutomovelServletXml extends HttpServlet{ private static final long serialVersionUID = -638896147519496287L; private Estoque estoque = new Estoque(); private static JAXBContext context; static{ try { context = JAXBContext.newInstance(Automoveis.class); } catch (JAXBException e) { throw new RuntimeException(e); } } protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{ try { Marshaller marshaller = context.createMarshaller(); response.setContentType("application/xml;charset=UTF-8"); PrintWriter out = response.getWriter(); Automoveis automoveis = new Automoveis(); automoveis.setAutomoviesXml(new ArrayList<>(estoque.listarAutomoveis())); marshaller.marshal(automoveis, out); } catch (Exception e) { response.sendError(500, e.getMessage()); } }}Resultado <automoveis><automovel/><automovel/><automovel/><automovel/><automovel/><automovel/></automoveis>***** Este funciona sem utilizar XMLpackage com.rest.servlet.http;import java.io.IOException;import java.io.PrintWriter;import java.util.Collection;import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import com.rest.servlet.model.Automovel;import com.rest.servlet.repository.Estoque;@WebServlet("/automoveis/*")public class AutomovelServlet extends HttpServlet{ //http://localhost:8080/pjRESTfulServlet1/automoveis private static final long serialVersionUID = -638896147519496287L; private Estoque estoque = new Estoque(); public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{ PrintWriter out = response.getWriter(); Collection<Automovel> automoveis = estoque.listarAutomoveis(); for(Automovel auto : automoveis){ out.print(auto); } }}Resultado Ka - FORDGol - WOLKSWAGEMEcoSport - FORD207 - PEUGEOTHB20 - HYUNDAICivic - HONDAESTOQUEpublic class Estoque { private Collection<Automovel> automoveis = new ArrayList<>(); public Collection<Automovel> getAutomoveis() { return automoveis; } public void setAutomoveis(Collection<Automovel> automoveis) { this.automoveis = automoveis; }public Estoque(){ Automovel automovel01 = new Automovel("Ka", "Compacto e economico", Categoria.HATCH, Marca.FORD); Automovel automovel02 = new Automovel("Gol", "Compacto e economico", Categoria.HATCH, Marca.WOLKSWAGEM); Automovel automovel03 = new Automovel("EcoSport", "Espaçoso e aventureiro", Categoria.SUV, Marca.FORD); Automovel automovel04 = new Automovel("207", "Compacto e completo", Categoria.HATCH, Marca.PEUGEOT); Automovel automovel05 = new Automovel("HB20", "Confortável e potente", Categoria.HATCH, Marca.HYUNDAI); Automovel automovel06 = new Automovel("Civic", "Luxuoso por completo", Categoria.SEDAN, Marca.HONDA); automoveis.add(automovel01); automoveis.add(automovel02); automoveis.add(automovel03); automoveis.add(automovel04); automoveis.add(automovel05); automoveis.add(automovel06); } public Collection<Automovel> listarAutomoveis(){ return new ArrayList<>(this.automoveis); } public void adicionarAutomovel(Automovel automovel){ this.automoveis.add(automovel); }}


DESDE JÀ AGRADEÇO!Att,Carl Edwin.https://carledwinj.wordpress.com/

Alexandre Saudate

unread,
Mar 13, 2014, 8:13:40 PM3/13/14
to soa-ap...@googlegroups.com
Olá, Carl, tudo bem?

Você poderia postar as classes Automovel e Automoveis ? Além disso, teria como você postar elas com alguma formatação? Da forma como você postou essas anteriores, eu enxergo todas as classes direto em uma única linha =/

[]'s


Em 13 de março de 2014 20:45, Carl Edwin Antonio Nascimento <carl...@gmail.com> escreveu:
Olá, estou tentando realizar o teste de impressão do xml e o mesmo trás lista vazia. poderia me auxiliar?O meio de comunicação sobre o livro pode ser este ou você indica outro?ESTE Servlet não exibe o xml populado:package com.rest.servlet.http;import java.io.IOException;import java.io.PrintWriter;import java.util.ArrayList;import java.util.Collection;import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import javax.xml.bind.JAXBContext;import javax.xml.bind.JAXBException;import javax.xml.bind.Marshaller;import com.rest.servlet.http.xml.Automoveis;import com.rest.servlet.repository.Estoque;@WebServlet("/automoveisXml/*")public class AutomovelServletXml extends HttpServlet{   private static final long serialVersionUID = -638896147519496287L;      private Estoque estoque = new Estoque();                private static JAXBContext context;             static{         try {                           context = JAXBContext.newInstance(Automoveis.class);                                    } catch (JAXBException e) { throw new RuntimeException(e);              }       }               protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{                            try {                                   Marshaller marshaller = context.createMarshaller();                                             response.setContentType("application/xml;charset=UTF-8");                                               PrintWriter out = response.getWriter();                                         Automoveis automoveis = new Automoveis();                                               automoveis.setAutomoviesXml(new ArrayList<>(estoque.listarAutomoveis()));                                               marshaller.marshal(automoveis, out);                                    } catch (Exception e) {                 response.sendError(500, e.getMessage());                }       }}Resultado <automoveis><automovel/><automovel/><automovel/><automovel/><automovel/><automovel/></automoveis>***** Este funciona sem utilizar XMLpackage com.rest.servlet.http;import java.io.IOException;import java.io.PrintWriter;import java.util.Collection;import javax.servlet.ServletException;import javax.servlet.annotation.WebServlet;import javax.servlet.http.HttpServlet;import javax.servlet.http.HttpServletRequest;import javax.servlet.http.HttpServletResponse;import com.rest.servlet.model.Automovel;import com.rest.servlet.repository.Estoque;@WebServlet("/automoveis/*")public class AutomovelServlet extends HttpServlet{    //http://localhost:8080/pjRESTfulServlet1/automoveis    private static final long serialVersionUID = -638896147519496287L;      private Estoque estoque = new Estoque();                public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException{                               PrintWriter out = response.getWriter();                         Collection<Automovel> automoveis = estoque.listarAutomoveis();                          for(Automovel auto : automoveis){                       out.print(auto);                }       }}Resultado Ka - FORDGol - WOLKSWAGEMEcoSport - FORD207 - PEUGEOTHB20 - HYUNDAICivic - HONDAESTOQUEpublic class Estoque {               private Collection<Automovel> automoveis = new ArrayList<>();           public Collection<Automovel> getAutomoveis() {          return automoveis;      }       public void setAutomoveis(Collection<Automovel> automoveis) {           this.automoveis = automoveis;   }public Estoque(){              Automovel automovel01 = new Automovel("Ka", "Compacto e economico", Categoria.HATCH, Marca.FORD);               Automovel automovel02 = new Automovel("Gol", "Compacto e economico", Categoria.HATCH, Marca.WOLKSWAGEM);                Automovel automovel03 = new Automovel("EcoSport", "Espaçoso e aventureiro", Categoria.SUV, Marca.FORD);         Automovel automovel04 = new Automovel("207", "Compacto e completo", Categoria.HATCH, Marca.PEUGEOT);            Automovel automovel05 = new Automovel("HB20", "Confortável e potente", Categoria.HATCH, Marca.HYUNDAI);         Automovel automovel06 = new Automovel("Civic", "Luxuoso por completo", Categoria.SEDAN, Marca.HONDA);                           automoveis.add(automovel01);            automoveis.add(automovel02);            automoveis.add(automovel03);            automoveis.add(automovel04);            automoveis.add(automovel05);            automoveis.add(automovel06);    }                               public Collection<Automovel> listarAutomoveis(){                return new ArrayList<>(this.automoveis);        }               public void adicionarAutomovel(Automovel automovel){            this.automoveis.add(automovel); }}


DESDE JÀ AGRADEÇO!Att,Carl Edwin.https://carledwinj.wordpress.com/

--
Você está recebendo esta mensagem porque se inscreveu no grupo "SOA Aplicado" dos Grupos do Google.
Para cancelar a inscrição neste grupo e parar de receber seus e-mails, envie um e-mail para soa-aplicado...@googlegroups.com.
Para postar neste grupo, envie um e-mail para soa-ap...@googlegroups.com.
Visite este grupo em http://groups.google.com/group/soa-aplicado.
Para obter mais opções, acesse https://groups.google.com/d/optout.

Carl Edwin Antonio Nascimento

unread,
Mar 17, 2014, 7:57:34 PM3/17/14
to soa-ap...@googlegroups.com

Ola Alexandre!

Muito obrigado por ter respondido, segue o projeto em anexo.

pacote com.rest.servlet.http.xml;

java.util.ArrayList importação;
java.util.List importação;

javax.xml.bind.annotation.XmlElement importação;
javax.xml.bind.annotation.XmlRootElement importação;

com.rest.servlet.model.Automovel importação;

@ XmlRootElement
public class {Automoveis

Lista privada <Automovel> automoviesXml = new ArrayList <> ();

@ XmlElement (name = "automovel")
Lista pública <Automovel> getAutomoviesXml () {
voltar automoviesXml;
}

setAutomoviesXml public void (Lista <Automovel> automoviesXml) {
this.automoviesXml = automoviesXml;
}
}






pacote com.rest.servlet.model;

public class Automovel {
nome_auto private String;
descricao_auto private String;
categoria_auto Categoria privado;
marca_auto Marca privado;
public enum Categoria {
ENTRADA, HATCH, SEDAN, SW, monovolume, GRANDCAB, ESPORTES, pickup, furgão, SUV
}

public enum {Marca
FORD, WOLKSWAGEM, FIAT, TOYOTA, SUZUKI, SUBARU, a SMART, RENAULT, VOLVO, PORSCHE, Peugeot, Nissan, Misubishi, MERCEDES, KIA, HONDA, HYUNDAI, Chevrolet, BMW 
}
Automovel pública () {}
Automovel pública (String nome_auto, String descricao_auto, Categoria categoria_auto, Marca marca_auto) {
this.nome_auto = nome_auto;
this.descricao_auto = descricao_auto;
this.categoria_auto = categoria_auto;
this.marca_auto = marca_auto;
}
public String toString () {
retornar this.nome_auto + "-" + this.marca_auto;
}
}





Em quinta-feira, 13 de março de 2014 21h13min40s UTC-3, Alexandre Saudate escreveu:
Olá, Carl, tudo bem?

Você. poderia POSTAR como classes Automovel e Automoveis? Disso de Além, térios Como Você. POSTAR ELAS com alguma Formatação? Da forma Como Você. postou essas Anteriores, eu enxergo como classes Todas Direto los UMA Única Linha = /

[] 'S


Em 13 de março de 2014 20:45, Carl Edwin Antonio Nascimento < carl ... @ gmail.com > escreveu:
Olá, ESTOU tentando Realizar o teste de IMPRESSÃO fazer xml EO MESMO Trás Lista Vazia. poderia me auxiliar O Meio de Comunicação sobre o Livro PODE serviços Este UO Você. indica Outro ESTE Servlet Localidade: Não exibe o xml populado:? pacote com.rest.servlet.http; import java.io.IOException; java.io.PrintWriter importação; import java.util.ArrayList; java.util.Collection importação;. javax.servlet importação ServletException; javax.servlet.annotation importação. WebServlet; javax.servlet.http importação. HttpServlet; javax.servlet.http importação. HttpServletRequest; javax importação . servlet.http. HttpServletResponse; javax.xml.bind.JAXBContext importação; javax.xml.bind.JAXBException importação; javax.xml.bind.Marshaller importação; . com.rest.servlet.http.xml importação Automoveis; com importação. rest.servlet.repository. Estoque de; @ WebServlet ("/ automoveisXml / * ") public class AutomovelServletXml estende HttpServlet {private static final = serialVersionUID longa-638896147519496287L; Estoque de Estoque de private = new Estoque de (); contexto JAXBContext estática privado; static {try {context = JAXBContext.newInstance ( Automoveis.class);} catch (JAXBException e) {throw new RuntimeException (e);}} protected void doGet (HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {try {Marshaller marshaller = contexto. createMarshaller (); response.setContentType (" application / xml; charset = UTF-8 " ); PrintWriter out = response.getWriter (); Automoveis automoveis = new Automoveis (); automoveis.setAutomoviesXml ( new ArrayList <> (estoque. listarAutomoveis ())); marshaller.marshal (automoveis, fora);} catch (Exception e) {response.sendError (500, e.getMessage ());}}} Resultado Este Funciona SEM utilizar XMLpackage com.rest.servlet.http; import java.io.IOException; java.io.PrintWriter importação; java.util.Collection importação;. Javax.servlet importação ServletException; javax.servlet.annotation importação. WebServlet; importar javax.servlet.http. HttpServlet; javax.servlet.http importação. HttpServletRequest; javax.servlet.http importação. HttpServletResponse;. com.rest.servlet.model importação Automovel;. com.rest.servlet.repository importação Estoque de; @ WebServlet ("/ automoveis / * ") public class AutomovelServlet estende HttpServlet {/ / http://localhost:8080/ pjRESTfulServlet1/automoveis    private static serialVersionUID longo final =-638896147519496287L; Estoque de Estoque de private = new Estoque de (); public void doGet ( HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {PrintWriter out = response.getWriter (); Coleção <Automovel> automoveis = estoque.listarAutomoveis (); for (Automovel auto: AUTOMOVEIS) {out.print (automático);}}} Resultado Ka - FORDGol - WOLKSWAGEMEcoSport - FORD207 - PEUGEOTHB20 - HYUNDAICivic - HONDAESTOQUEpublic classe Estoque de {private Coleção <Automovel> automoveis = new ArrayList <> (); Coleção público <Automovel> getAutomoveis () {return automoveis;} setAutomoveis public void (Collection < Automovel> AUTOMOVEIS) {this.automoveis = automoveis;} Estoque de público () {Automovel automovel01 = new Automovel ("Ka", "Compacto e economico", Categoria.HATCH, Marca.FORD); Automovel automovel02 = new Automovel ("Gol "," Compacto e economico ", Categoria.HATCH, Marca.WOLKSWAGEM); Automovel automovel03 = new Automovel (" EcoSport "," Espaçoso e aventureiro ", Categoria.SUV, Marca.FORD); Automovel automovel04 = new Automovel (" 207 "," Compacto e completo ", Categoria.HATCH, Marca.PEUGEOT); Automovel automovel05 = new Automovel (" HB20 "," Confortável e potente ", Categoria.HATCH, Marca.HYUNDAI); Automovel automovel06 = new Automovel (" Civic "," Luxuoso POR completo ", Categoria.SEDAN, Marca.HONDA); automoveis.add (automovel01); automoveis.add (automovel02); automoveis.add (automovel03); automoveis.add (automovel04); automoveis.add (automovel05 ); automoveis.add (automovel06);} Coleção público <Automovel> listarAutomoveis () {return new ArrayList <> (this.automoveis);} public void adicionarAutomovel (Automovel automovel) {this.automoveis.add ( automovel);}} DESDE JA AGRADEÇO Att, Carl Edwin!. https://carledwinj. wordpress.com / - . Voce esta recebendo ESTA MENSAGEM o Porque se inscreveu nenhum Grupo "SOA Aplicado" dos Grupos do Google fazer Pará Cancelar um INSCRIÇÃO Neste Grupo e Parar de receber SEUs e-mails, envie e-mail hum parágrafo soa-aplicado ... @ googlegroups.com . Pará POSTAR Neste Grupo, envie e-mail hum parágrafo soa-ap ... @ googlegroups.com . Visite Este Grupo los http: / / groups.google.com / group / soa-aplicado . Para obter Mais opções, Acessem https://groups.google.com/d/ optout .











pjRESTfulServlet1.war

Alexandre Saudate

unread,
Mar 18, 2014, 9:48:24 AM3/18/14
to soa-ap...@googlegroups.com
Carl,

Só está faltando, na sua classe "Automovel", getters e setters para os atributos que você quer expôr como XML (ou a anotação @XmlAccessorType(FIELD) na classe). 

Alerto para o fato de que existem diversos erros conceituais na sua classe. Se você quiser saber mais sobre REST, leia o cap. 5 do livro ou compre meu outro livro  "REST: Construa API's inteligentes de maneira simples".

[]'s


--
Você recebeu essa mensagem porque está inscrito no grupo quot;SOA Aplicado" dos Grupos do Google.
Para cancelar inscrição nesse grupo e parar de receber e-mails dele, envie um e-mail para soa-aplicado...@googlegroups.com.
Para postar nesse grupo, envie um e-mail para soa-ap...@googlegroups.com.
Para mais opções, acesse https://groups.google.com/d/optout.

Reply all
Reply to author
Forward
0 new messages