BackEnd_Servers

[BackEnd_Servers] REST API (GET/POST/PUT/DELETE)

wookjae 2021. 10. 4. 12:46

HTTP METHODS

  - GET: 리소스 조회 

  - POST: 데이터의 처리 (등록 INSERT)

  - PUT: 리소스 대체(전체수정 UPDATE), 대체 리소스가 없을 시 생성(INSERT) 

  - PATCH: 리소스 "부분변경" (수정 UPDATE)

  - DELETE: 리소스 삭제   

 


 REST API Excesicse..

 

1.1. XML Response

REST API Code 

Spring REST API for HTTP GET method.

@RequestMapping(value = "/employees", produces = MediaType.APPLICATION_XML_VALUE, method = RequestMethod.GET)
public String getAllEmployeesXML(Model model)
{
    model.addAttribute("employees", getEmployeesCollection());
    return "xmlTemplate";
}

REST Client Code 

Spring REST client using RestTemplate to access HTTP GET api requests.

private static void getEmployees()
{
    final String uri = "http://localhost:8080/springrestexample/employees.xml";
     
    RestTemplate restTemplate = new RestTemplate();
    String result = restTemplate.getForObject(uri, String.class);
     
    System.out.println(result);
}

 

 

1.2. JSON Response

REST API Code

Spring REST API for HTTP GET method.

@RequestMapping(value = "/employees", produces = MediaType.APPLICATION_JSON_VALUE,  method = RequestMethod.GET)
public String getAllEmployeesJSON(Model model)
{
    model.addAttribute("employees", getEmployeesCollection());
    return "jsonTemplate";
}

REST Client Code

Spring REST client using RestTemplate to access HTTP GET api requests.

private static void getEmployees()
{
    final String uri = "http://localhost:8080/springrestexample/employees.json";
     
    RestTemplate restTemplate = new RestTemplate();
    String result = restTemplate.getForObject(uri, String.class);
     
    System.out.println(result);
}

 

 

1.3. Custom HTTP Headers with RestTemplate

REST API Code

@RequestMapping(value = "/employees", produces = MediaType.APPLICATION_JSON_VALUE,  method = RequestMethod.GET)
public String getAllEmployeesJSON(Model model)
{
    model.addAttribute("employees", getEmployeesCollection());
    return "jsonTemplate";
}

REST Client Code

private static void getEmployees()
{
    final String uri = "http://localhost:8080/springrestexample/employees";
     
    RestTemplate restTemplate = new RestTemplate();
     
    HttpHeaders headers = new HttpHeaders();
    headers.setAccept(Arrays.asList(MediaType.APPLICATION_JSON));
    HttpEntity<String> entity = new HttpEntity<String>("parameters", headers);
     
    ResponseEntity<String> result = restTemplate.exchange(uri, HttpMethod.GET, entity, String.class);
     
    System.out.println(result);
}

 

 

1.4. Get Response as Object

REST API Code

@RequestMapping(value = "/employees", produces = MediaType.APPLICATION_XML_VALUE, method = RequestMethod.GET)
public String getAllEmployeesXML(Model model)
{
    model.addAttribute("employees", getEmployeesCollection());
    return "xmlTemplate";
}

REST Client Code

private static void getEmployees()
{
    final String uri = "http://localhost:8080/springrestexample/employees";
    RestTemplate restTemplate = new RestTemplate();
     
    EmployeeListVO result = restTemplate.getForObject(uri, EmployeeListVO.class);
     
    System.out.println(result);
}

 

 

1.5. URL Parameters

REST API Code

@RequestMapping(value = "/employees/{id}")
public ResponseEntity<EmployeeVO> getEmployeeById (@PathVariable("id") int id)
{
    if (id <= 3) {
        EmployeeVO employee = new EmployeeVO(1,"Ukjae","Jung","test@email.com");
        return new ResponseEntity<EmployeeVO>(employee, HttpStatus.OK);
    }
    return new ResponseEntity(HttpStatus.NOT_FOUND);
}

REST Client Code

private static void getEmployeeById()
{
    final String uri = "http://localhost:8080/springrestexample/employees/{id}";
     
    Map<String, String> params = new HashMap<String, String>();
    params.put("id", "1");
     
    RestTemplate restTemplate = new RestTemplate();
    EmployeeVO result = restTemplate.getForObject(uri, EmployeeVO.class, params);
     
    System.out.println(result);
}

 

 

2. Spring RestTemplate – HTTP POST Method Example

REST API Code

Spring REST API for HTTP POST method.

@RequestMapping(value = "/employees", method = RequestMethod.POST)
public ResponseEntity<String> createEmployee(@RequestBody EmployeeVO employee)
{
    System.out.println(employee);
    return new ResponseEntity(HttpStatus.CREATED);
}

REST Client Code

Spring REST client using RestTemplate to access HTTP POST api requests.

private static void createEmployee()
{
    final String uri = "http://localhost:8080/springrestexample/employees";
 
    EmployeeVO newEmployee = new EmployeeVO(-1, "Ukjae", "Jung", "test@email.com");
 
    RestTemplate restTemplate = new RestTemplate();
    EmployeeVO result = restTemplate.postForObject( uri, newEmployee, EmployeeVO.class);
 
    System.out.println(result);
}

 

 

3. Spring RestTemplate – HTTP PUT Method Example

REST API Code

Spring REST API for HTTP PUT method.

@RequestMapping(value = "/employees/{id}", method = RequestMethod.PUT)
public ResponseEntity<EmployeeVO> updateEmployee(@PathVariable("id") int id, @RequestBody EmployeeVO employee)
{
    System.out.println(id);
    System.out.println(employee);
    return new ResponseEntity<EmployeeVO>(employee, HttpStatus.OK);
}

REST Client Code

Spring REST client using RestTemplate to access HTTP PUT api requests.

private static void updateEmployee()
{
    final String uri = "http://localhost:8080/springrestexample/employees/{id}";
     
    Map<String, String> params = new HashMap<String, String>();
    params.put("id", "2");
     
    EmployeeVO updatedEmployee = new EmployeeVO(2, "New Name", "Ukjae", "test@email.com");
     
    RestTemplate restTemplate = new RestTemplate();
    restTemplate.put ( uri, updatedEmployee, params);
}

 

 

4. Spring RestTemplate – HTTP DELETE Method Example

REST API Code

Spring REST API for HTTP DELETE method.

@RequestMapping(value = "/employees/{id}", method = RequestMethod.DELETE)
public ResponseEntity<String> updateEmployee(@PathVariable("id") int id)
{
    System.out.println(id);
    return new ResponseEntity(HttpStatus.OK);
}

REST Client Code

Spring REST client using RestTemplate to access HTTP DELETE api requests.

private static void deleteEmployee()
{
    final String uri = "http://localhost:8080/springrestexample/employees/{id}";
     
    Map<String, String> params = new HashMap<String, String>();
    params.put("id", "2");
     
    RestTemplate restTemplate = new RestTemplate();
    restTemplate.delete ( uri,  params );
}

 

 

 

 

exit..