반응형
초보개발자 신씨
뉴가든's 개발한 생각
초보개발자 신씨
전체 방문자
오늘
어제
  • 개발한 생각 (28)
    • 웹개발 (5)
      • Spring & SpringBoot (5)
      • 웹개발 개론 (0)
    • CS이것저것 (0)
      • 자료구조 (0)
      • 알고리즘 (0)
      • DB (0)
      • 네트워크 (0)
    • 백준코딩테스트 (22)
    • IT끄적끄적 (0)

블로그 메뉴

  • 홈

공지사항

인기 글

태그

  • 코딩테스트
  • spring
  • Get
  • REST API
  • 백준
  • DI
  • 행렬 덧셈
  • 2738
  • HTTP
  • Dependency Injection
  • springboot
  • post
  • put
  • 의존성주입
  • Delete

최근 댓글

최근 글

티스토리

hELLO · Designed By 정상우.
초보개발자 신씨

뉴가든's 개발한 생각

웹개발/Spring & SpringBoot

REST API - (3) PUT METHOD 예제

2022. 10. 23. 14:50
반응형

PUT METHOD

HTTP PUT요청이 들어왔을 때 동작


CRUD 멱등성 안정성 PathVariable QueryParameter DataBody
C/U O X O △ O

멱등성: 같은 요청에 대해 여러번 반복해도 결과가 달라지지 않는 성질


Query Parameter : 데이터 처리하기위해 넘겨주는 매개변수(URL 중 ? 이후 문자열),  &로 변수를 구분

                                ex) /user?id=abcd123&pw=1234

 

※ Query Parameter가 △인 이유?

 해당 방식으로 데이터 생성할 수 있으나 일부 웹 서버는 URI길이가 제한 때문에,  Query Parameter가 부적절한 경우 有

 

Path Variable : 데이터를 처리하기 위해 URI경로에 변수를 넣어줌   

                          ex) /user/123

 

 

PUT 방식 요청에 대한 예제


package com.example.hello.controller;

import com.example.hello.dto.PutRequestDto;
import org.springframework.web.bind.annotation.*;

@RestController //API컨트롤러
@RequestMapping("/api")

public class PutApiController {

    @PutMapping("/put")
    public PutRequestDto put(@RequestBody PutRequestDto putRequestDto){
        System.out.println(putRequestDto);

        return putRequestDto;  //JSON으로 응답할 떄 @JsonProperty, @JsonNaming에 의한 네이밍으로 내려감
    }

    
    //path-variable 활용
    @PutMapping("/put/{userId}")
    public PutRequestDto putId(@RequestBody PutRequestDto putRequestDto, @PathVariable Long userId) {
        System.out.println(userId);
        return putRequestDto;

    }

}
package com.example.hello.dto;

import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.databind.PropertyNamingStrategy;
import com.fasterxml.jackson.databind.annotation.JsonNaming;


import java.util.List;

public class PutRequestDto {
    private String name;
    private int age;

    //@JsonNaming(value = PropertyNamingStrategy.SnakeCaseStrategy.class) 
    //class 전체 Json 스네이크타입으로 변경해줌
    
    @JsonProperty("car_list")
    private List<CarDto> carList;

    public String getName() {
        return name;
    }

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

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    public List<CarDto> getCarList() {
        return carList;
    }

    public void setCarList(List<CarDto> carList) {
        this.carList = carList;
    }

    @Override
    public String toString() {
        return "PutRequestDto{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", carList=" + carList +
                '}';
    }
}
package com.example.hello.dto;

import com.fasterxml.jackson.annotation.JsonProperty;

public class CarDto {
    private String name;
    @JsonProperty("car_number")
    private String carNumber;

    public String getName() {
        return name;
    }

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

    public String getCarNumber() {
        return carNumber;
    }

    public void setCarNumber(String carNumber) {
        this.carNumber = carNumber;
    }

    @Override
    public String toString() {
        return "CarDto{" +
                "name='" + name + '\'' +
                ", carNumber='" + carNumber + '\'' +
                '}';
    }
}

1)

 

2)

반응형

'웹개발 > Spring & SpringBoot' 카테고리의 다른 글

[Spring]의존성 주입(Dependency Injection, DI) - 미완  (0) 2022.11.05
REST API - (4) DELETE METHOD 예제  (0) 2022.10.24
REST API - (2) POST METHOD 예제  (0) 2022.10.23
REST API - (1) GET METHOD 예제  (0) 2022.10.23
    '웹개발/Spring & SpringBoot' 카테고리의 다른 글
    • [Spring]의존성 주입(Dependency Injection, DI) - 미완
    • REST API - (4) DELETE METHOD 예제
    • REST API - (2) POST METHOD 예제
    • REST API - (1) GET METHOD 예제
    초보개발자 신씨
    초보개발자 신씨
    공부한 내용을 공유하는 블로그

    티스토리툴바