JSON 방식으로 데이터를 전송 방식
string : value
number : value
boolean : value
object : value {} 객체
array : value [] 배열
{
"phone_number" : "value", // snake case 주로 사용
"phoneNumber" : "value", // camel case 통상 사용하지 않지만 사용 할 수 있으므로 데이터를 받을 수 있게 작업해야한다.
{
"phone_number" : "010-1111-2222", // 문자열
"age" : 15, //숫자
"isAgree" : false, // boolean
"account" {
"email" : "steve@gmail.com",
"password" : "1234"
} //객체
}
// User 조회하는 경우
{
"user_list" : [
{"account" : " abcd",
"password" : "1234"
},
{"account" : " efgh",
"password" : "5678"
},
{"account" : " ijkl",
"password" : "9012"
}
] // 배열 : 같은 형태의 값이 반복
}
위와 같은 형태의 JSON파일로 데이터를 받아올 수 있다.
어떠한 형태의 값인지 알 수 있다면 단순하게 key값과 value 값을 받아올 수 있지만 항목이 다르거나 여러개일 경우 문제가 발생한다.
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 java.util.Map;
@RequestMapping("/api") //주소를 정해줌
@RestController // 요청제어 선언
public class PostApicontroller {
@PostMapping("/post")//주소를 정해줌
//@RequestBody 를 통해서 JSON 데이터를 파싱할 수 있다.
public void post(@RequestBody Map<String, Object> requestData){
requestData.forEach((key, value) -> {
System.out.println("key:" + key);
System.out.println("value:" + value);
});
}
}
DTO데이터 전송 객체(Data transfer object)를 생성 후 getter 와 sette를 작성하여 값을 설정 할 수 있게 한다.
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;
}
}
toString를 생성하여 값을 받고 전송하여 준다.
// 이후 IntelliJ가 제공하는 기능으로 toString를 생성한다.
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 + '\'' +
'}';
}
}
위의 key와 value대신에 DTO를 통해서 값을 받아 출력한다.
데이터를 받아서 출력한다.
package com.example.post.controller;
import com.example.post.dto.PostRequestDto;
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 java.util.Map;
@RequestMapping("/api")
@RestController
public class PostApicontroller {
@PostMapping("/post")
public void post(@RequestBody PostRequestDto requestData){
System.out.println(requestData);
}
}
Annotation 정리
@RestControll - Rest API 설정
@RequsetMapping - 리소스를 설정
@PostMapping - Post Resource 설정
@RequestBody - Request Body 부분 Parsing
@PathVariable - URL Path Variable Parsing
@JsonProperty - json naming
@JsonNaming - class json naming
DELETE API (0) | 2022.08.17 |
---|---|
PUT API (0) | 2022.08.17 |
Get API (0) | 2022.08.16 |
SpringBoot를 사용하여 "hello spring boot" 출력하기 (0) | 2022.08.16 |
스프링 부트 란? (0) | 2022.08.04 |