Hello

15,16 http요청 실습

by 볼빵빵오춘기

- https://www.youtube.com/watch?v=BNiDNAWZn-E&list=PL93mKxaRDidECgjOBjPgI3Dyo8ka6Ilqm&index=15&t=1s

- https://www.youtube.com/watch?v=Fd5Rhz0j8QQ&list=PL93mKxaRDidECgjOBjPgI3Dyo8ka6Ilqm&index=16

 

src > mai > java > com > cos > blog > test > HttpControllerTest 클래스 생성

src > mai > java > com > cos > blog > test > Member 클래스 생성

 

Member.java 

package com.cos.blog.test;

import lombok.*;

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 void setId(int id) {
        this.id = id;
    }

    public void setUsername(String username) {
        this.username = username;
    }

    public void setPassword(String password) {
        this.password = password;
    }

    public void setEmail(String email) {
        this.email = email;
    }

    public int getId() {
        return id;
    }

    public String getUsername() {
        return username;
    }

    public String getPassword() {
        return password;
    }

    public String getEmail() {
        return email;
    }
}

 

HttpControllerTest.java

package com.cos.blog.test;

import org.springframework.web.bind.annotation.*;

@RestController
public class HttpControllerTest {

    // http://localhost:8080/http/get (select)
    @GetMapping("/http/get")
    public String getTest(){
        return "get 요청";
    }

    // 값을 쿼리스트링으로 전달
    // http://localhost:8080/http/get1?id=1&username=test&password=1234&email=test@test.com
    @GetMapping("/http/get1")
    public String getTest1(@RequestParam int id, @RequestParam String username, @RequestParam String password, @RequestParam String email ){
        return "get 요청 : id="+id+", username="+username+", password="+password+", email="+email;
    }

    // http://localhost:8080/http/get2?id=1&username=test&password=1234&email=test@test.com
    @GetMapping("/http/get2")
    public String getTest2(Member m){
        return "get 요청 : id="+m.getId()+", username="+m.getUsername()+", password="+m.getPassword()+", email="+m.getEmail();
    }

    // http://localhost:8080/http/post (insert) form태그 이용방식
    @PostMapping("/http/post")
    public String postTest(Member m){
        return "post 요청 : id="+m.getId()+", username="+m.getUsername()+", password="+m.getPassword()+", email="+m.getEmail();
    }

    // http://localhost:8080/http/post2 (insert) text 받아보기
    @PostMapping("/http/post2")
    public String postTest2(@RequestBody String text){
        return "post 요청 : "+text;
    }

    // http://localhost:8080/http/post3 (insert) json으로 받아보기
    @PostMapping("/http/post3")
    public String postTest3(@RequestBody Member m){ // MessageConverter(스프링부트)
        return "post 요청 : id="+m.getId()+", username="+m.getUsername()+", password="+m.getPassword()+", email="+m.getEmail();
    }

    // http://localhost:8080/http/put (update)
    @PutMapping("http/put")
    public String putTest(@RequestBody Member m){
        return "put 요청 : id="+m.getId()+", username="+m.getUsername()+", password="+m.getPassword()+", email="+m.getEmail();
    }

    // http://localhost:8080/http/delete (delete)
    @DeleteMapping("http/delete")
    public String deleteTest(@RequestBody Member m){
        return "delete 요청 : id="+m.getId()+", username="+m.getUsername()+", password="+m.getPassword()+", email="+m.getEmail();
    }

}

인터넷 브라우저는 요청은 get밖에 할 수 없으므로

post, put, delete 방식의 url은 postman을 켜서 진행

 

해당 url을 넣어서 값이 어떻게 나오는지 확인 가능하다. 

 

그리고 get 요청방식 때는 쿼리스트링 방식으로 

url 뒤에 ?를 붙이고 키와 값을 쌍으로 해서 값을 넘기면 

controller에서 메소드에 매개변수 앞에 @RequestParam을 붙여 받아올 수도 있지만

위에 Member클래스를 만들어서 받아올 수도 있다. 

대신 위에서 id, password, email, username 을 받아온다고 한다면 

Member 클래스에도 id, password, email, username로 변수로 정의되어있고

getter,setter, 생성자도 함께 만들어 놔야한다. 

 

'강의 따라하기 > blog' 카테고리의 다른 글

18 lombok 세팅 및 사용해보기  (0) 2024.01.01
17 maven이란  (0) 2024.01.01
6. 프로젝트 실행해보기  (0) 2023.07.26
5. 의존성 설정  (0) 2023.07.26
Springboot - 나만의 블로그 만들기  (0) 2023.07.26

블로그의 정보

Hello 춘기's world

볼빵빵오춘기

활동하기