How to transfer an Image with RestfulAPI – Image RestfulApi

Exchange Images between Client & Server is a common problem in programming. So in the tutorial: How to transfer an Image with RestfulAPI – Image RestfulApi, JavaSampleApproach will guide you how to build a restful Api for exchanging images with Spring Boot & Spring RestTemplate & Java8-Base64

Related posts:
1. How to use Spring RestTemplate client for consuming Restful WebService
2. How to use Java 8 Encode (Decode) an Image to Base64

I. Overview

In the tutorial, we create 2 projects, once for Restful Services, another for RestTemplate Client
– For exchange data between Client & Server, Images had been encoded to Base64-String.

1. Overview

image restfulapi project structure

2. Step to do

– Create a Spring Boot project for Restful WebServices & Spring RestTemplate Client
– Create a business model class
– Create Utilize class to encode/decode an Image to Base64 String
– For Restful Web Services, create Controllers: POST, GET
– For Rest-Template client, create Spring Rest-Template with functions: POST, GET
– Apply Rest-Template client
– Build & Enjoys with results

II. Practice
1. Create a Spring Boot project for Restful WebServices & Spring RestTemplate Client

Open Spring Tool Suite, on main menu, choose File->New->Spring Starter Project, then input needed project info. Press Next, then press Finish, a Spring Boot project will be created successfully.

Open pom.xml file, add Web dependencies:

2. Create a business model class

3. Create Utilize class to encode/decode an Image to Base64 String

4. For Restful Web Services, create Controllers: POST, GET

5. For Rest-Template client, create Spring Rest-Template with functions: POST, GET

6. Apply Rest-Template client

7. Build & Enjoys with results

Create folders:
– For Client: “C:\\client” & “C:\\client\\get”
– For Server: C:\\server
Build & Run by Order:
1. Run Spring Restful Service
2. Run Spring RestTemplate Client

Result:
1. Logs from Spring Restful Service:

2. Logs from Spring RestTemplate Client:

III. Sourcecode

SpringbootRestfulService
SpringRestTemplateForTransferImage



By grokonez | January 16, 2017.

Last updated on June 4, 2017.



Related Posts


5 thoughts on “How to transfer an Image with RestfulAPI – Image RestfulApi”

  1. Thanks for the tutorial. Do you know if there is any method than base64 encoding with which we can transfer image to server through RESTAPI.
    If we are testing this service using POSTMAN- how can we encode every image to base64. before hitting the rest API.

      1. hello,

        i am new to spring and i have created a discussion board where i’d need to attach files for posts and comments. i’m trying to upload a file in ui and convert the file into base64 and then save to database using rest api. when call my service it gives me null as a response and saves in the database. can you please help me with the service to save/get image/any file attachment to my database.

        i’m using angular 5 for front-end and spring boot for services.

        PS: when i test it from postman i get a error: “JSON parse error: Invalid UTF-8 start byte 0x80\n at [Source: (PushbackInputStream);
        this happens when i copy the base64 code from Ui console and test in post man.

        when i convert any file using an online converter and then copy the base64 code and test in the post man the data is saved successfully.

  2. May 30, 2018 at 7:56 pm
    hello,

    i am new to spring and i have created a discussion board where i’d need to attach files for posts and comments. i’m trying to upload a file in ui and convert the file into base64 and then save to database using rest api. when call my service it gives me null as a response and saves in the database. can you please help me with the service to save/get image/any file attachment to my database.

    i’m using angular 5 for front-end and spring boot for services.

    PS: when i test it from postman i get a error: “JSON parse error: Invalid UTF-8 start byte 0x80\n at [Source: (PushbackInputStream);
    this happens when i copy the base64 code from Ui console and test in post man.

    when i convert any file using an online converter and then copy the base64 code and test in the post man the data is saved successfully.

  3. Seal armpit brings discomfort, pulls. to Postpone visit doctors is not appropriate. Lump under the muscle cavity may turn out to be cancer illness. But often it is consequence excessive use deodorants, violation conditions personal hygiene, excessive sweating. Inflammation triggers narrow blouse, alien razor, the virus.
    [url=http://armpit.info/what-does-a-small-hard-lump-under-armpit-mean]small hard lump under armpit[/url]
    Compaction under the arm, redness, lump getting warm? This is the boil that will cut from the surgeon, then drink antibiotics. In the first stage cost ointments, antibacterial drugs. If the process is started, shown surgery.

Got Something To Say:

Your email address will not be published. Required fields are marked *

*