Galera blz?
Será que alguém poderia dar uma força, estou aprendendo sobre WebService, e está retornando o seguinte erro: do GlassFish
MessageBodyWriter not found for media type=application/json, type=class java.util.ArrayList, genericType=class java.util.ArrayList.
segue abaixo minhas classes, estou usando Jersey 1.17
WebService:
package br.com.ophos.services;
import br.com.ophos.dao.ClienteDAO;
import br.com.ophos.dao.DAOException;
import br.com.ophos.dao.DAOFactory;
import br.com.ophos.mobile.model.Cliente;
import java.util.List;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.UriInfo;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.Produces;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PUT;
import javax.ws.rs.PathParam;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
/**
* REST Web Service
*
* @author Victor Oliveira
*/
@Path("cliente")
public class WebService {
@Context
private UriInfo context;
private ClienteDAO dao = DAOFactory.getClienteDAO();
public WebService() {
}
@GET
@Produces(MediaType.APPLICATION_JSON)
public String getJson() {
//TODO return proper representation object
throw new UnsupportedOperationException();
}
@Path("list")
@GET
@Produces(MediaType.APPLICATION_JSON)
public Response list() {
try {
List<Cliente> cliente = (List<Cliente>) dao.list();
if (cliente == null || cliente.isEmpty()) {
return Response.status(Response.Status.NOT_FOUND).build();
}
return Response.ok(cliente).build();
} catch (Exception e) {
return Response.serverError().build();
}
}
@GET
@Path("/find/{Id}")
@Consumes(MediaType.TEXT_PLAIN)
@Produces(MediaType.APPLICATION_JSON)
public Response Find(@PathParam("Id") Long Id) {
try {
Cliente obj = dao.find(Id);
if (obj == null) {
return Response.status(Response.Status.NOT_FOUND).build();
}
return Response.ok(obj).build();
} catch (Exception e) {
return Response.serverError().build();
}
}
@POST
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.TEXT_PLAIN)
public Response Create(Cliente obj) {
try {
obj.setId(obj.getId());
obj.setNome(obj.getNome());
dao.create(obj);
return Response.status(Response.Status.OK).build();
} catch (DAOException ex) {
return Response.serverError().build();
}
}
@POST
@Path("/update/{id}")
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.TEXT_PLAIN)
public Response Update(Cliente obj, @PathParam("id") long id) {
try {
dao.update(obj);
return Response.status(Response.Status.OK).build();
} catch (DAOException ex) {
return Response.serverError().build();
}
}
@Path("/delete/{id}")
@DELETE
@Consumes(MediaType.APPLICATION_JSON)
@Produces(MediaType.TEXT_PLAIN)
public Response Delete(@PathParam("id") Long id) {
try {
dao.delete(id);
return Response.status(Response.Status.OK).build();
} catch (DAOException ex) {
return Response.serverError().build();
}
}
/**
* PUT method for updating or creating an instance of WebService
*
* @param content representation for the resource
*/
@PUT
@Consumes(MediaType.APPLICATION_JSON)
public void putJson(String content) {
}
}
Classe DAO:
package br.com.ophos.dao.impl;
import br.com.ophos.dao.ClienteDAO;
import br.com.ophos.dao.DAOException;
import br.com.ophos.mobile.model.Cliente;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.logging.Level;
import java.util.logging.Logger;
/**
*
* @author Victor Oliveira
*/
public class ClienteDAOImpl implements ClienteDAO {
private Connection conn;
private void open() throws ClassNotFoundException, SQLException {
Class.forName("oracle.jdbc.OracleDriver");
String dbURL = "jdbc:oracle:thin:@localhost:1521:XE";
String username = "System";
String password = "194!@#";
conn = DriverManager.getConnection(dbURL, username, password);
}
private void close() {
if (conn != null) {
try {
conn.close();
} catch (SQLException ex) {
Logger.getLogger(ClienteDAOImpl.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
@Override
public Collection<Cliente> list() throws DAOException {
List<Cliente> lista = new ArrayList<>();
try {
open();
try (PreparedStatement stmt = conn.prepareStatement("SELECT * FROM CLIENTE");
ResultSet rs = stmt.executeQuery()) {
while (rs.next()) {
// criando o objeto Contato
Cliente cliente = new Cliente();
cliente.setId(rs.getLong("ID"));
cliente.setNome(rs.getString("NOME"));
lista.add(cliente);
}
}
return lista;
} catch (SQLException | ClassNotFoundException e) {
throw new DAOException("Ocorreu um erro ao consultar os clientes. Erro: " + e.getMessage());
} finally {
close();
}
}
@Override
public Cliente find(Long id) throws DAOException {
try {
open();
try (PreparedStatement stmt = conn.prepareStatement("SELECT * FROM CLIENTE WHERE ID = ?")) {
stmt.setLong(1, id.longValue());
ResultSet rs = stmt.executeQuery();
while (rs.next()) {
// criando o objeto Contato
Cliente cliente = new Cliente();
cliente.setId(rs.getLong("ID"));
cliente.setNome(rs.getString("NOME"));
return cliente;
}
return null;
}
} catch (ClassNotFoundException | SQLException ex) {
throw new DAOException("Ocorreu um erro ao consultar o cliente. Erro: " + ex.getMessage());
} finally {
close();
}
}
@Override
public void create(Cliente obj) throws DAOException {
try {
// autoincremento
open();
PreparedStatement stat;
String SQLInsert = "INSERT INTO CLIENTE (ID, NOME) VALUES (?, ?)";
stat = conn.prepareStatement(SQLInsert);
stat.setLong(1, obj.getId());
stat.setString(2, obj.getNome());
stat.executeUpdate();
} catch (SQLException | ClassNotFoundException ex) {
throw new DAOException("Ocorreu um erro ao criar o cliente. Erro: " + ex.getMessage());
} finally {
close();
}
}
@Override
public void update(Cliente obj) throws DAOException {
try {
open();
PreparedStatement stat;
String SQLUpdate = "UPDATE CLIENTE SET NOME = ? WHERE ID = ? ";
stat = conn.prepareStatement(SQLUpdate);
stat.setString(1, obj.getNome());
stat.setLong(2, obj.getId());
stat.execute();
close();
} catch (ClassNotFoundException | SQLException ex) {
throw new DAOException("Ocorreu um erro ao atualizar o cliente. Erro: " + ex.getMessage());
} finally {
close();
}
}
@Override
public void delete(Long id) throws DAOException {
try {
open();
PreparedStatement stat;
String SQLDelete = "DELETE FROM CLIENTE WHERE ID = ?";
stat = conn.prepareStatement(SQLDelete);
stat.setLong(1, id);
stat.execute();
} catch (SQLException | ClassNotFoundException ex) {
throw new DAOException("Ocorreu um erro ao apagar o cliente. Erro: " + ex.getMessage());
} finally {
close();
}
}
}
POJO:
package br.com.ophos.mobile.model;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
public class Cliente {
private Long id;
private String nome;
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;
}
}
Agradeço quem puder dar uma força.