본문 바로가기
Spring/이론

post방식--등록할 때 사용//talend Api//@RequestBody()

by SEOKIHOUSE 2023. 7. 31.
  • POST API  웹 애플리케이션을 통해 데이터베이스 등의 저장소에 리소스를 저장할 때 사용하는 API


-JSON형태를 써야할 경우
@RequestBody 어노테이션을 사용하면 클라이언트가 보낸 데이터를 간편하게 자바 객체로 변환할 수 있으므로, JSON, XML 등의 데이터를 처리하는 웹 서비스를 개발할 때 유용하게 사용됩니다.

 

  • post는 url에 적는게 아니라 body에 적어야한다


 

RequestBody안적으면 null이 뜬다


 

practice0731_ApiTest.zip
0.09MB

package com.study.springboot;

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

import com.study.springboot.dto.MemberDto;

@RestController
@RequestMapping("/api/v1/post-api")
public class PostController {
	
	@RequestMapping(value = "/domain", method = RequestMethod.POST)
	public String postExample() {
		return "Hello postAPI";
	}
	
	@PostMapping(value="/member")
	public String postMember(@RequestBody Map<String, Object> postData) {
		StringBuilder sb = new StringBuilder();
		postData.entrySet().forEach(map -> {
			sb.append(map.getKey() + " : " + map.getValue() + "\n");
		});
		
		return sb.toString();
	}
	
	@PostMapping(value="/member2")
	public String postMemberDto(@RequestBody MemberDto member) {
		return member.toString();
	}
}