problem with JSON when you flash an id

0

Esteemed I introduce myself to this great community, I always look for help here but it is the first time that I am forced to publish an application, since I thank those who can respinderme.

My problem is that I'm getting this error when running a JSON

  

javax.json.bind.JsonbException: Error getting value on:   root.persistence.entities.Product [idproductos = 1]

and I do not understand very well why, I'm working with netbeans but deploying in heroku to a postgress base the tests I'm doing with postman

my persistence file is as follows.

    /*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package root.persistence.entities;

import java.io.Serializable;
import java.math.BigInteger;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.Id;
import javax.persistence.NamedQueries;
import javax.persistence.NamedQuery;
import javax.persistence.Table;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import javax.xml.bind.annotation.XmlRootElement;

/**
 *
 * @author rodrigo
 */
@Entity
@Table(name = "productos")
@XmlRootElement
@NamedQueries({
    @NamedQuery(name = "Producto.findAll", query = "SELECT p FROM Producto p")
    , @NamedQuery(name = "Producto.findByIdproductos", query = "SELECT p FROM Producto p WHERE p.idproductos = :idproductos")
    , @NamedQuery(name = "Producto.findByNombre", query = "SELECT p FROM Producto p WHERE p.nombre = :nombre")
    , @NamedQuery(name = "Producto.findByDetalle", query = "SELECT p FROM Producto p WHERE p.detalle = :detalle")
    , @NamedQuery(name = "Producto.findByPrecio", query = "SELECT p FROM Producto p WHERE p.precio = :precio")
    , @NamedQuery(name = "Producto.findByDescuento", query = "SELECT p FROM Producto p WHERE p.descuento = :descuento")})
public class Producto implements Serializable {

    private static final long serialVersionUID = 1L;
    @Id
    @Basic(optional = false)
    @NotNull
    @Column(name = "idproductos")
    private Integer idproductos;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 2147483647)
    @Column(name = "nombre")
    private String nombre;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 2147483647)
    @Column(name = "detalle")
    private String detalle;
    @Basic(optional = false)
    @NotNull
    @Column(name = "precio")
    private BigInteger precio;
    @Column(name = "descuento")
    private BigInteger descuento;

    public Producto() {
    }

    public Producto(Integer idproductos) {
        this.idproductos = idproductos;
    }

    public Producto(Integer idproductos, String nombre, String detalle, BigInteger precio) {
        this.idproductos = idproductos;
        this.nombre = nombre;
        this.detalle = detalle;
        this.precio = precio;
    }

    public Integer getIdproductos() {
        return idproductos;
    }

    public void setIdproductos(Integer idproductos) {
        this.idproductos = idproductos;
    }

    public String getNombre() {
        return nombre;
    }

    public void setNombre(String nombre) {
        this.nombre = nombre;
    }

    public String getDetalle() {
        return detalle;
    }

    public void setDetalle(String detalle) {
        this.detalle = detalle;
    }

    public BigInteger getPrecio() {
        return precio;
    }

    public void setPrecio(BigInteger precio) {
        this.precio = precio;
    }

    public BigInteger getDescuento() {
        return descuento;
    }

    public void setDescuento(BigInteger descuento) {
        this.descuento = descuento;
    }

    @Override
    public int hashCode() {
        int hash = 0;
        hash += (idproductos != null ? idproductos.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 Producto)) {
            return false;
        }
        Producto other = (Producto) object;
        if ((this.idproductos == null && other.idproductos != null) || (this.idproductos != null && !this.idproductos.equals(other.idproductos))) {
            return false;
        }
        return true;
    }

    @Override
    public String toString() {
        return "root.persistence.entities.Producto[ idproductos=" + idproductos + " ]";
    }

    public String getProducto() {
        throw new UnsupportedOperationException("Not supported yet."); //To change body of generated methods, choose Tools | Templates.
    }

}

and the call to the API I generate it from the Product class that is this

package root.services;

import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;
import javax.ws.rs.Consumes;
import javax.ws.rs.DELETE;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.PUT;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import root.persistence.entities.Producto;

@Path("/productos")
public class ProductosREST {

    EntityManagerFactory emf = Persistence.createEntityManagerFactory("UP_Productos");
    EntityManager em;

    @GET
    @Produces(MediaType.APPLICATION_JSON)
    public Response listarTodo(){
        try{
        em =emf.createEntityManager();

        List<Producto> lista = em.createNamedQuery("Producto.findAll").getResultList();


        return Response.ok().entity(lista).build();
        }catch (Exception e) {

        } finally {
            em.close();
                    return Response.ok(500).entity("Error- no se logro listar los productos").build();

        }
    }

    @GET
    @Path("/{idbuscar}")
    @Produces(MediaType.APPLICATION_JSON)
    public Response buscarId(@PathParam("idbuscar") Integer idbuscar) {
        try {
            em = emf.createEntityManager();
            Producto producto = em.find(Producto.class, idbuscar);
            return Response.ok(producto).build();
        } catch (Exception e) {

        } finally {
            em.close();
        }

        return Response.ok(500).entity("Error- no se logro buscar el producto por id").build();
    }
    @POST
    @Consumes(MediaType.APPLICATION_JSON)
    public String nuevo(Producto productoNuevo) {

        try {
            em = emf.createEntityManager();
            em.getTransaction().begin();
            em.persist(productoNuevo);
            em.getTransaction().commit();

            return "Producto Guardado";
        } catch (Exception e) {
            return "Error al guardar el Producto: ";
        } finally {
            em.close();
        }

    }

    @PUT
    public Response actualizar(Producto productoUpdate) {
        String resultado = null;

        try {
            em = emf.createEntityManager();
            em.getTransaction().begin();
            productoUpdate = em.merge(productoUpdate);
            em.getTransaction().commit();
            return Response.ok("Producto actualizado OK").build();
        } catch (Exception e) {

            String id = productoUpdate.getProducto();

            if (id == null) {
                return Response.ok("no existe id de producto").build();
            }

        } finally {

            if (em != null) {
                em.close();
            }
        }
        return Response.ok(resultado).build();
    }
      @DELETE
    @Path("/{iddelete}")
    @Produces({MediaType.TEXT_PLAIN, MediaType.APPLICATION_JSON})
    public Response eliminaId(@PathParam("iddelete") Integer iddelete) {

        try { 

            em = emf.createEntityManager();
            em.getTransaction().begin();
            Producto productoEliminar = em.getReference(Producto.class, iddelete);
            em.remove(productoEliminar);
            em.getTransaction().commit();
            return Response.ok("Producto eliminado").build();
        } catch (Exception e) {

            return Response.ok("Error al eliminar").build();
        } finally {
            em.close();
        }

    }
}

and my persistence xml I have configured it like this.

I have tried several ways but the error persists, since I thank you for your invaluable help.

    
asked by Rodrigo 02.12.2018 в 16:59
source

0 answers