[Springboot] RESTful POST 해보기!

@욕심쟁이

·

2021. 11. 6. 00:33

반응형

- Post는 PathVariable, DataBody에 데이터를 보내기 때문에 QueryParameter에 데이터를 보내지 않음.

 

1.  src > main > java > 패키지 > controller 패키지 생성 후 PostController.class 생성

package com.example.springboot202111.controller;

import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("/api")
public class PostController {

    @PostMapping("/post")
    void post(@RequestBody Map<String, Object> requestData){
		
        //entrySet() 사용시
        requestData.entrySet().forEach(stringObjectEntry -> {
            System.out.println("--------------------------------------------");
            System.out.println("value : " + stringObjectEntry.getValue() );
            System.out.println("key : " + stringObjectEntry.getKey() );
            System.out.println("--------------------------------------------");
        });
        
        requestData.forEach((key, value) -> {
            {
                System.out.println("--------------------------------------------");
                System.out.println("value : " + key);
                System.out.println("key : " + value);
                System.out.println("--------------------------------------------");
            };
        });
    }
}

2.  src > main > java > 패키지 > dto 생성 후 PostRequestDto생성

package com.example.springboot202111.dto;

import com.fasterxml.jackson.annotation.JsonProperty;

public class PostRequestDto {

    private String num;

    @JsonProperty("p_code") // 특정 이름에대해 매칭이 된다.
    private String pCode;

    private String name;

    public String getNum() {
        return num;
    }

    public void setNum(String num) {
        this.num = num;
    }

    public String getPCode() {
        return pCode;
    }

    public void setPCode(String pCode) {
        this.pCode = pCode;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "PostRequestDto{" +
                "num='" + num + '\'' +
                ", pCode='" + pCode + '\'' +
                ", name='" + name + '\'' +
                '}';
    }
}
package com.example.springboot202111.controller;

import com.example.springboot202111.dto.PostRequestDto;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Map;

@RestController
@RequestMapping("/api")
public class PostController {

    @PostMapping("/post")
    public void post(@RequestBody PostRequestDto postDTO){
        System.out.println(postDTO);
    }
}

반응형

'IT > Spring' 카테고리의 다른 글

[Spring] AOP란?  (0) 2022.01.22
[Spring] 주요 Annotation  (0) 2022.01.19
[Springboot] RESTful GET 해보기!  (0) 2022.01.18
[Springboot] 프로젝트 생성하기  (0) 2021.11.05