REST API_RESTCONTROLLER
๐ REST API
๐ @RestController
@RestController ๋
@RestController ๋ @Controller์ @ResponseBody๊ฐ ์ถ๊ฐ๋ ๊ฒ์ด๋ค.
์ฃผ์ฉ๋๋ Json ํํ๋ก ๊ฐ์ฒด ๋ฐ์ดํฐ๋ฅผ ๋ฐํํ๋ ๊ฒ์ด๋ค.
์ต๊ทผ์ ๋ฐ์ดํฐ๋ฅผ ์๋ต์ผ๋ก ์ ๊ณตํ๋ REST API๋ฅผ ๊ฐ๋ฐํ ๋ ์ฃผ๋ก ์ฌ์ฉํ๋ฉฐ ๊ฐ์ฒด๋ฅผ ResponseEntity๋ก ๊ฐ์ธ์ ๋ฐํํ๋ค.
@RestController ์์ ์ฝ๋
HttpStatus๋ฅผ ์ค์ ํด์ฃผ๊ธฐ ์ํด ResponseEntity๋ฅผ ์ฌ์ฉํ๋ค.
ResponseEntity๋ Spring Framework์์ ์ ๊ณตํ๋ ํด๋์ค ์ค HttpEntity๋ฅผ ์์๋ฐ๋๋ค.
HttpEntity๋ HTTP ์์ฒญ ( request ) ๋๋ ์๋ต ( response ) ์ ํด๋นํ๋ HttpHeader ์ HttpBody๋ฅผ ํฌํจํ๊ณ ์๋ค.
ResponseEntity๋ HttpRequest์ ๋ํ ์๋ต ๋ฐ์ดํฐ๋ฅผ ํฌํจํ๋ ํด๋์ค์ด๋ค.
๋ฐ๋ผ์ HttpStatus, HttpHeaders, HttpBody๋ฅผ ํฌํจํ๋ค.
์๋ ๊ฒฐ๊ณผ์ ๋ฐ๋ผ ์๋ต์ ๋ณด๋ด์ค์ ๊ฒฐ๊ณผ๋ฅผ ํ์ธํ๋ฉด ๋๋ค.
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.CrossOrigin;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
import com.ssafy.guestbook.model.MemberDto;
import com.ssafy.guestbook.model.service.MemberService;
@RestController
// ๋ฐ์ ๋ค @ResponseBody๋ฅผ ์ธ๊ฑฐ์ฌ์ RestController ๋ผ๊ณ ํ๋ค
@RequestMapping("/admin")
@CrossOrigin("*")
public class AdminController {
private static final Logger logger = LoggerFactory.getLogger(AdminController.class);
@Autowired
private MemberService memberService;
// responseEntity -> ์ํ์ฝ๋์ ๋ฐ์ดํฐ๋ฅผ ๊ฐ์ด ๋ฌถ์ด์ ๋ณด๋ผ ์ ์๋ค. <๋ฐํ๊ฐ ์๋ฃํ>(๋ฐํ๊ฐ, http์ํ )
// HTTPStatus.OK -> ์ ์์ ์ผ๋ก ์คํ๋๋ค
@GetMapping("/user")
public ResponseEntity<?> userList() throws Exception {
List<MemberDto> list = memberService.listMember();
// ์ ์์ ์ผ๋ก list๊ฐ ์กด์ฌํ๋ฉด list๋ฅผ ๋ฐํํด์ฃผ๊ณ ์ํ(OK)๋ฅผ ๋ฆฌํดํ๋ค.
// ์กด์ฌํ์ง ์์ผ๋ฉด ์ํ(NO_CONTENT)๋ฅผ ๋ฆฌํดํ๋ค.
if (list != null && !list.isEmpty()) {
return new ResponseEntity<List<MemberDto>>(list, HttpStatus.OK);
} else {
return new ResponseEntity<Void>(HttpStatus.NO_CONTENT);
}
}
// ๋ฑ๋กํ๊ธฐ
@PostMapping("/user")
public ResponseEntity<?> userRegister(@RequestBody MemberDto memberDto) throws Exception {
memberService.registerMember(memberDto);
// listMember ๋ฉ์๋๋ฅผ ํธ์ถํ๊ณ ์ํ(OK)๋ฅผ ๋ฆฌํดํ๋ค.
return new ResponseEntity<List<MemberDto>>(memberService.listMember(), HttpStatus.OK);
}
// id๋ก ํ์์ฐพ๊ธฐ
@GetMapping("/user/{userid}")
public ResponseEntity<?> userInfo(@PathVariable("userid") String userid) throws Exception {
MemberDto memberDto = memberService.getMember(userid);
// ์ ์์ ์ผ๋ก memberDto๊ฐ ์กด์ฌํ๋ฉด memberDto๋ฅผ ๋ฐํํด์ฃผ๊ณ ์ํ(OK)๋ฅผ ๋ฆฌํดํ๋ค.
// ์กด์ฌํ์ง ์์ผ๋ฉด ์ํ(NO_CONTENT)๋ฅผ ๋ฆฌํดํ๋ค.
if (memberDto != null) {
return new ResponseEntity<MemberDto>(memberDto, HttpStatus.OK);
} else {
return new ResponseEntity<Void>(HttpStatus.NO_CONTENT);
}
}
// update
@PutMapping("/user")
public ResponseEntity<?> userModify(@RequestBody MemberDto memberDto) throws Exception {
memberService.updateMember(memberDto);
// listMember ๋ฉ์๋๋ฅผ ํธ์ถํ๊ณ ์ํ(OK)๋ฅผ ๋ฆฌํดํ๋ค.
return new ResponseEntity<List<MemberDto>>(memberService.listMember(), HttpStatus.OK);
}
// delete
@DeleteMapping("/user/{userid}")
public ResponseEntity<?> userDelete(@PathVariable("userid") String userid) throws Exception {
memberService.deleteMember(userid);
// listMember ๋ฉ์๋๋ฅผ ํธ์ถํ๊ณ ์ํ(OK)๋ฅผ ๋ฆฌํดํ๋ค.
return new ResponseEntity<List<MemberDto>>(memberService.listMember(), HttpStatus.OK);
}
}