[Web_3] Spring 4 ๐Ÿฆ€

08627ยท2022๋…„ 9์›” 20์ผ
1

Spring

๋ชฉ๋ก ๋ณด๊ธฐ
4/13
post-thumbnail


๐Ÿ“Œ ์‘๋‹ต(Response) ๋ฐ์ดํ„ฐ ๋ณด๋‚ด๊ธฐ


๐Ÿ’ก ์‘๋‹ต(Response) ๋ฐ์ดํ„ฐ ๋ณด๋‚ด๊ธฐ

@ResponseBody ์–ด๋…ธํ…Œ์ด์…˜์ด ๋ถ™์€ ๋ฉ”์†Œ๋“œ์—์„œ Map์„ ๋ฐ˜ํ™˜ํ•˜๋ฉด ์ž๋™์œผ๋กœ JSON ํ˜•ํƒœ๋กœ ๋ณ€ํ™˜๋œ๋‹ค. (@RestController ํฌํ•จ)

๋ฐ˜๋Œ€๋กœ JSON ์ •๋ณด๊ฐ€ @RequestBody ์–ด๋…ธํ…Œ์ด์…˜์ด ๋ถ™์€ ์ปจํŠธ๋กค๋Ÿฌ๋กœ ๋ณด๋‚ด์ง€๋ฉด, Map ์œผ๋กœ ์„ ์–ธ๋œ ๋ณ€์ˆ˜์— Json ์ž๋ฃŒํ˜•์„ ๋ฐ”๋กœ ๋ฐ›์„ ์ˆ˜ ์žˆ๋‹ค.

๐Ÿ‘พ

โ–ซ๏ธ CMRespDto.java

@AllArgsConstructor
@Getter
public class CMRespDto<T> {
    private int code; // 1 : ์„ฑ๊ณต, -1 : ์‹คํŒจ
    private String msg; // commit ๋ฉ”์„ธ์ง€
    private T data; // ์‘๋‹ต์œผ๋กœ ๋ณด๋‚ผ ๋ฐ์ดํ„ฐ
}

โ–ซ๏ธ StudentRespDto.java

@Builder
@Getter
public class StudentRespDto {
    private int studentCode;
    private String studentName;
    private int studentYear;
    private String studentAddress;
    private String studentPhone;
}

โ–ซ๏ธ CMResponseController.java
์ปค๋ฐ‹ ๋ฉ”์‹œ์ง€์™€ ํ•จ๊ป˜ ์‘๋‹ต ๋ฐ์ดํ„ฐ๋ฅผ ๋ณด๋‚ด์ค€๋‹ค.

@RestController
public class CMResponseController {

	@GetMapping("/api/v1/students")
    public Object getStudentList() {

        List<StudentRespDto> students = new ArrayList<StudentRespDto>();
        Map<String, Object> studentMap = new HashMap<String, Object>();

        StudentRespDto student1 = StudentRespDto.builder()
                .studentCode(20220001)
                .studentName("์‹ ์งฑ๊ตฌ")
                .studentYear(1)
                .studentAddress("์นด์Šค์นด๋ฒ ์‹œ")
                .studentPhone("010-1111-1111")
                .build();

        students.add(student1);

        StudentRespDto student2 = StudentRespDto.builder()
                .studentCode(20220002)
                .studentName("๊น€์ฒ ์ˆ˜")
                .studentYear(1)
                .studentAddress("์นด์Šค์นด๋ฒ ์‹œ")
                .studentPhone("010-2222-2222")
                .build();

        students.add(student2);

        // return students; โ†’ ๋ฆฌ์ŠคํŠธ ๋ฐ˜ํ™˜ ์‹œ Json ํ˜•ํƒœ๋กœ ๋ณ€ํ™˜ ๋ถˆ๊ฐ€๋Šฅ
		// โญ ํ‚ค ๊ฐ’์„ ์ฃผ๊ธฐ ์œ„ํ•ด StudentList๋ฅผ Map ์— ๋„ฃ์–ด์ค€๋‹ค.  
        studentMap.put("students", students); 

        return studentMap; // ํด๋ผ์ด์–ธํŠธ์—์„œ Json ํ˜•ํƒœ๋กœ ์‘๋‹ต๋œ๋‹ค. 
    }
	

    @GetMapping("/api/v1/cm/data1")
    public CMRespDto<?> getData() {
        return new CMRespDto<String>(1, "๋ฐ์ดํ„ฐ ์‘๋‹ต ์„ฑ๊ณต", "ํ…Œ์ŠคํŠธ ๋ฐ์ดํ„ฐ");
    }

    @GetMapping("/api/v1/cm/data2")
    public CMRespDto<?> getData2() {
        return new CMRespDto<Boolean>(-1, "๋ฐ์ดํ„ฐ ์‘๋‹ต ์‹คํŒจ", false);
    }

    @GetMapping("/api/v1/cm/data3")
    public CMRespDto<?> getData3() {
        List<StudentRespDto> dtoList = new ArrayList<StudentRespDto>();
        dtoList.add(StudentRespDto.builder().studentCode(1).build());
        dtoList.add(StudentRespDto.builder().studentCode(2).build());
        dtoList.add(StudentRespDto.builder().studentCode(3).build());
        
        return new CMRespDto<>(1, "ํ•™์ƒ ์ •๋ณด ๋ฆฌ์ŠคํŠธ ๋ฐ์ดํ„ฐ ์ž…๋‹ˆ๋‹ค.", dtoList);
        // StudentList ๋ฅผ ํ‚ค ๊ฐ’์„ ์ฃผ๊ธฐ ์œ„ํ•ด Map ์— ๋„ฃ์—ˆ์—ˆ์Œ.
        // ์ด ๊ฒฝ์šฐ CMRespDto๋กœ ๋ณด๋‚ด ํ‚ค(data)๊ฐ€ ์žˆ์Œ. 
    }
}




๐Ÿ“ข ์†Œ๊ฐ ๐Ÿฆ€

0๊ฐœ์˜ ๋Œ“๊ธ€