[Java/Spring] Spring으로 PUT Api 만들기

Hyeri Park·2022년 7월 13일
0

JAVA/Spring 기초

목록 보기
7/22
post-thumbnail
post-custom-banner

1. PUT API

  • annotation

2. PutMapping 예제 (1) - DTO

PostRequestDto.java

package com.example.put.dto;

import java.util.List;

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

//go
@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 + "]";
	}
	
	
}

PutApiController.java

package com.example.put;

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;

import com.example.put.dto.PostRequestDto;

@RestController
@RequestMapping("/api")
public class PutApiController{
	
	@PutMapping("/put")
	//public void put(@RequestBody PostRequestDto requestDto) { //리턴불가 
	public PostRequestDto put(@RequestBody PostRequestDto requestDto) {	
		System.out.println(requestDto);
		return requestDto;
	}
	
}

3. PutMapping 예제 (2) - @PathVariable

PutApiController.java

package com.example.put;

import org.springframework.web.bind.annotation.PathVariable;
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;

import com.example.put.dto.PostRequestDto;

@RestController
@RequestMapping("/api")
public class PutApiController{
	
	@PutMapping("/put/{userId}")
	public PostRequestDto put(@RequestBody PostRequestDto requestDto, @PathVariable(name = "userId") Long id) {	//pathVariable 예제 	
		System.out.println(id);
		return requestDto;
	}
	
}

profile
Backend Developer
post-custom-banner

0개의 댓글