0. Postman 준비
https://www.postman.com/downloads/
Download Postman | Try Postman for Free
Try Postman for free! Join 13 million developers who rely on Postman, the collaboration platform for API development. Create better APIs—faster.
www.postman.com
1. 테스트할 컨트롤러와 오브젝트 만들기
- Member.java
@Data
@NoArgsConstructor
@AllArgsConstructor
public class Member {
private int id;
private String username;
private String password;
private String email;
}
- HttpControllerTest.java
// 사용자가 요청 -> 응답(HTML)
// @Controller
// 사용자가 요청 -> 응답(Data)
@RestController
public class HttpControllerTest {
// 인터넷 브라우저 요청은 무조건 Get요청밖에 할 수 없다.
// http://localhost:8080/http/get (select)
@GetMapping("/http/get")
public String getTest() {
return "get요청";
}
// http://localhost:8080/http/post (insert)
@PostMapping("/http/post")
public String postTest() {
return "post요청";
}
// http://localhost:8080/http/put (update)
@PutMapping("/http/put")
public String putTest() {
return "put요청";
}
// http://localhost:8080/http/delete (delete)
@DeleteMapping("/http/delete")
public String deleteTest() {
return "delete요청";
}
}
2. Get 요청
- Get방식으로 데이터를 보낼때는 클라이언트의 데이터를 URL뒤에 붙여서 보낸다.(key=value)
- 간단한 데이터를 넣도록 설계되어, 데이터를 보내는 양의 한계가 있다.
// 인터넷 브라우저 요청은 무조건 Get요청밖에 할 수 없다.
// http://localhost:8080/http/get (select)
@GetMapping("/http/get")
public String getTest(Member m) {
return "get요청 : " + m.getId() + ", " + m.getUsername() +
", " + m.getPassword() + ", " + m.getEmail();
}
3. Post 요청
- Post방식은 Get방식과 달리, 데이터 전송을 기반으로 한 요청 메서드이다.
(Get)URL에 데이터를 붙여 보냄 <-> (Post) URL에 붙이지 않고 Body에 데이터를 넣어 보냄
- 헤더필드에서 Body의 데이터를 설명하는 Content-Type이라는 헤더 필드가 들어간다.
// http://localhost:8080/http/post (insert)
@PostMapping("/http/post")
public String getTest(Member m) {
return "post요청 : " + m.getId() + ", " + m.getUsername() +
", " + m.getPassword() + ", " + m.getEmail();
}
- @RequestBody를 붙여줘야함
// http://localhost:8080/http/post (insert)
@PostMapping("/http/post")
public String postTest(@RequestBody Member m) {
return "post요청 : " + m.getId() + ", " + m.getUsername() + ", " + m.getPassword() + ", " + m.getEmail();
}
4. Put 요청 (데이터 수정 시에 사용)
// http://localhost:8080/http/put (update)
@PutMapping("/http/put")
public String putTest(@RequestBody Member m) {
return "put요청 : " + m.getId() + ", " + m.getUsername() +
", " + m.getPassword() + ", " + m.getEmail();
}
5. Delete 요청 (데이터 제거할 때 사용)
// http://localhost:8080/http/delete (delete)
@DeleteMapping("/http/delete")
public String deleteTest() {
return "delete요청";
}
'프로그래밍 > Spring' 카테고리의 다른 글
[Spring] json데이터로 통신하기 (0) | 2021.05.04 |
---|---|
[Spring] 컨트롤러에서 ModelMap으로 View로 넘기는중 오류(해결) (0) | 2021.05.04 |