[패스트캠퍼스]스프링부트프로젝트-어드민페이지만들기-SpringBoot란

김주현·2021년 7월 1일
0

Springboot

목록 보기
1/3

SpringBoot란

스프링 프레임워크 기반 프로젝트를 어려운 설정이나 WAS에 대한 설정 없이 바로 개발에 들어갈 수 있게 만든 프레임워크이다.
스프링 프레임워크를 사용하려면 많은 XML설정 파일들을 작성해야하고, 기존에 사용했던 설정들을 복붙하거나 검색을 통해 일일이 설정을 해야하나 스프링 부트를 사용하면 복잡한 설정 없이 쉽고 빠르게 프레임워크를 사용할 수 있다.

HTTP Method

HTTP Method란? 클라이언트와 서버 사이에 이루어지는 요청과 응답데이터를 전송하는 방식
GET이나 POST는 매우 자주 쓰는 HTTP 메소드들이다.

HTTP Method - GET

Get Method 예제

1) controller패키지에 GetController클래스 생성
주소들의 묶음을 controller라고 한다.

package com.example.study.controller;

import com.example.study.model.SearchParam;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api")  // Localhost:8090/api ,로컬호스트의 8080포트의 api까지 주소가 매칭됨
public class GetController {

    //localhost:8090/api/getMethod 주소로 들어가면 Hi getMethod를 리턴함
    @RequestMapping(method= RequestMethod.GET, path ="/getMethod")
    public String getRequest(){

        return "Hi getMethod";
    }

    //변수이름을 pwd로 하면 스프링에서의 password와 이름이 달라서 매칭이 되지 않음
    //requestparam에서 이름을 password로 지정해야 주소창에 있는 파라미터와 서로 매칭이 되서 pwd로 세팅이 된다.
    @GetMapping("/getParameter")  // localhost:8090/api/getParameter?id=1234&password=abcd
    public String getParameter(@RequestParam String id, @RequestParam(name="password") String pwd) {

        String password="bbbb";
        System.out.println("id : " + id);
        System.out.println("pwd : " + pwd);
        return id + pwd;
    }
    //localhost:8090/api/getMultiParameter?account=abcd&email=study@gmail.com&page=10
    @GetMapping("/getMultiParameter")
    public String getMultiParamter(SearchParam searchParam){
        System.out.println(SearchParam.getAccount());
        System.out.println(SearchParam.getEmail());
        System.out.println(SearchParam.getPage());

        return "OK";
    }



}

2)model패키지에 SearchParam클래스 생성

package com.example.study.model;

import org.springframework.web.servlet.view.feed.AbstractAtomFeedView;

// get,set을 하기 위해서는
// run -> console-> generate로 이동한 다음 getter and setter 선택하면 자동생성됨
public class SearchParam {

    private static String account;
    private static String email;
    private static int page;

    public static String getAccount() {
        return account;
    }

    public void setAccount(String account) {
        this.account = account;
    }

    public static String getEmail() {
        return email;
    }

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

    public static int getPage() {
        return page;
    }

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

결과1 : console창에 순서대로 print한 값이 출력되며 사이트에는 리턴값 OK가 출력된다.

결과2 : 다음과 같이 수정해주면 json형태로 출력된다.

📌참고사이트
https://pp-ppi.tistory.com/39
https://im-developer.tistory.com/166
https://velog.io/@taeha7b/api-restapi-restfulapi

0개의 댓글

관련 채용 정보