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

Hyeri Park·2022년 7월 12일
0

JAVA/Spring 기초

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

1. POST API

  • annotation

2. Spirng으로 api 만들기

package com.example.post.controller;

import java.util.Map;

import org.springframework.web.bind.annotation.PostMapping;
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 PostApiController {
	
	@PostMapping("/post")
	public void post(@RequestBody Map<String, Object> requestData) {
		
        // 방법 1
		requestData.entrySet().forEach( t -> {
			System.out.println("key : " + t.getKey());
			System.out.println("value : " + t.getValue());
		});
		
        // 방법2
//		requestData.forEach((key,value) -> {
//			System.out.println(key);
//			System.out.println(value);
//		});
	}

}

Headers/Content-Type : application/json

3. PostMapping 예제 (1) - DTO

1) PostRequestDto.java

package com.example.post.dto;

public class PostRequestDto {
	private String account;
	private String email;
	private String address;
	private String password;
	public String getAccount() {
		return account;
	}
	public void setAccount(String account) {
		this.account = account;
	}
	public String getEmail() {
		return email;
	}
	public void setEmail(String email) {
		this.email = email;
	}
	public String getAddress() {
		return address;
	}
	public void setAddress(String address) {
		this.address = address;
	}
	public String getPassword() {
		return password;
	}
	public void setPassword(String password) {
		this.password = password;
	}
	
	@Override
	public String toString() {
		return "PostRequestDto [account=" + account + ", email=" + email + ", address=" + address + ", password="
				+ password + "]";
	}
}

2) PostApiController.java

package com.example.post.controller;



import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.example.post.dto.PostRequestDto;

@RestController
@RequestMapping("/api")
public class PostApiController {
	
	@PostMapping("/post")

	
	// DTO
	public void post(@RequestBody PostRequestDto requestData) {
		System.out.println(requestData.toString());
		System.out.println("\n");
		System.out.println(requestData);
	}

}

4. PostMapping 예제 (2) - JsonProperty

@JsonProperty - 변수 매핑 방법

1) PostRequestDto.java

package com.example.post.dto;

import com.fasterxml.jackson.annotation.JsonProperty;

public class PostRequestDto {
	private String account;
	private String email;
	private String address;
	private String password;
	
	@JsonProperty("phone_number")
	private String phoneNumber;

위와같이 phone_number를 snakecase로 요청을했을때
DTO에 carmelcase로 매핑을 시키는 방법은 @JsonProperty를 이용하면된다.

profile
Backend Developer
post-custom-banner

0개의 댓글