Pessoal, estou com um problema ao tentar retornar um objeto qualquer em um WebService que utilize Jersey + Jettison, seja um ArrayList ou um objeto do tipo “Pessoa” por exemplo.
Fiz uma aplicação de teste, mas não funciona retornando um “Internal Server Error”. Mas caso trocar o tipo de retorno para String ou int por exemplo, ele retorna normalmente. Não estou usando o web.xml para configuração. Olha o código:
br.com.teste.Pessoa.java
import java.util.Objects;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class Pessoa {
private String nome;
private int idade;
private String cpf;
private int id;
public Pessoa(int id,String nome, int idade, String cpf) {
this.nome = nome;
this.idade = idade;
this.cpf = cpf;
this.id = id;
}
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
public int getIdade() {
return idade;
}
public void setIdade(int idade) {
this.idade = idade;
}
public String getCpf() {
return cpf;
}
public void setCpf(String cpf) {
this.cpf = cpf;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
@Override
public int hashCode() {
int hash = 3;
hash = 89 * hash + Objects.hashCode(this.nome);
hash = 89 * hash + this.idade;
hash = 89 * hash + Objects.hashCode(this.cpf);
hash = 89 * hash + this.id;
return hash;
}
@Override
public boolean equals(Object obj) {
if (obj == null) {
return false;
}
if (getClass() != obj.getClass()) {
return false;
}
final Pessoa other = (Pessoa) obj;
if (!Objects.equals(this.nome, other.nome)) {
return false;
}
if (this.idade != other.idade) {
return false;
}
if (!Objects.equals(this.cpf, other.cpf)) {
return false;
}
if (this.id != other.id) {
return false;
}
return true;
}
@Override
public String toString() {
return "Pessoa{" + "nome=" + nome + ", idade=" + idade + ", cpf=" + cpf + ", id=" + id + '}';
}
}
br.com.teste.ApplicationConfig.java
import java.util.Set;
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;
import org.glassfish.jersey.jettison.JettisonFeature;
@ApplicationPath("servico")
public class ApplicationConfig extends Application{
@Override
public Set<Class<?>> getClasses() {
Set<Class<?>> resources = new java.util.HashSet<>();
addRestResourceClasses(resources);
resources.add(JettisonFeature.class);
return resources;
}
private void addRestResourceClasses(Set<Class<?>> resources) {
resources.add(br.com.teste.PessoaResource.class);
}
}
br.com.teste.PessoaResource.java
import br.edu.ifpr.modelo.Pessoa;
import java.util.ArrayList;
import java.util.List;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
@Path("/pessoas")
public class PessoaResource {
private List<Pessoa> pessoas = new ArrayList<Pessoa>();
public PessoaResource(){
popular();
}
private void popular(){
pessoas.add(new Pessoa(1,"Pessoa 01", 20, "021.021.021-01"));
pessoas.add(new Pessoa(2,"Pessoa 02", 21, "022.022.022-02"));
pessoas.add(new Pessoa(3,"Pessoa 03", 22, "023.023.023-03"));
pessoas.add(new Pessoa(4,"Pessoa 04", 23, "024.024.024-04"));
}
@GET
@Produces(MediaType.APPLICATION_JSON)
public String get(){
return pessoas.toString();
}
@GET
@Produces(MediaType.APPLICATION_JSON)
@Path("{id}")
public Pessoa get(@PathParam("id") int id){
for(Pessoa p : pessoas){
if(p.getId() == id){
System.out.println(p.getId());
return p;
}
}
return null;
}
}
Somente para testar, mas mesmo assim não funciona. Já tentei pesquisar em vários lugares mas não achei a solução. Não sei se pode ser alguma configuração errada que fiz pela IDE, achei exemplos usando pelo web.xml mas os exemplos passados em aula não utilizavam-o. Os jars que adicionei foram:
Obrigado a todos.