PUT API
ํด๋์ค ์์ฑ
package com.springboot.api.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/api/v1/put-api")
public class PutController {
}
@RequestBody๋ฅผ ํ์ฉํ PUT ๋ฉ์๋ ๊ตฌํ
POST์ ๋ง์ฐฌ๊ฐ์ง๋ก ๋ฐ์ดํฐ์ ํํ๊ฐ ์ ํด์ ธ ์๋๊ฒฝ์ฐ, ๊ทธ๋ ์ง ์์ ๊ฒฝ์ฐ 2๊ฐ์ง๋ก ๋๋๋ค.
- ๋ฐ์ดํฐ์ ํํ๊ฐ ์ ํด์ ธ ์์ง ์์ ๊ฒฝ์ฐ
@PutMapping("/member") public String putMember(@RequestBody Map<String, Object> putData){ StringBuilder sb =new StringBuilder(); putData.entrySet().forEach(map->{ sb.append(map.getKey()+" : "+map.getValue()+"\n"); }); return sb.toString(); }
- ๋ฐ์ดํฐ์ ํํ๊ฐ ์ ํด์ ธ ์๋ ๊ฒฝ์ฐ
@PutMapping("/member2") public String putMember2(@RequestBody MemberDTO memberDTO){ return memberDTO.toString(); }
๊ฒฐ๊ณผ๋ POST์ ๊ฐ์ผ๋ฏ๋ก ์๋ตํ๋ค.
ResponseEntity๋ฅผ ํ์ฉํ PUT ๋ฉ์๋ ๊ตฌํ
์๋ต์ฝ๋ ๋ณ๊ฒฝ, Header, Body๋ฅผ ๊ฐ๊ฐ ๋ณ๊ฒฝํด์ฃผ๊ณ ์ถ๋ค๋ฉด, ResponseEntity
๋ฅผ ํ์ฉํ๋ฉด ๋๋ค.
@PutMapping("/member3")
public ResponseEntity<MemberDTO> memMemberDTO(@RequestBody MemberDTO memberDTO){
return ResponseEntity
.status(HttpStatus.ACCEPTED)
.body(memberDTO);
}
๋ค์๊ณผ ๊ฐ์ ํ์์ผ๋ก ๋ฐํ์ด ๋๋ค.
- ์๋ต์ฝ๋
- ACCEPTED
- body
- memberDTO
DELETE API
ํด๋์ค ์์ฑ
package com.springboot.api.controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/api/v1/delete-api")
public class DeleteController {
}
@PathVariable์ ํ์ฉํ DELETE ๋ฉ์๋ ๊ตฌํ
@DeleteMapping(value="/{variable}")
public String DeleteVariable(@PathVariable String variable){
return variable;
}
@RequestParam์ ํ์ฉํ DELETE ๋ฉ์๋ ๊ตฌํ
@DeleteMapping(value="/request1")
public String deleteRequestParam(@RequestParam String email){
return "e-mail : "+email;
}
Uploaded by N2T
'Programming > [Spring Boot]' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
DB ๊ด๋ จ ์ฉ์ด (1) | 2023.02.01 |
---|---|
Swagger๋ฅผ ํ์ฉํ REST API ๋ช ์ธ ๋ฌธ์ํ (0) | 2023.01.25 |
POST API ๋ง๋ค๊ธฐ (0) | 2023.01.20 |
GET API ๋ง๋ค๊ธฐ (0) | 2023.01.19 |
ํ๋ก์ ํธ ์์ฑํ๊ธฐ (0) | 2023.01.19 |