본문 바로가기
Spring

Spring PostController

by code2772 2022. 12. 8.

[ 목차 ]

    728x90
    반응형

    ✔ post방식의 postController

    package com.koreait.day2.controller;
    
    import com.koreait.day2.model.Member;
    import org.springframework.web.bind.annotation.*;
    
    @RestController // rest로 호출할 수 있는 기능이다. url 호출기능이다
    @RequestMapping("/api")  // http://localgost:8888/api
    public class PostContorller { // url로 호출을하면 get방식이다.  - > url 사용
        // http://localhost:8888/api/postmethod
        @RequestMapping(method = RequestMethod.POST, path ="/postmethod")
        public String postMethod(){
            return  "postMethod() 호출!";
        }
    
        //url을 사용하요 포스트 방식에서 출력을 하고 싶을면 form이나 postman을 사용하여 확인을 해야한다.

    ReqysetMethod.POST 방식으로 사용한다. Get방식과 다르게 브라우저 localhost 사용이 바로는 불가능 하다

    URL 사용하기 위해서는 FORM이나 POSTMAN을 사용하여 결과를 확인한다.

    ✔ post 방식또한 변수를 선언한다. 

    // http://localhost:8888/api/postparameter
    @RequestMapping(method = RequestMethod.POST, path = "/postparameter")
    public String postParameter(@RequestParam String userid, @RequestParam String userpw){
        System.out.println("userid: " + userid);
        System.out.println("userpw: " + userpw);
        return "postParameter() 호출";
    }
    

    -> http://localhost:8888/api/postparameter 뒤에 따로 변수를 선언하지 않고 postman 에서 직접 입력을 한다.

    ✔ post 방식에서도 member 클래스 사용이 가능하다.

    // http://localhost:8888/api/postmultiparamer
    @PostMapping("postmultiparamer")
    public Member postMultiParamer(@RequestBody Member member){ // post는 객체를 보낼 때 json으로
        //@RequestBody Member member 사용 post 사용간에는
        System.out.println(member);
        return member;
    }
    반응형