상세 컨텐츠

본문 제목

PUT API

Back-end/JavaSpringBoot

by 본투비곰손 2022. 8. 17. 23:17

본문

728x90

리소스의 갱신 , 생성 (멱등성, 안정적이지 않음) 데이터는 하나이고 계속 업데이트 한다.

post와 동일하게 JSON을 사용 한다.

 

 

package com.example.put;


import com.example.put.dto.PostRequestDto;
import org.springframework.web.bind.annotation.*;

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

    @PutMapping("/put/{userId}") // put 주소를 매핑해준다.
    //{}안의 userId 는 변수로 아래 userId와 일치해야한다. 아래 userId를 사용하지 못 할 경우
    // @PathVariavle(name="userId")로 선언 후 다른 변수로 사용한다.
    public PostRequestDto put(@RequestBody PostRequestDto requestDto, @PathVariable long userId){
        System.out.println(userId);
        return requestDto; 
        // spring boot 특히 @RestController에서는 오브젝트를 리턴하고 
        // 설정된 Property로 변환하여 JSON으로 변환되어 response(응답) 한다.
    }

}

post와 동일 하게 DTO를 작성해준다. (DTO안에 다른 객체를 만들어서 객체를 받을 수 도 있다.)

package com.example.put.dto;

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

import java.util.List;
@JsonNaming(value = PropertyNamingStrategy.SnakeCaseStrategy.class)
// camelcase를 인식 하지 못 하기 때문에 snake case 로 일괄 변환 하여준다. 
public class PostRequestDto {

    private String name;
    private int age;
	//@JsonPropety(value = 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 "PostRequestDto{" +
                "name='" + name + '\'' +
                ", age=" + age +
                ", carList=" + carList +
                '}';
    }
}

CarDto 객체를 만들고 동일하게 겟셋을 만들어 준다.

package com.example.put.dto;

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

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

    private String name;

    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 + '\'' +
                '}';
    }
}

JSON 을 동일하게 디자인하여 생성하고 전송 하면 된다 

{
"name": "steve",
"age": 20,
"car_list":[
{
"name": "bmw",
"car_number": "11가 1234"
},
{
"name": "a5",
"car_number": "12나 3566"
}
]
}
728x90

'Back-end > JavaSpringBoot' 카테고리의 다른 글

DELETE API  (0) 2022.08.17
POST API  (0) 2022.08.16
Get API  (0) 2022.08.16
SpringBoot를 사용하여 "hello spring boot" 출력하기  (0) 2022.08.16
스프링 부트 란?  (0) 2022.08.04

관련글 더보기