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
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);
}
}
@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를 이용하면된다.