Save and retrieve image from database using spring mvc and hibernate Rest services

I am developing an application in which I need to save an image in db and retrieve.I made the saved part. I saved the form in the form of byte []. When i get json image returning

jThiODUwMjE0MDNlNGFlZTI2MTk1YjBlNDFlYjAwYTI2MTI4OWRlOGU4NGU0OTFlZGU2NzAzMjE2ODA0N2RkMDY5NTkyODg4NzliOWE1ODlhNTNhYTM1OTYxNzZjNTc4YjcwMTJiZmUyNmY1NTJkNzI1MjhkN2FhZjU0ZmU0MWZmMzVjMWJhYzU2NmU3Y2M4NTUzMTBlNWMxODRhMjczYTIwZjhhNDk1MzU0NzhhN2YxNDgxMmYxNzRhNTVhMDI4YzVkYmI3NzgzNWMzNjZkYjFiNDgwN2JhNTUyNDEzMDAzZTJlZmRjYjNkNzFkZTIzZDNiMmNjYTgyN2I4ZTgzM

goes like this 100lines.Here I'm seraching from Image ID.

Recreation Service:

@RequestMapping(value="/getphoto/{clsfdimageid}", method=RequestMethod.GET) @ResponseBody public List<ClassifiedImages> getPhoto(@PathVariable int clsfdimageid) { System.out.println("entered...................."); return classifiedService.getImage(clsfdimageid); } 

DaoImpl Class:

 @Override public List<ClassifiedImages> getImage(int clsfdimageid) { session = sessionFactory.getCurrentSession(); String sql = "SELECT * from tblclsfdimages where clsfdimageid = :clsfdimageid"; Query query= session.createSQLQuery(sql) .addEntity(ClassifiedImages.class) .setParameter("clsfdimageid",clsfdimageid); List<ClassifiedImages> images = query.list(); return images; } 

How can I get json image with byte []. Thanks in advance for your suggestions.

+1
source share
1 answer

Even if you did not specify ClassifiedImages , I assume that you have a field like:

 private byte[] image; 

On this line, you see the Base64 encoded value of your byte[] array. It is beautiful, and there is nothing wrong with that. REST and JSON require that each contained field be serialized as included arrays String, byte [].

So, in the client code, you will need a Base64 decoder to take the encoded String and convert it to a byte[] array and display your image.

+2
source

Source: https://habr.com/ru/post/976517/


All Articles