스프링부트 입문 5. PUT API

min seung moon·2021년 6월 26일
0

Spring

목록 보기
22/50
post-thumbnail

1. 프로젝트 세팅

  • Package : controller
  • Class : PutApiController

2. 프로젝트 작성

01. DTO

  • Package : dto
  • Class : PostRequestDto, CarDto
  • PostRequestDto.java
package com.example.PUT.dto;

import java.util.List;

public class PostRequestDto {

    private String name;
    private int age;
    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.java
package com.example.PUT.dto;

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

02. Contoller 작성

  • PutApiContoller.java
package com.example.PUT.controller;

import com.example.PUT.dto.PostRequestDto;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

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

    @PutMapping("/put")
    public void put(@RequestBody PostRequestDto requestDto) {
        System.out.println(requestDto);
    }
}



03. DTO 수정

  • 식별자가 달라서 매칭이 안되어 누락된 부분 수중

-1. 어노테이션

  • @JsonNaming(value = PropertyNamingStrategy.SnakeCaseStrategy.class)
    • naming 룰 설정
    • 일괄적으로 naming 룰 수정
  • @JsonProperty("car_number")
    • 부분적으로 naming 설정

-2. 코드 수정

  • PostRequestDto.java
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)
public class PostRequestDto {

    private String name;
    private int age;
    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.java
package com.example.PUT.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 + '\'' +
                '}';
    }
}



04. Controller Response(객체 값 전달, return)

  • @RestController
    • Object 자체를 return 시키면 Spring Boot 자체에서 오브젝트를 Object Mapper로 제이슨으로 바꿔준다
  • PushApiController.java
package com.example.PUT.controller;

import com.example.PUT.dto.PostRequestDto;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

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

    @PutMapping("/put")
    public PostRequestDto put(@RequestBody PostRequestDto requestDto) {
        System.out.println(requestDto);
        return requestDto;
    }
}


05. @PathVariable 어노테이션 적용

  • PushApiController.java
package com.example.PUT.controller;

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

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

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



3. PUT 어노테이션

profile
아직까지는 코린이!

0개의 댓글