rest api java code example
Example 1: rest api
Representational state transfer is a software architectural style that defines
a set of constraints to be used for creating Web services.
Web services that conform to the REST architectural style, called RESTful Web
services, provide interoperability between computer systems on the internet
Example 2: spring @restcontroller
package com.example.restservice;
import java.util.concurrent.atomic.AtomicLong;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class GreetingController {
private static final String template = "Hello, %s!";
private final AtomicLong counter = new AtomicLong();
@GetMapping("/greeting")
public Greeting greeting(@RequestParam(value = "name", defaultValue = "World") String name) {
return new Greeting(counter.incrementAndGet(), String.format(template, name));
}
}Copy
Example 3: rest api
1) REQUEST
2) RESPONSE
When we send request, we need to know the API
methods/endpoints that are available:
- read documentation about API methods.
- Swagger tool, that has API methods and descriptions
simple endpoint:
…school.com/api/students
Types of Requests:
GET -> Read data
POST -> Create/insert data
PUT -> Update data
DELETE-> Delete data
I send GET, POST, PUT, DELETE type of API requests to
API endpoint/method and get response.
ORDS (oracle data service) API -> HR Database
ORDS API has methods that we can send request to, and it sends
response with Data from HR database.