How to expose Blob (Image) data via REST webservice - java

I'm storing images in blob form in a MySql database table. I need to expose these images via a REST webservices and was wondering what my options are. The main option I see is to read the database, create the image and store it on the filesystem and send the path to the image in the REST response. Any other ideas here? I'm thinking I will use RESTeasy for my JAX-RS implementation. Thanks for the help!

You can parse your image into bytes and then can send those bytes in JSON form via web service.

Related

Can a REST Service Controller read a text file which is located at the path given in request param?

My requirement is to create a java restful web service which reads a text file and pull out some information from that text file. I am new to restful web services so I don't know exactly how to code this. I am thinking there would be a REST Service Controller which has a post HTTP method that takes a file path as a request parameter.
Now I wonder how that rest controller has an access to that file or can rest controller read it and extract information from that file?
The location to the file would not be enough, you will need to send the protocol to connect to the location of the file, the timeout for the connection, and if the protocol needs some authentication you need to verify it and send the required parameters.
Implementing the rest controller is not issue, after exposing an api you can use InputStream to read the content of the file, check the docs

How to transfer files (PDF, images etc) in JSON?

How to send file from my backend project to my frontend using JSONObject in Java so I want to know if it is possible and how? (I'm using Spring framework)
You can directly send bytes of the file or you can convert file into stream and then send.

RESTful Webservices with PDF as an input file

Is is possible to exchange PDF file as a input for RESTful webservices and also I would like to send PNG image as a response to it.
If we can do it through REST services please provide me the references link to implement it using REST services.
I was able to achieve a similar output by converting the file to string/byte[] data and sending it via REST.
My implementation was in Java and the steps used is outline below
Convert the file on disk to byte[] array (apache common-io can convert the file to byte[] in easy step. Try the IOUtils class)
Encoded the byte[] as String (apache common-codec was used for the encoding)
Wrapped the string data in a model class
Converted the model class to json format (GSON was used for the conversion)
Sent the json data over to the server
The server application reversed the process, and the file was available on the server
A rest service isn't the right way for what you want. The input for this kind of services are HTTP request attributes or some kind of pushed data. Maybe it's possible to implement a file upload but it's not typical.
For restful services is also common to tell your service how to handle the requested resource via the used request method (GET, POST, PUT, DELETE)
The response of rest services is normally some kind of structured text output - for instance json.
All in all rest services seems to me not the way to implement your desired scenario.
What about a normal cgi or servlet solution?

Posting file content to a REST webservice along with other data in java

I have a REST webservice developed using java. I have a jar file which does some logic and writes some data to a file. My requirement is to send the data along with a couple of other data (other two data are just simple strings). How do i write the service part ?
1. Should i write the service so as to accept data as byte stream ? If so how do i send the file content from the jar to the service ?
Any help is appreciated

Passing data from Excel to a Web service

I have a excel file with testing data and am trying to send these parameters to a new web service (Testing) in java. These data will inturn be sent to another web service that is already in use now. After sending the request data, I will also need to get the response and update the excel file with the testing result ,say pass or fail. I haven't worked with web services(java) before , so am not sure how to go about this. Any tutorials or advice on this from people who have worked on this before will be great.
Thanks
Hope you have worked with Java before because this is not a straightforward assignment.
You will need an API to read the Excel spreadsheet ( look up JXL or Apache POI ), you will need to generate a web services client for your WS endpoint using JAX-WS ( wsimport ). Once you have that, you can call your web service via Java and the response will be a Java object that you can then use to update your Excel sheet ( via the api of your choice ).
Good luck.
http://jexcelapi.sourceforge.net/
http://download.oracle.com/javase/6/docs/technotes/tools/share/wsimport.html
http://poi.apache.org/spreadsheet/index.html

Categories