투케이2K

2. (spring/스프링) controller 컨트롤러 사용해 get, post api 생성 실시 본문

Spring

2. (spring/스프링) controller 컨트롤러 사용해 get, post api 생성 실시

투케이2K 2021. 7. 6. 11:57

/* =========================== */

[ 개발 환경 설정 ]

개발 툴 : inteli j

개발 언어 : spring

/* =========================== */

/* =========================== */

[폴더 및 파일 추가]

/* =========================== */

/* =========================== */

[소스 코드]

 

package com.project.solutionpackage.controller;

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

import javax.servlet.http.HttpServletRequest;
import java.util.ArrayList;
import java.util.Map;

@RestController
public class FormatApiController {

    // [get : Servlet 방식 : 경로 지정 : http://localhost:7000/get/servlet/format?idx=1&name=투케이]
    @GetMapping("/get/servlet/format")
    public String getServletUserInfo(HttpServletRequest request) {
        System.out.println("[FormatApiController] : [getServletUserInfo]");
        System.out.println("[request method] : " + String.valueOf(request.getMethod()));
        System.out.println("[request url] : " + String.valueOf(request.getRequestURL()));
        System.out.println("[request idx] : " + String.valueOf(request.getParameter("idx")));
        System.out.println("[request name] : " + String.valueOf(request.getParameter("name")));

        //리턴 값 반환 실시
        ArrayList arrayList = new ArrayList();
        arrayList.add("get");
        arrayList.add("Servlet");
        arrayList.add(request.getParameter("idx"));
        arrayList.add(request.getParameter("name"));
        String arr = String.valueOf(arrayList.toString());
        return String.valueOf(arr);
    }


    // [get : Map 방식 : 경로 지정 : http://localhost:7000/get/map/format?idx=1&name=투케이]
    @GetMapping("/get/map/format")
    public String getMapUserInfo(@RequestParam Map<String, String> param) {
        System.out.println("[FormatApiController] : [getMapUserInfo]");
        System.out.println("[request keySet] : " + String.valueOf(param.keySet()));
        System.out.println("[request idx] : " + String.valueOf(param.get("idx")));
        System.out.println("[request name] : " + String.valueOf(param.get("name")));

        //리턴 값 반환 실시
        ArrayList arrayList = new ArrayList();
        arrayList.add("get");
        arrayList.add("Map");
        arrayList.add(param.get("idx"));
        arrayList.add(param.get("name"));
        String arr = String.valueOf(arrayList.toString());
        return String.valueOf(arr);
    }


    // [post query param : Servlet 방식 : 경로 지정 : http://localhost:7000/post/servlet/format?idx=1&name=투케이]
    @PostMapping("/post/servlet/format")
    public String postServletUserInfo(HttpServletRequest request) {
        System.out.println("[FormatApiController] : [postServletUserInfo]");
        System.out.println("[request method] : " + String.valueOf(request.getMethod()));
        System.out.println("[request url] : " + String.valueOf(request.getRequestURL()));
        System.out.println("[request idx] : " + String.valueOf(request.getParameter("idx")));
        System.out.println("[request name] : " + String.valueOf(request.getParameter("name")));

        //리턴 값 반환 실시
        ArrayList arrayList = new ArrayList();
        arrayList.add("post");
        arrayList.add("Servlet");
        arrayList.add(request.getParameter("idx"));
        arrayList.add(request.getParameter("name"));
        String arr = String.valueOf(arrayList.toString());
        return String.valueOf(arr);
    }


    // [post query param : Map 방식 : 경로 지정 : http://localhost:7000/post/map/format?idx=1&name=투케이]
    @PostMapping("/post/map/format")
    public String postMapUserInfo(@RequestParam Map<String, String> param) {
        System.out.println("[FormatApiController] : [postMapUserInfo]");
        System.out.println("[request keySet] : " + String.valueOf(param.keySet()));
        System.out.println("[request idx] : " + String.valueOf(param.get("idx")));
        System.out.println("[request name] : " + String.valueOf(param.get("name")));

        //리턴 값 반환 실시
        ArrayList arrayList = new ArrayList();
        arrayList.add("post");
        arrayList.add("Map");
        arrayList.add(param.get("idx"));
        arrayList.add(param.get("name"));
        String arr = String.valueOf(arrayList.toString());
        return String.valueOf(arr);
    }


    // [post body json]
    // [경로 지정 : http://localhost:7000/post/body/format]
    // [body data : {"idx":1, "name":"투케이"}]
    @PostMapping("/post/body/format")
    public String postBodyUserInfo(@RequestBody Map<String, String> param) {
        System.out.println("[FormatApiController] : [postBodyUserInfo]");
        System.out.println("[request keySet] : " + String.valueOf(param.keySet()));
        System.out.println("[request idx] : " + String.valueOf(param.get("idx")));
        System.out.println("[request name] : " + String.valueOf(param.get("name")));

        //리턴 값 반환 실시
        ArrayList arrayList = new ArrayList();
        arrayList.add("post");
        arrayList.add("body");
        arrayList.add(param.get("idx"));
        arrayList.add(param.get("name"));
        String arr = String.valueOf(arrayList.toString());
        return String.valueOf(arr);
    }

}

/* =========================== */

/* =========================== */

[결과 출력]

​[get 방식 : 쿼리 파람]

​[post 방식 : 쿼리 파람]

​[post 방식 : body json]

/* =========================== */

반응형
Comments