Dúvida JAX-RS

0 respostas
R

Galera,

estou me baseando no tutorial da Caelum pra invocar um webservice via REST:

@XmlRootElement
public class Pedido {

	Integer id;
	String descricao;
	
	public Integer getId() {
		return id;
	}
	public void setId(Integer id) {
		this.id = id;
	}
	public String getDescricao() {
		return descricao;
	}
	public void setDescricao(String descricao) {
		this.descricao = descricao;
	}
	public Pedido(){}
	public Pedido(Integer id,String descricao){
		this.id  = id;
		this.descricao = descricao;
	}

Webservice:

@Path("/pedido/{id}")
public class Busca {
	
	@GET
	@Produces( { MediaType.APPLICATION_XML })
	 public Pedido getPedidoById(@PathParam("id") Integer id) {
	  Pedido p1 = new Pedido(1,"Descricao 1");
	  Pedido p2 = new Pedido(2,"Teste 2");
	  Map<Integer,Pedido> mapa = new HashMap<Integer, Pedido>();
	  mapa.put(p1.getId(), p1);
	  mapa.put(p2.getId(), p2);
	  
	  return mapa.get(id);
	 }

}

Classe p/teste:

public class Main {

	public static void main(String[] args) throws HttpException, IOException {
		try {
			HttpClient httpClient  = new HttpClient();
			GetMethod httpMethod=  new GetMethod("http://localhost:8080/autenticacao/busca/pedido/1");
			httpMethod.addRequestHeader("Accept", "application/xml");
			httpClient.executeMethod(httpMethod);
			Scanner scan =
			      new Scanner(httpMethod.getResponseBodyAsStream());
			    PrintStream ps = System.out;
			    while (scan.hasNext()) {
			      ps.println(scan.nextLine());
			    }
			
		}catch(Exception e){
			e.printStackTrace();
		}

	}
}

Minha dúvida é sem tem como parsear essa resposta automaticamente em um objeto,sem precisar usar um Xstream da vida.

Criado 22 de junho de 2012
Respostas 0
Participantes 1