I'm building a web application using AngularJS as frontend. I'm using Rest services to call the backend and deploying the application in Jboss Wildfly. The application connects to a Mysql database where I have a Table Products. A product consists in one id and one image that is stored as a BLOB. The Product java entity is as follows, the :
@Entity
@Table(name = "PRODUCTS")
public class Product implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "ID")
private Long id;
@Column(name = "IMAGE", nullable = false, insertable = true, updatable = true)
private Blob image;
@Column(name="PRICE",nullable=false, insertable=true,updatable=true)
private Long price;
GETTERS AND SETTERS......
}
Then the code of the Rest Service:
@Path("/products")
public class ProductsService extends CommonService{
private static final Logger log = Logger.getLogger(ProductsService.class);
@EJB
private ProductsServiceHandler handler;
@GET
public Response getAllProducts(@Context Request request, @Context HttpHeaders httpHeaders)
{
try
{
List<Product> products=handler.findAllProducts();
return Response.ok(products).header(ALLOW_ORIGIN_HEADER, "*").build();
}
catch(Exception e){return Response.status(Status.INTERNAL_SERVER_ERROR).build();}
}
The products are retrieved correctly from the database, with the images stores as Blob, but then when I try to send the response I get the following error:
Caused by: com.fasterxml.jackson.databind.JsonMappingException: No serializer found for class java. io.ByteArrayInputStream and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) ) (through reference chain: java.util.ArrayList[0 ]->com.fgonzalez.domainmodel.Product["image"]->$Proxy70["binaryStream"])
Which I'm guessing is because it does not know how to serialize the Blob. I noticed that if I send only the image as InputStream it does not complain (but now I'll have to figure out how to display that in Html). Any idea of how to achieve what I need?
Thank yo very much in advance!!