如何使用格式为json的web服务restful将映像发送到mysql

时间:2016-01-12 17:50:04

标签: java mysql json rest

我不知道是谁将图像服务器发送到Web服务,使用json,我的发送方法:

@POST
    @Path("imagenGuardar")
    @Consumes({("application/json")})
    @Produces("text/plain")
    public int guarda(Imagenes entity) {
        em.persist(entity);
        return entity.getIdImagenes();
    }

我的实体来自数据库:

import java.io.Serializable;
import javax.persistence.Basic;
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.JoinColumn;
import javax.persistence.Lob;
import javax.persistence.ManyToOne;
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 Valar_Morgulis
 */
@Entity
@Table(name = "imagenes")
@XmlRootElement
@NamedQueries({
    @NamedQuery(name = "Imagenes.findAll", query = "SELECT i FROM Imagenes i"),
    @NamedQuery(name = "Imagenes.findByIdImagenes", query = "SELECT i FROM Imagenes i WHERE i.idImagenes = :idImagenes"),
    @NamedQuery(name = "Imagenes.findByDescripcion", query = "SELECT i FROM Imagenes i WHERE i.descripcion = :descripcion")})
public class Imagenes implements Serializable {
    private static final long serialVersionUID = 1L;
    @Id
    @GeneratedValue(strategy = GenerationType.IDENTITY)
    @Basic(optional = false)
    @Column(name = "ID_IMAGENES")
    private Integer idImagenes;
    @Basic(optional = false)
    @NotNull
    @Size(min = 1, max = 45)
    @Column(name = "DESCRIPCION")
    private String descripcion;
    @Lob
    @Column(name = "IMAGEN")
    private byte[] imagen;
    @JoinColumn(name = "ID_ACTIVO", referencedColumnName = "ID_ACTIVO")
    @ManyToOne(optional = false)
    private Activo idActivo;

    public Imagenes() {
    }

    public Imagenes(Integer idImagenes) {
        this.idImagenes = idImagenes;
    }

    public Imagenes(Integer idImagenes, String descripcion) {
        this.idImagenes = idImagenes;
        this.descripcion = descripcion;
    }

    public Integer getIdImagenes() {
        return idImagenes;
    }

    public void setIdImagenes(Integer idImagenes) {
        this.idImagenes = idImagenes;
    }

    public String getDescripcion() {
        return descripcion;
    }

    public void setDescripcion(String descripcion) {
        this.descripcion = descripcion;
    }

    public byte[] getImagen() {
        return imagen;
    }

    public void setImagen(byte[] imagen) {
        this.imagen = imagen;
    }

    int activo;
    public int getIdActivo() {
        return idActivo.getIdActivo();
    }

    public void setIdActivo(int idActivo) {
        this.activo = idActivo;
    }

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

    @Override
    public String toString() {
        return "activo.entities.Imagenes[ idImagenes=" + idImagenes + " ]";
    }

}

并按如下方式发送数据:

[{"descripcion":"Imagen trasera","idActivo":84,"idImagenes":7,"imagen":"R0lGODlhEAAQAPfAAAgAAPgAAP8JCQhUAAZBAABgmAAGCg6ZAI/W//X79Pf89wAQGju3/+/3+z9wjQCc+GbG/+/x8uPz4Q6YAAAGAAAaKgCR5gpoAACH1u/5//YAABehCSanGEWzOePw4e/z9SgAAABWiP+Hh/9eXpXLjwBMebDAr+/2+gMkAKgAAGkAAP8uLvH5/5CZj+zx7Pz+/P8eHnkAAIgAAOT1/5gAABGn//+cnC9WbO/y9Njv1vz+/zmvLPT7/wAtSEkAAMnr/4+eprgAAAB1uu/1+LPdr5Cej4+aoAA4Wuf2/wCS5//Pz8joxP/T04/H6I/H583rygZAAAAkOkJhP+cAAK3eqP9zc/L68d/y3Yp/fwCI10u2QP8zM+P04f9ISP+ysq+zr0eUP/L68gleADRdLwIaAAARHAU2AGjCXkBKPxkAAIGXfw2PAAoAAAU3AFkAAI+epy9EUQAHAI/M74+ntgEQAI++2QAbK+Dy3rrl/wyl/zkAAPR/f+Tw4y+NxIXOfk++/wppAHDFZ//c3ABCai+f4ACS6DeELwB+x//Hx4S3f/j8+G7J/0qsPwBqqENiP//x8QyGAEV4P93x2ssPD3vKcwdLAAyFAACI2EeVP7LQrwt7AOf15tgAANoPDwldAIKgf4Suf6jcome0X8gAAGWbX3HFaN7x3O/y7yAkH9HszghTAC9+rMDlvOvx6glfAJTGjy9FUcLfvwt8AA2OAJTTjS+e3wpyAEV/P+34/1O5SNzw2Y/Shw2QAMXnwf8AAP///wAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACH5BAEAAMAALAAAAAAQABAAAAjGAIEJFDjhgMEDEwYqBLZmDZFfEH8RabjQEgkJgThs2MAhkAQSlgZqepVjB4eTKHfkeKVJ4IVNWjrIhCizg5ZNF4BdyEQrl89cEH/mopXpgphfpc4oPQNx6ZlSv8QM+EWJUsSrVK0OqPRr1y6sEb3+qkTgV6hQByEePPuLQBsTqYjIfShxbioTbYBJUfCk7xOIfp8okCIQRREFd65cMaj4joIiKAaSafEiQRgrVsIkeNGCzEI6aL5c/YKGzsKBcSigokAhzsKAADs="}]

图片格式为BASE64,但会引发以下错误:

javax.servlet.ServletException: Exception [EclipseLink-6065] (Eclipse Persistence Services - 2.5.2.v20140319-9ad6abd): org.eclipse.persistence.exceptions.QueryException
Exception Description: Cannot add the object [activo.entities.Imagenes[ idImagenes=0 ]], of class [class activo.entities.Imagenes], to container [class activo.entities.Imagenes].
Internal Exception: java.lang.ClassCastException: activo.entities.Imagenes cannot be cast to java.util.Collection

因为它应该发送表格?感谢

1 个答案:

答案 0 :(得分:0)

我只需删除括号,删除GlassFish默认有netbeans,删除所有记录并安装新记录。

所以我坚持将图像插入base64到数据库

{"descripcion":"Imagen trasera","idActivo":{"idActivo":"84"},"idImagenes":0,"imagen":"R0lGODlhEAAQAPfAAAgAAPgAAP8JCQhUAAZBAABgmAAGCg6ZAI/W//X79Pf89wAQGju3/+/3+z9wjQCc+GbG/+/x8uPz4Q6YAAAGAAAaKgCR5gpoAACH1u/5//YAABehCSanGEWzOePw4e/z9SgAAABWiP+Hh/9eXpXLjwBMebDAr+/2+gMkAKgAAGkAAP8uLvH5/5CZj+zx7Pz+/P8eHnkAAIgAAOT1/5gAABGn//+cnC9WbO/y9Njv1vz+/zmvLPT7/wAtSEkAAMnr/4+eprgAAAB1uu/1+LPdr5Cej4+aoAA4Wuf2/wCS5//Pz8joxP/T04/H6I/H583rygZAAAAkOkJhP+cAAK3eqP9zc/L68d/y3Yp/fwCI10u2QP8zM+P04f9ISP+ysq+zr0eUP/L68gleADRdLwIaAAARHAU2AGjCXkBKPxkAAIGXfw2PAAoAAAU3AFkAAI+epy9EUQAHAI/M74+ntgEQAI++2QAbK+Dy3rrl/wyl/zkAAPR/f+Tw4y+NxIXOfk++/wppAHDFZ//c3ABCai+f4ACS6DeELwB+x//Hx4S3f/j8+G7J/0qsPwBqqENiP//x8QyGAEV4P93x2ssPD3vKcwdLAAyFAACI2EeVP7LQrwt7AOf15tgAANoPDwldAIKgf4Suf6jcome0X8gAAGWbX3HFaN7x3O/y7yAkH9HszghTAC9+rMDlvOvx6glfAJTGjy9FUcLfvwt8AA2OAJTTjS+e3wpyAEV/P+34/1O5SNzw2Y/Shw2QAMXnwf8AAP///wAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAACH5BAEAAMAALAAAAAAQABAAAAjGAIEJFDjhgMEDEwYqBLZmDZFfEH8RabjQEgkJgThs2MAhkAQSlgZqepVjB4eTKHfkeKVJ4IVNWjrIhCizg5ZNF4BdyEQrl89cEH/mopXpgphfpc4oPQNx6ZlSv8QM+EWJUsSrVK0OqPRr1y6sEb3+qkTgV6hQByEePPuLQBsTqYjIfShxbioTbYBJUfCk7xOIfp8okCIQRREFd65cMaj4joIiKAaSafEiQRgrVsIkeNGCzEI6aL5c/YKGzsKBcSigokAhzsKAADs="}