๋ฐ์ํ
๊ฐ๋ฐ์ ํ๋ค ๋ณด๋ฉด ์๋ง์ ์ค๋ฅ๊ฐ ๋ฐ์ํ๊ฒ ๋๊ณ ๋ฐ์ํ๋ ์ค๋ฅ์ ๋ํด ์ ์ ํ HTTP Status Code๋ก ํธ๋ค๋งํ๊ธฐ๋ ์ด๋ ค์์ด ์์ต๋๋ค.
๊ทธ๋ ๊ธฐ ๋๋ฌธ์ ๊ณตํต์ ์ธ ํฌ๋งท์ ๋ง๋ค๊ณ Response status์ message๋ก Request์ ๋ํ ์ํ์ ๋ฉ์์ง๋ฅผ ๋๋ ค์ฃผ๋ ์ฝ๋๋ฅผ ๋ง๋ค์ด๋ณด๊ฒ ์ต๋๋ค.
ResponseStatus
package com.hyuckang.springboot.utils; public enum ResponseStatus { SUCCESS, FAILURE, ERROR }
ResponseDto
package com.hyuckang.springboot.web.dto; import com.hyuckang.springboot.utils.ResponseStatus; import lombok.AllArgsConstructor; import lombok.Getter; @Getter @AllArgsConstructor public class ResponseDto<T> { private final ResponseStatus status; private final String message; private final T data; }
ResponseUtil
package com.hyuckang.springboot.utils; import com.hyuckang.springboot.web.dto.ResponseDto; public class ResponseUtil { public static <T>ResponseDto<T> SUCCESS (String message, T data) { return new ResponseDto(ResponseStatus.SUCCESS, message, data); } public static <T>ResponseDto<T> FAILURE (String message, T data) { return new ResponseDto(ResponseStatus.FAILURE, message, data); } public static <T>ResponseDto<T> ERROR (String message, T data) { return new ResponseDto(ResponseStatus.ERROR, message, data); } }
DemoController
package com.hyuckang.springboot.web.controller; import com.hyuckang.springboot.utils.ResponseUtil; import com.hyuckang.springboot.web.dto.ResponseDto; import org.springframework.web.bind.annotation.*; import java.util.ArrayList; import java.util.List; @RestController public class DemoController { List<String> UserList = new ArrayList<>(); int userLimit = 5; @GetMapping("/user") public ResponseDto getUsers() { return ResponseUtil.SUCCESS("์ ์ ์กฐํ์ ์ฑ๊ณตํ์์ต๋๋ค.", UserList); } @PostMapping("/user") public ResponseDto addUser(@RequestBody String userName) { if (UserList.size() < userLimit) { UserList.add(userName); return ResponseUtil.SUCCESS("์ ์ ๋ฅผ ์ถ๊ฐํ์์ต๋๋ค.", null); } else return ResponseUtil.ERROR("์ ์ ์ถ๊ฐ ์ค ๋ฌธ์ ๊ฐ ๋ฐ์ํ์์ต๋๋ค.", null); } }
์ฑ๊ณต ์๋ต
์๋ฌ ์๋ต
๋ฐ์ํ
'๐ JAVA > Spring' ์นดํ ๊ณ ๋ฆฌ์ ๋ค๋ฅธ ๊ธ
[SpringBoot] SNAPSHOT.jar์ SNAPSHOT-plain.jar (0) | 2022.03.17 |
---|---|
[SpringBoot] H2 Database ์ฐ๊ฒฐํ๊ธฐ (0) | 2022.03.13 |
[SpringBoot] application ์ค์ ํ์ผ ๋ถ๋ฆฌ (0) | 2022.03.09 |
[SpringBoot] ์คํ๋ง๋ถํธ ํ๋ก์ ํธ ์์ํ๊ธฐ (0) | 2021.09.13 |