Pessoal, estou fazendo um webservice, mas estou pegando um HTTP Status 500!
ClienteController
package Controller;
import Dao.ClienteDao;
import Model.Cliente;
import java.util.ArrayList;
import java.util.List;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.SessionScoped;
@ManagedBean
@SessionScoped
public class ClienteController {
public List<Cliente> buscarTodos() throws ClassNotFoundException {
ClienteDao clienteDAO = new ClienteDao();
//return clienteDAO.buscarTodos();
return (ArrayList<Cliente>) clienteDAO.buscarTodos();
//return clienteDAO.buscarTodos();
}
}
ClienteDao
package Dao;
import Interface.ClienteInterface;
import Model.Cliente;
import Util.HibernateUtil;
import java.util.List;
import org.hibernate.Session;
import org.hibernate.Transaction;
public class ClienteDao implements ClienteInterface {
@Override
public List<Cliente> buscarTodos() {
Session session = HibernateUtil.getSessionFactory().openSession();
Transaction t = session.beginTransaction();
List lista = session.createQuery("From Cliente").list();
t.commit();
return lista;
}
}
ClienteInterface
package Interface;
import Model.Cliente;
import java.util.List;
/**
*
* @author jr
*/
public interface ClienteInterface {
public List<Cliente>buscarTodos();
}
ClienteResource
package Resources;
import Controller.ClienteController;
import Model.Cliente;
import com.google.gson.Gson;
import java.util.List;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
@Path("/cliente")
public class ClienteResource {
@GET
@Path("/buscarTodos")
@Produces("application/json")
public List<Cliente> selTodos() throws ClassNotFoundException{
return new ClienteController().buscarTodos();
}
@GET
@Path("/buscarTodosGSON")
@Produces("application/json")
public String selTodosGSON() throws ClassNotFoundException{
return new Gson().toJson(new ClienteController().buscarTodos());
}
}
HibernateUtil
package Util;
import Model.Cliente;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.tool.hbm2ddl.SchemaExport;
public class HibernateUtil {
private static SessionFactory sessionFactory;
private HibernateUtil() {
}
public static SessionFactory getSessionFactory() {
if (sessionFactory == null) {
try {
AnnotationConfiguration ac = new AnnotationConfiguration();
ac.addAnnotatedClass(Cliente.class);
sessionFactory = ac.configure().buildSessionFactory();
SchemaExport se = new SchemaExport(ac);
se.create(true, true);
} catch (Throwable ex) {
// Log the exception.
System.err.println("===================Initial SessionFactory creation failed.==============================" + ex);
throw new ExceptionInInitializerError(ex);
}
return sessionFactory;
} else {
return sessionFactory;
}
}
public static void main(String[] args) {
HibernateUtil.getSessionFactory();
}
}
Cliente
package Model;
import java.io.Serializable;
import javax.persistence.*;
import javax.validation.constraints.NotNull;
import javax.xml.bind.annotation.XmlRootElement;
/**
*
* @author jr
*/
@Entity
@Table(name = "Cliente", catalog = "sisau", schema = "sisau")
@XmlRootElement
@NamedQueries({
@NamedQuery(name = "Cliente.findAll", query = "SELECT c FROM Cliente c"),
@NamedQuery(name = "Cliente.findById", query = "SELECT c FROM Cliente c WHERE c.id = :id"),
@NamedQuery(name = "Cliente.findByNome", query = "SELECT c FROM Cliente c WHERE c.nome = :nome"),
@NamedQuery(name = "Cliente.findByCpf", query = "SELECT c FROM Cliente c WHERE c.cpf = :cpf"),
@NamedQuery(name = "Cliente.findByEndereco", query = "SELECT c FROM Cliente c WHERE c.endereco = :endereco")})
public class Cliente implements Serializable {
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Basic(optional = false)
@NotNull
@Column(name = "id", nullable = false)
private Long id;
@Column(name = "nome")
private String nome;
@Column(name = "cpf")
private String cpf;
@Column(name = "endereco")
private String endereco;
public Cliente() {
}
public Cliente(Long id) {
this.id = id;
}
public Long getId() {
return id;
}
public void setId(Long id) {
this.id = id;
}
public String getNome() {
return nome;
}
public void setNome(String nome) {
this.nome = nome;
}
public String getCpf() {
return cpf;
}
public void setCpf(String cpf) {
this.cpf = cpf;
}
public String getEndereco() {
return endereco;
}
public void setEndereco(String endereco) {
this.endereco = endereco;
}
@Override
public int hashCode() {
int hash = 0;
hash += (id != null ? id.hashCode() : 0);
return hash;
}
@Override
public boolean equals(Object object) {
// TODO: Warning - this method won't work in the case the id fields are not set
if (!(object instanceof Cliente)) {
return false;
}
Cliente other = (Cliente) object;
if ((this.id == null && other.id != null) || (this.id != null && !this.id.equals(other.id))) {
return false;
}
return true;
}
@Override
public String toString() {
// TODO Auto-generated method stub
return "ID: " + this.id +
"\nNome: " + this.nome +
"\nCPF: " + this.cpf +
"\nEndereço: " + this.endereco;
}
}
package Dao;
import java.util.List;
import org.junit.*;
import static org.junit.Assert.*;
/**
*
* @author jr
*/
public class ClienteDaoTest {
public ClienteDaoTest() {
}
@BeforeClass
public static void setUpClass() throws Exception {
}
@AfterClass
public static void tearDownClass() throws Exception {
}
@Before
public void setUp() {
}
@After
public void tearDown() {
}
/**
* Test of buscarTodos method, of class ClienteDao.
*/
@Test
public void testBuscarTodos() {
System.out.println("buscarTodos");
ClienteDao instance = new ClienteDao();
List expResult = null;
List result = instance.buscarTodos();
assertEquals(expResult, result);
// TODO review the generated test code and remove the default call to fail.
fail("TESTE FALHOU");
}
}
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE hibernate-configuration PUBLIC "-//Hibernate/Hibernate Configuration DTD 3.0//EN" "http://hibernate.sourceforge.net/hibernate-configuration-3.0.dtd">
<hibernate-configuration>
<session-factory>
<property name="hibernate.dialect">org.hibernate.dialect.PostgreSQLDialect</property>
<property name="hibernate.connection.driver_class">org.postgresql.Driver</property>
<property name="hibernate.connection.url">jdbc:postgresql://localhost:5432/sisau</property>
<property name="hibernate.connection.username">root</property>
<property name="hibernate.connection.password">debian23</property>
<property name="hibernate.default_schema">sisau</property>
<property name="hibernate.show_sql">true</property>
<property name="hibernate.use_sql_comments">true</property>
<property name="hibernate.format_sql">true</property>
<property name="hibernate.hbm2ddl.auto">update</property>
<mapping class="Model.Cliente"/>
</session-factory>
</hibernate-configuration>