컨트롤러에서 여러 개의 Dto를 한 번에 반환하기

aj4941·2023년 7월 18일
0

한 컨트롤러에서 Dto를 여러 개 반환하고 싶었는데 일반적으로 하나만 반환하도록 했었다.
이를 해결하기 위해서 Map을 사용하였고 다음과 같이 해결할 수 있었다.

@RestController
@RequestMapping("/members")
@RequiredArgsConstructor
public class MemberController {

    private final AttemptProblemService attemptProblemService;
    private final TestService testService;
    @GetMapping("/login")
    public ResponseEntity<Map<String, Object>> memberInfo(@RequestParam Long memberId) {
        List<GrassDto> grassDtos =
                attemptProblemService.getGrassDtosByMemberId(memberId);
        List<TestRecordDto> testRecordDtos
                = testService.getTestRecordDtosByMemberId(memberId);
        List<GraphDto> graphDtos =
                attemptProblemService.getGraphDtosByMemberId(memberId);
        List<TestRatingDto> testRatingDtos
                = testService.getTestRatingDtosByMemberId(memberId);

        Map<String, Object> responseData = new HashMap<>();
        responseData.put("grass", grassDtos);
        responseData.put("testRecord", testRecordDtos);
        responseData.put("graph", graphDtos);
        responseData.put("testRating", testRatingDtos);

        return new ResponseEntity<>(responseData, HttpStatus.OK);
    }
}

이를 통해 테스트로 데이터가 잘 출력되는 것을 확인해본 결과 성공적으로 잘 반환된 것을 알 수 있었다.

{
    "testRating": [
        {
            "testDate": "2023-07-18",
            "testTypeName": "Practice Test 1",
            "testRating": 1500
        },
        {
            "testDate": "2023-07-19",
            "testTypeName": "Practice Test 2",
            "testRating": 1600
        }
    ],
    "grass": [
        {
            "testDate": "2023-07-19",
            "solvedCount": 3
        },
        {
            "testDate": "2023-07-18",
            "solvedCount": 3
        }
    ],
    "graph": [
        {
            "algorithmName": "dp",
            "solvedRate": 0.5714285714285714
        },
        {
            "algorithmName": "그리디",
            "solvedRate": 0.6666666666666666
        },
        {
            "algorithmName": "완전탐색",
            "solvedRate": 0.5714285714285714
        }
    ],
    "testRecord": [
        {
            "testDate": "2023-07-18T10:00:00",
            "testTime": 3600,
            "problemCount": 20,
            "startDifficulty": 3,
            "endDifficulty": 6,
            "testTypename": "Practice Test 1",
            "testRating": 1500,
            "solvedInfo": {
                "1": true,
                "2": true,
                "3": true,
                "4": false,
                "5": false
            }
        },
        {
            "testDate": "2023-07-19T14:30:00",
            "testTime": 4200,
            "problemCount": 15,
            "startDifficulty": 4,
            "endDifficulty": 7,
            "testTypename": "Practice Test 2",
            "testRating": 1600,
            "solvedInfo": {
                "1": true,
                "2": true,
                "3": true,
                "4": false,
                "5": false
            }
        }
    ]
}
profile
안녕하세요 aj4941 입니다.

1개의 댓글

comment-user-thumbnail
2023년 7월 18일

덕분에 좋은 정보 얻어갑니다, 감사합니다.

답글 달기