key와 value가 정해져있지만, 받아야할 파라미터가 많을 경우 DTO 객체를 사용한 방식
com.example.testproject.controller
package com.example.testproject.controller;
import com.example.testproject.dto.MemberDTO;
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;
@RestController
@RequestMapping("/api/v1/post-api")
public class PostController {
@PostMapping(value="/default")
public String postMethod(){
return "Hello world";
}
// http://localhost:8080/api/v1/post-api/member
@PostMapping(value="/member")
public String postMember(@RequestBody Map<String, String> postData){
StringBuilder sb=new StringBuilder();
postData.entrySet().forEach(map ->{
sb.append(map.getKey() + " : "+ map.getValue() + "\n");
});
//param.forEach((key,value-> sb.append(key).append(":").append(value).append("\n"))
return sb.toString();
}
//http://localhost:8080/api/v1/post-api/member2
@PostMapping(value="/member2")
public String postMemberDto(@RequestBody MemberDTO memberDTO){
return memberDTO.toString();
}
}
package com.example.testproject.dto;
public class MemberDTO {
private String name;
private String email;
private String organization;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getEmail() {
return email;
}
public void setEmail(String email) {
this.email = email;
}
public String getOrganization() {
return organization;
}
public void setOrganization(String organization) {
this.organization = organization;
}
@Override
public String toString() {
return "MemberDTO{" +
"name='" + name + '\'' +
", email='" + email + '\'' +
", organization='" + organization + '\'' +
'}';
}
}