[Practice] 공지사항 게시판 목록에 대한 요청 API 만들기 (7)
2021. 4. 12. 15:00ㆍSpring/Practice
1. 문제
- REST API 형식으로 구현
- HTTP METHOD는 POST
- 요청 주소는 "/api/notice"
- 전달되는 파라미터는 x-www-form-urlencoded 형식의 제목, 내용을 입력 받음
- 파라미터를 공지사항 모델로 추상화하여 전달받음
- 리턴값은 입력된 형태에 게시글ID(2)과 등록일자(현재시간)을 추가하여 모델 형태로 리턴
2. 풀이
- ApiNoticeController.java
package com.example.jpa.sample.notice.controller;
import com.example.jpa.sample.notice.model.NoticeModel;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
@RestController
public class ApiNoticeController {
/*
// 문제 6
@GetMapping("/api/notice")
public String noticeString() {
return "공지사항입니다.";
}
*/
/*
// 문제 7
@GetMapping("/api/notice")
public NoticeModel notice() {
LocalDateTime regDate = LocalDateTime.of(2021, 1, 1, 0, 0);
NoticeModel notice = new NoticeModel();
notice.setId(1);;
notice.setTitle("공지사항입니다.");
notice.setContents("공지사항 내용입니다.");
notice.setRegDate(regDate);
return notice;
}
*/
/*
// 문제 8
@GetMapping("/api/notice")
public List<NoticeModel> notice() {
List<NoticeModel> noticeList = new ArrayList<>();
noticeList.add(NoticeModel.builder()
.id(1)
.title("공지사항입니다.")
.contents("공지사항 내용입니다.")
.regDate(LocalDateTime.of(2021, 1, 1, 0, 0))
.build()
);
noticeList.add(NoticeModel.builder()
.id(2)
.title("두번째 공지사항입니다.")
.contents("두번째 공지사항 내용입니다.")
.regDate(LocalDateTime.of(2021, 1, 2, 0, 0))
.build()
);
return noticeList;
}
*/
// 문제 9
@GetMapping("/api/notice")
public List<NoticeModel> notice() {
List<NoticeModel> noticeList = new ArrayList<>();
// return null;
return noticeList;
}
// 문제 10
@GetMapping("/api/notice/count")
public int noticeCount() {
// return "10";
return 10;
}
/*
// 문제 11
@PostMapping("/api/notice")
public NoticeModel addNotice(@RequestParam String title, @RequestParam String contents) {
NoticeModel notice = NoticeModel.builder()
.id(1)
.title(title)
.contents(contents)
.regDate(LocalDateTime.now())
.build();
return notice;
}
*/
// 문제 12
@PostMapping("/api/notice")
public NoticeModel addNotice(NoticeModel noticeModel) {
noticeModel.setId(2);
noticeModel.setRegDate(LocalDateTime.now());
return noticeModel;
}
}
728x90
'Spring > Practice' 카테고리의 다른 글
[Practice] 공지사항 게시판 목록에 대한 요청 API 만들기 (9) (0) | 2021.04.12 |
---|---|
[Practice] 공지사항 게시판 목록에 대한 요청 API 만들기 (8) (0) | 2021.04.12 |
[Practice] 공지사항 게시판 목록에 대한 요청 API 만들기 (6) (0) | 2021.04.12 |
[Practice] 공지사항 게시판 목록에 대한 요청 API 만들기 (5) (0) | 2021.04.12 |