Spring Boot2 (5) – Design api interface and data structure
This commit is contained in:
68
src/main/java/com/rest/api/service/ResponseService.java
Normal file
68
src/main/java/com/rest/api/service/ResponseService.java
Normal file
@@ -0,0 +1,68 @@
|
||||
package com.rest.api.service;
|
||||
|
||||
import com.rest.api.model.response.BasicResult;
|
||||
import com.rest.api.model.response.CommonResult;
|
||||
import com.rest.api.model.response.ListResult;
|
||||
import org.springframework.stereotype.Service;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@Service
|
||||
public class ResponseService {
|
||||
|
||||
// enum으로 api 요청 결과에 대한 code, message를 정의합니다.
|
||||
public enum CommonResponse {
|
||||
SUCCESS(0, "성공하였습니디."),
|
||||
FAIL(-1, "실패하였습니다.");
|
||||
|
||||
int code;
|
||||
String msg;
|
||||
|
||||
CommonResponse(int code, String msg) {
|
||||
this.code = code;
|
||||
this.msg = msg;
|
||||
}
|
||||
|
||||
public int getCode() {
|
||||
return code;
|
||||
}
|
||||
|
||||
public String getMsg() {
|
||||
return msg;
|
||||
}
|
||||
}
|
||||
// 단일건 결과를 처리하는 메소드
|
||||
public <T> BasicResult<T> getBasicResult(T data) {
|
||||
BasicResult<T> result = new BasicResult<>();
|
||||
result.setData(data);
|
||||
setSuccessResult(result);
|
||||
return result;
|
||||
}
|
||||
// 다중건 결과를 처리하는 메소드
|
||||
public <T> ListResult<T> getListResult(List<T> list) {
|
||||
ListResult<T> result = new ListResult<>();
|
||||
result.setList(list);
|
||||
setSuccessResult(result);
|
||||
return result;
|
||||
}
|
||||
// 성공 결과만 처리하는 메소드
|
||||
public CommonResult getSuccessResult() {
|
||||
CommonResult result = new CommonResult();
|
||||
setSuccessResult(result);
|
||||
return result;
|
||||
}
|
||||
// 실패 결과만 처리하는 메소드
|
||||
public CommonResult getFailResult() {
|
||||
CommonResult result = new CommonResult();
|
||||
result.setSuccess(false);
|
||||
result.setCode(CommonResponse.FAIL.getCode());
|
||||
result.setMsg(CommonResponse.FAIL.getMsg());
|
||||
return result;
|
||||
}
|
||||
// 결과 모델에 api 요청 성공 데이터를 세팅해주는 메소드
|
||||
private void setSuccessResult(CommonResult result) {
|
||||
result.setSuccess(true);
|
||||
result.setCode(CommonResponse.SUCCESS.getCode());
|
||||
result.setMsg(CommonResponse.SUCCESS.getMsg());
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user