REST API Example [Java Code]

To send JSON data to the REST API endpoint, you must enclose the JSON data in the request body and indicate the data type with the "Content-Type: application/json" request header. The client also can send the "Accept: application/json" request header, which indicates that the client wants to receive the data in JSON format. If the REST API server returns a JSON, it indicates the type of data in response with the "Content-Type: application/json" response header. In this REST API Example, we are sending JSON data to the ReqBin echo URL. Click Send to make a REST API request online and see the results. The Java code was automatically generated for the REST API example.
REST API Example [Java Code] Send
POST /echo/post/json HTTP/1.1
Accept: application/json
Content-Type: application/json
Content-Length: 42

{"Id": 78912,"Quantity": 1,"Price": 18.00}
Updated: Viewed: 25005 times
Java code for REST API example

Java code for REST API Example

This Java code snippet was generated automatically for the REST API example.
<< Back to the REST API example

What is REST API?

The REST API is an application programming interface created in 2000 by computer scientist Roy Fielding. A REST API is an API that follows REST design principles or an architectural style of representational state transfer that provides a relatively high level of flexibility and freedom for developers. This flexibility is just one of the reasons why REST APIs have become the standard way to connect components and applications in a microservice architecture. For this reason, REST APIs are sometimes referred to as RESTful APIs. REST APIs communicate via HTTP requests to perform standard CRUD (read, update, and delete) operations within a resource. For example, a REST API will use a GET request to retrieve a document, a POST request to create it, a PUT request to update a record, and a DELETE request to delete it. API calls can use all HTTP methods.

What is an API?

API (Application Programming Interface) are definitions and protocols for building and integrating application software. An API is a software mediator that allows two applications to exchange data. The APIs are standards-compliant (HTTP and REST) that are user-friendly and readily available to developers. The API has its software development lifecycle (SDLC), which includes designing, testing, building, managing, and versioning, just like any other software produced. In addition, the APIs are well documented for use and version control.

What is REST?

REpresentational State Transfer (REST) is an architectural style that enforces standards between computer systems on the Internet to make it easier for systems to interact with each other. Like other architectural styles, REST has its own rules and limitations. REST systems often referred to as RESTful systems, are separate stateless client and server tasks.

When the API is considered RESTful?

For an API to be considered RESTful, it must meet the following criteria:

  • The client-server architecture consists of clients, servers, and resources, and requests are managed over HTTP.
  • Cached data that simplifies the exchange of data between client and server.
  • Stateless communication on the client-server, which means no client information, is kept between GET requests, and each request is separate and unrelated.
  • A multi-tiered system that organizes servers of each type (responsible for security, load balancing)

REST API Requests Example

To post JSON to the server, you must set the appropriate content type for the request body. If your client is expecting a JSON data from the server, it should also send the Accept: application/json request header.

Content-Type Syntax
POST /echo/post/json HTTP/1.1
Accept: application/json
Content-Type: application/json
Content-Length: 42

{"Id": 78912,"Quantity": 1,"Price": 18.00}

REST API Responses Example

Below is an example of a REST API server response to our test request. We've cleaned up the optional HTTP headers returned by the server to make the example clearer.

Accept Header Example
HTTP/1.1 200 OK
Content-Type: application/json
Content-Length: 19


The Content-Type: application/json response header informs the client that the server has returned JSON data. The Content-Length: 19 header indicates the size of JSON in response.

See also

Generate code snippets for Java and other programming languages

Convert your REST API request to the PHP, JavaScript/AJAX, Curl/Bash, Python, Java, C#/.NET code snippets using the Java code generator.