빽기의 코딩공부Story

[HTTP] Get Method, Get방식 본문

[SPRING-BOOT]/AdminPage_Project

[HTTP] Get Method, Get방식

빽기 2020. 7. 21. 15:23

● Get Method 란

 - 주소창에 파라미터가 노출된다.

 - 브라우저에서 주소에 대한 캐시가 이루어 지며 정보를 얻을 때 사용한다.

 - 전송 가능 데이터 길이 제한이 있다.

 - 보안에 취약하다.

 - 전송 속도가 POST에 비해 빠르다.

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

@RestController // 어노테이션 선언
@RequestMapping("/api") //localhost:8080/api 주소 맵핑
public class GetController {
                                                        //방식 선언
    @RequestMapping (method = RequestMethod.GET, path = "/getMethod") // //localhost:8080/api/getMethod  주소 맵핑
    public String getRequest() {

        return "GET Method 공부 중입니다."; // 문자열 리턴  
    }
}

 

프로젝트 실행

 

사용자가 http://localhost:8080/api/getMethod 주소로 들어오면 return한다.

 

● Get Method - Parameter 통신

    @GetMapping("/getParameter")//localhost:8080/api/getParameter?id=qwe123&password=1q2w3e4r 주소 맵핑
    public String getParameter(@RequestParam String id, @RequestParam(name = "password")String pw){
                                                                    //파라미터에 password란 이름을 지정 할 수 있다.
        System.out.println("아이디는"+ id + "패스워드는"+ pw);

        return id + pw;

    }

- 넘어오는 파라미터에 이름을 지정하여 변경 해줄 수 있다.

※ RequestParam를 계속해서 선언 할 경우 코드 가독성이 떨어진다

넘어오는 파라미터가 10개 이상이면

매번 RequestParam을 선언 할수는 없다.

이러한 경우 모델(Model) 객체를 활용하여 받아올수 있다.

 

 Get Method -  다중 Parameter 통신

패키지 생성
model 패키지에서 class파일 생성

public class SearchParam {

    private String id;
    private String email;
    private int page;

    public String getId() {
        return id;
    }

    public void setId(String id) {
        this.id = id;
    }

    public String getEmail() {
        return email;
    }

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

    public int getPage() {
        return page;
    }

    public void setPage(int page) {
        this.page = page;
    }
}

- 변수 선언과 Getter, Setter메서드를 생성한다.

    //다중 파리미터 받기
    @GetMapping("/getMultiParameter")
    public String getMultiParameter(SearchParam searchParam){
        
        return  "아이디는"+searchParam.getId() + 
                "이메일은"+ searchParam.getEmail() + 
                "페이지는"+ searchParam.getPage();

- 컨트롤러로 돌아와 생성한 class파일을 RequestParam으로 선언한다.

정상적으로 파라미터를 받아 리턴

 

Comments