[Practice] 공지사항 게시판 목록에 대한 요청 API 만들기 (5)

2021. 4. 12. 14:44Spring/Practice

1. 문제

  • REST API 형식으로 구현
  • HTTP METHOD는 GET
  • 요청 주소는 "/api/notice/count"
  • 리턴값은 게시판의 게시글 개수(정수)를 리턴
  • 단, 컨트롤러에서 정수형을 리턴하더라도 클라이언트에 내려가는 부분은 문자열

 

 

 

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.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;
    }
}
728x90