📌 HTTP1.1 요청방식
1. POSTMAN 설치
https://www.postman.com/downloads/
2. HTTP1.1 (통신방법4가지 = 요청의 방법)
- get : 데이터를 달라! - select
- post : 데이터를 추가해줘! - insert
- delete : 데이터를 삭제해줘! -delete
- put : 데이터를 수정해줘! - update
3. Stateless 와 Stateful
- stateful : 연결이 지속되다.
- stateless : 요청시마다 스트림을 연결해서 Data를 주고 받는 방식(=http)
4. MIME 타입
https://developer.mozilla.org/ko/docs/Web/HTTP/Basics_of_HTTP/MIME_types/Common_types
5. Controller 생성 및 실습
- HttpControllerTest.java 생성
- Get 메서드
- Post 메서드
- Put 메서드
- Delete 메서드
6. Http 요청 실습
1. com.cos.blog.test에 class를 하나 만든다.
2. 클래스명은 HttpControllerTest라고 만든다
package com.cos.blog.test;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RestController;
//사용자가 요청 -->응답(html)
// @Controller
// 사용자가 요청 --> 응답(Data)
@RestController
public class HttpControllerTest {
// http://localhost:8081/http/get(select)
@GetMapping("/http/get")
public String getTest() {
return "get요청";
}
// http://localhost:8081/http/post(insert)
@PostMapping("/http/post")
public String postTest() {
return "post요청";
}
// http://localhost:8081/http/put(update)
@PutMapping("/http/put")
public String putTest() {
return "put요청";
}
// http://localhost:8081/http/delete(delete)
@DeleteMapping("/http/delete")
public String deleteTest() {
return "delete 요청";
}
}
3. 프로젝트 우클릭 run as -> spring boot app클릭
4. get요청
chrome에서 localhost:8081/http/get을 입력하면, 아까 get방식에서 썼던 return값을 출력한다.
5. post, put, delete요청
chrome에서 localhost:8081/http/post or put or delete를 입력하면, 405에러가 발생하는데,
405에러는 이 방식으로는 웹브라우저에서 요청할 수 없다는 것이다.
그말은 즉슨, 인터넷 브라우저 요청은 무조건 get요청만 가능하다는 것이다!!!!
📌Get 방식 이용해서 보내기
1. com.blog.test 패키지에 Member라는 클래스를 생성한다.
package com.cos.blog.test;
public class Member {
private int id;
private String username;
private String password;
private String email;
public Member(int id, String username, String password, String email) {
this.id = id;
this.username = username;
this.password = password;
this.email = email;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
2. postman을 실행시켜서 get방식으로 id와 username을 쿼리스트링으로 기입하고
3. 다시 sts에 돌아와 get안에 @RequestParam + 해당하는 변수자료형과 return값에 그 변수를 넣어주고 실행하면
@GetMapping("/http/get")
public String getTest(@RequestParam int id, @RequestParam String username) {
return "get요청" +id + username;
}
다음과 같이 리턴값이 출력이 된다.
4. get방식에 member object를 넣어서 해보면 굳이 하나하나씩 @RequestParam을 쓸필요가 없다.
get방식의 가장 큰 특징은 쿼리 스트링으로만 값을 받아낸다는 것이다.
(ex) id=1&username=ssar&password=korea
@GetMapping("/http/get")
public String getTest(Member m) {
return "get요청 : " +m.getId()+","+m.getUsername()+","+m.getPassword();
}
📌Post 방식 이용해서 보내기
1. post에 GET방식처럼 Member object를 삽입해서 return값을 요청
// http://localhost:8081/http/post(insert)
@PostMapping("/http/post")
public String postTest(Member m) {
return "post요청 : " +m.getId()+","+m.getUsername()+","+m.getPassword();
}
2. post방식은 get방식과 다르게 쿼리스트링을 사용하지 않고, 두가지 방법이 있는데,
postman을 실행시킨 다음 body 에서 x-www-form-urlencoded 방식을 사용하는 것은
html에서 form 태그 안에 input 으로 삽입하는 방식과 같다.
3. text/plain로 @RequestBody이용하여 Post 요청
@PostMapping("/http/post")
public String postTest(@RequestBody String text) {
return "post요청 : " + text;
}
post - Body - raw - Text를 삽입 후 출력
raw 데이터란 = text/plain = 즉, 가장 낮은 데이터 = 가장 기본적인 데이터
4 . application/json 방식으로 post 요청
@PostMapping("/http/post") // text/plain, application/json
public String postTest(@RequestBody Member m) {
return "post요청 : " +m.getId()+","+m.getUsername()+","+m.getPassword();
}
***** json 방식으로 요청하는 과정에서 다음과 같은 오류가 발생하였다 ******
- jackson library가 빈 생성자가 없는 모델을 생성하는 방법을 모른다는 의미였다.
Cannot construct instance of `com.cos.blog.test.Member` (no Creators, like default constructor, exist): cannot deserialize from Object value (no delegate- or property-based Creator) |
-해결방안은?
- 따로 빈 생성자를 추가해줘야 한다.
- Member.java 에 Member기본생성자 만들자
package com.cos.blog.test;
public class Member {
private int id;
private String username;
private String password;
private String email;
//기본 생성자 추가
public Member() {
}
public Member(int id, String username, String password, String email) {
this.id = id;
this.username = username;
this.password = password;
this.email = email;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
}
다시 실행해보니 정상적으로 동작하였다
위와 같이 JSON 형태로 데이터를 요청하는데 Object, 즉, Member 클래스에 매핑시켜서 출력이 될 수 있는 이유는
바로 MessageConverter(스프링 부트)가 JSON을 Object로 변환을 해주기 때문이다.
'Spring > Spring 블로그만들기' 카테고리의 다른 글
[Springboot] 나만의 블로그 만들기 - 9. yml설정하기 (0) | 2022.03.21 |
---|---|
[Springboot] 나만의 블로그 만들기 - 8. Lombok 세팅하기 (0) | 2022.03.21 |
[Springboot] 나만의 블로그 만들기 - 6. Git 세팅 (0) | 2022.03.08 |
[Springboot] 나만의 블로그 만들기 - 5. MySQL 스프링 연결 (0) | 2022.03.07 |
[Springboot] 나만의 블로그 만들기 - 4. MySQL 세팅하기 (0) | 2022.03.07 |