[Spring Boot] PUT API

이맑음·2021년 9월 15일
0

Spring Boot

목록 보기
5/21
post-thumbnail

PUT API

  • 리소스 갱신, 생성
  • CRUD 중 Create, Update
  • 멱등성을 가진다. 처음 한번은 데이터가 생성되고, 그 이후에는 계속해서 업데이트 되기 때문에 데이터는 항상 하나이다. 즉 항상 같은 상태를 유지하기 때문에 멱등하다고 할 수 있다.
  • Path Variable, Query Parameter(body에 데이터를 담기 때문에 굳이 사용할 필요가 없다), DataBody

예제

@JsonNaming(value = PropertyNamingStrategy.SnakeCaseStrategy.class)
public class CarDto {

    private String name;
    private String carNumber;

    // getter/setter, toString 생략..
}

// dto/PutReqeustDto
@JsonNaming(value = PropertyNamingStrategy.SnakeCaseStrategy.class)
public class PutRequestDto {

    private String name;
    private int age;
    private List<CarDto> carList;
    
    // getter/setter, toString 생략..
}

// controller/PutApiController
@RestController
@RequestMapping("/api")
public class PutApiController {

    @PutMapping("/put/{userId}")
    public PutRequestDto put(@RequestBody PutRequestDto requestDto @PathVariable(name = "userId") Long id) {
        System.out.println(requestDto);
        System.out.println(id);

        return requestDto;

    }
}
  • Talend Api Test
  • Talend Api Response
  • 서버
  • @RestController : Rest API 설정
  • @RequestMapping : 리소스 설정(Method로 구분 가능)
  • @PutMapping : Put Resource 설정
  • @RequestBody : Request Body Parsing
  • @PathVariable : URL Path Variable Parsing
    - Talend Api Test

    - 서버

1개의 댓글

comment-user-thumbnail
2022년 8월 11일

글 잘 봤습니다 !! 감사합니다 :)

답글 달기
Powered by GraphCDN, the GraphQL CDN