Notice
Recent Posts
Recent Comments
Link
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | ||||
4 | 5 | 6 | 7 | 8 | 9 | 10 |
11 | 12 | 13 | 14 | 15 | 16 | 17 |
18 | 19 | 20 | 21 | 22 | 23 | 24 |
25 | 26 | 27 | 28 | 29 | 30 | 31 |
Tags
- #devops #terraform #state
- 개발자
- 인텔리제이
- MariaDB
- 제로베이스 #백엔드 #Java #Spring #개발자 #백엔드공부 #백엔드 스쿨
- DAO 설계
- 스프링부트실전가이드
- 백엔드스쿨
- 제로베이스
- validated
- 백엔드
- 리포지토리 인터페이스
- spring
- 유효성검사
- JPA
- 프로젝트 생성
- Swagger
- 데이터베이스 연동
- ORM
- DAO 연동 컨트롤러 서비스 설계
- 백엔드공부
- Java
- 엔티티 설계
- 스프링 부트 핵심 가이드
- auditing
Archives
- Today
- Total
JeongJin's Blog
05. API를 작성하는 다양한 방법 (2) 본문
5.3 POST API 만들기
- 데이터 베이스 등의 저장소에 리소스를 저장할 때 사용하는 API
- 소스
- Map을 사용하는 이유는 어떤 데이터가 요청될지 모르기 때문에 사용
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/v1/post-api")
public class PostController {
@PostMapping("/member")
public String postMember(
@RequestBody Map<String, String> postData
) {
StringBuilder sb = new StringBuilder();
postData.entrySet().forEach(map -> {
sb.append(map.getKey() + ":" + map.getValue());
});
return sb.toString();
}
}
- 결과
- 요청데이터가 Dto 인 경우
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.zerobase.account.dto.MemberDto;
@RestController
@RequestMapping("/api/v1/post-api")
public class PostController {
@PostMapping("/member2")
public String postMember2(
@RequestBody MemberDto memberDto
) {
return memberDto.toString();
}
}
- 결과
- DTO 객체를 반환하는 요청
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.zerobase.account.dto.MemberDto;
@RestController
@RequestMapping("/api/v1/post-api")
public class PostController {
@PostMapping("/member3")
public MemberDto postMember3(
@RequestBody MemberDto memberDto
) {
return memberDto;
}
}
- 결과
- Json 형태로 응답을 준다.
'Book Study > 스프링 부트 핵심 가이드' 카테고리의 다른 글
06. 데이터베이스 연동 (1) (0) | 2023.10.30 |
---|---|
05. API를 작성하는 다양한 방법 (4) (0) | 2023.10.23 |
04. 프로젝트 생성 (2) | 2023.10.23 |
05. API를 작성하는 다양한 방법 (5) (0) | 2023.10.23 |
05. API를 작성하는 다양한 방법 (3) (0) | 2023.10.23 |