본문 바로가기
Spring

HTTP Method RequestBody 실습

by 코린이엄현종 2024. 3. 20.

PostController 클래스 생성

package com.eom.controllerexercise.controller;

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

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

@RestController
@RequestMapping(value = "/post")
public class PostController {

    @RequestMapping(method = GET)
    public String getPost() {
        return "GET /post";
    }

    @RequestMapping(method = POST)
    public String savePost() {
        return "POST /post";
    }

    @RequestMapping(method = PUT)
    public String updatePost() {
        return "PUT /post";
    }

    @RequestMapping(method = DELETE)
    public String deletePost() {
        return "DELETE /post";
    }
}

 

 

Postman

 

GET

 

 

POST

 

 

PUT

 

 

DELETE

 

 


 

 

package com.eom.controllerexercise.controller;

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

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

@RestController
@RequestMapping(value = "/post")
public class PostController {

    @GetMapping
    public String getPost() {
        return "GET /post";
    }

    @PostMapping
    public String savePost() {
        return "POST /post";
    }

    @PutMapping
    public String updatePost() {
        return "PUT /post";
    }

    @DeleteMapping
    public String deletePost() {
        return "DELETE /post";
    }
}

 

위와 같이 동일하게 작동

 


 

RequestBody

 

 

DTO 패키지에 PostDto 클래스 생성

package com.eom.controllerexercise.dto;

public class PostDto {
    Integer id;
    String title;
    String content;
    String username;

    public PostDto(Integer id, String title, String content, String username) {
        this.id = id;
        this.title = title;
        this.content = content;
        this.username = username;
    }

    public Integer getId() {
        return id;
    }

    public String getTitle() {
        return title;
    }

    public String getContent() {
        return content;
    }

    public String getUsername() {
        return username;
    }
}

 

 

PostController 클래스 수정

package com.eom.controllerexercise.controller;

import com.eom.controllerexercise.dto.PostDto;
import org.springframework.web.bind.annotation.*;

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

@RestController
@RequestMapping(value = "/post")
public class PostController {

    @GetMapping
    public String getPost() {
        return "GET /post";
    }

    @PostMapping
    public String savePost(@RequestBody PostDto postDto) {
        System.out.println(postDto.getId());
        System.out.println(postDto.getTitle());
        System.out.println(postDto.getContent());
        System.out.println(postDto.getUsername());

        return "POST /post";
    }

    @PutMapping
    public String updatePost() {
        return "PUT /post";
    }

    @DeleteMapping
    public String deletePost() {
        return "DELETE /post";
    }
}

 

POST 나 PUT Method 같은 경우에는 대용량의 데이터, 장문의 게시물 이러한 큰 용량이 들어올 수 있고

그것을 RequestBody로 전송을 할 수가 있고 그럴 경우에는 객체를 JSON 데이터를 받을 수 있는데 그 앞에는

RequestBody 라는 Annotation 사용

 

 

JSON 형식의 문법으로 작성 key 부분에 "" 잊지 말기 

 

 

console 부분에 잘 작동 하는 모습

'Spring' 카테고리의 다른 글

Spring Service  (0) 2024.03.21
REST API 문서의 활용  (0) 2024.03.20
RequestBody  (0) 2024.03.20
HTTP Method  (0) 2024.03.20
Response 데이터와 JSON 포맷 실습  (0) 2024.03.20