#31 loan: common response
This commit is contained in:
@@ -2,7 +2,9 @@ package com.example.loan;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.data.jpa.repository.config.EnableJpaAuditing;
|
||||
|
||||
@EnableJpaAuditing
|
||||
@SpringBootApplication
|
||||
public class LoanApplication {
|
||||
|
||||
|
||||
@@ -0,0 +1,23 @@
|
||||
package com.example.loan.controller;
|
||||
|
||||
import com.example.loan.dto.ResponseDto;
|
||||
import com.example.loan.dto.ResultObject;
|
||||
|
||||
public abstract class AbstractController {
|
||||
|
||||
protected <T> ResponseDto<T> ok() {
|
||||
return ok(null, ResultObject.getSuccess());
|
||||
}
|
||||
|
||||
protected <T> ResponseDto<T> ok(T data) {
|
||||
return ok(data, ResultObject.getSuccess());
|
||||
}
|
||||
|
||||
protected <T> ResponseDto<T> ok(T data, ResultObject result) {
|
||||
ResponseDto<T> obj = new ResponseDto<>();
|
||||
obj.setResult(result);
|
||||
obj.setData(data);
|
||||
|
||||
return obj;
|
||||
}
|
||||
}
|
||||
44
loan/src/main/java/com/example/loan/dto/ResponseDto.java
Normal file
44
loan/src/main/java/com/example/loan/dto/ResponseDto.java
Normal file
@@ -0,0 +1,44 @@
|
||||
package com.example.loan.dto;
|
||||
|
||||
import com.example.loan.exception.BaseException;
|
||||
import com.fasterxml.jackson.annotation.JsonInclude;
|
||||
import lombok.*;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
@JsonInclude
|
||||
public class ResponseDto<T> implements Serializable {
|
||||
|
||||
private ResultObject result;
|
||||
|
||||
private T data;
|
||||
|
||||
public ResponseDto(ResultObject result) {
|
||||
this.result = result;
|
||||
}
|
||||
|
||||
public ResponseDto(T data) {
|
||||
this.data = data;
|
||||
}
|
||||
|
||||
public static <T> ResponseDto<T> ok() {
|
||||
return new ResponseDto<>(ResultObject.getSuccess());
|
||||
}
|
||||
|
||||
public static <T> ResponseDto<T> ok(T data) {
|
||||
return new ResponseDto<>(ResultObject.getSuccess(), data);
|
||||
}
|
||||
|
||||
public static <T> ResponseDto<T> response(T data) {
|
||||
return new ResponseDto<>(ResultObject.getSuccess(), data);
|
||||
}
|
||||
|
||||
public ResponseDto(BaseException ex) {
|
||||
this.result = new ResultObject(ex);
|
||||
}
|
||||
}
|
||||
42
loan/src/main/java/com/example/loan/dto/ResultObject.java
Normal file
42
loan/src/main/java/com/example/loan/dto/ResultObject.java
Normal file
@@ -0,0 +1,42 @@
|
||||
package com.example.loan.dto;
|
||||
|
||||
import java.io.Serializable;
|
||||
|
||||
import com.example.loan.exception.BaseException;
|
||||
import com.example.loan.exception.ResultType;
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Builder;
|
||||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.Setter;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@Builder
|
||||
@NoArgsConstructor
|
||||
@AllArgsConstructor
|
||||
public class ResultObject implements Serializable {
|
||||
|
||||
public String code;
|
||||
|
||||
public String desc;
|
||||
|
||||
public ResultObject(ResultType resultType) {
|
||||
this.code = resultType.getCode();
|
||||
this.desc = resultType.getDesc();
|
||||
}
|
||||
|
||||
public ResultObject(ResultType resultCode, String message) {
|
||||
this.code = resultCode.getCode();
|
||||
this.desc = message;
|
||||
}
|
||||
|
||||
public ResultObject(BaseException e) {
|
||||
this.code = e.getCode();
|
||||
this.desc = e.getDesc();
|
||||
}
|
||||
|
||||
public static ResultObject getSuccess() {
|
||||
return new ResultObject(ResultType.SUCCESS);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,24 @@
|
||||
package com.example.loan.exception;
|
||||
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
|
||||
import com.example.loan.dto.ResponseDto;
|
||||
import com.example.loan.dto.ResultObject;
|
||||
import lombok.extern.slf4j.Slf4j;
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
import org.springframework.web.bind.annotation.RestControllerAdvice;
|
||||
|
||||
@Slf4j
|
||||
@RestControllerAdvice
|
||||
public class APIExceptionHandler extends RuntimeException {
|
||||
|
||||
@ExceptionHandler(BaseException.class)
|
||||
protected ResponseDto<ResultObject> handleBaseException(
|
||||
BaseException e,
|
||||
HttpServletRequest request,
|
||||
HttpServletResponse response) {
|
||||
log.error(e.getMessage(), e);
|
||||
return new ResponseDto<>(e);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
package com.example.loan.exception;
|
||||
|
||||
import lombok.Getter;
|
||||
import lombok.NoArgsConstructor;
|
||||
import lombok.Setter;
|
||||
|
||||
@Getter
|
||||
@Setter
|
||||
@NoArgsConstructor
|
||||
public class BaseException extends RuntimeException {
|
||||
|
||||
private String code = "";
|
||||
private String desc = "";
|
||||
private String extraMessage = "";
|
||||
|
||||
public BaseException(ResultType resultType) {
|
||||
super(resultType.getDesc());
|
||||
this.code = resultType.getCode();
|
||||
this.desc = resultType.getDesc();
|
||||
}
|
||||
|
||||
public BaseException(ResultType resultType, String extraMessage) {
|
||||
super(resultType.getDesc() + " - " + extraMessage);
|
||||
this.code = resultType.getCode();
|
||||
this.desc = resultType.getDesc();
|
||||
this.extraMessage = extraMessage;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,16 @@
|
||||
package com.example.loan.exception;
|
||||
|
||||
import lombok.AllArgsConstructor;
|
||||
import lombok.Getter;
|
||||
|
||||
@Getter
|
||||
@AllArgsConstructor
|
||||
public enum ResultType {
|
||||
|
||||
SUCCESS("0000", "success"),
|
||||
|
||||
SYSTEM_ERROR("9000", "system error");
|
||||
|
||||
private final String code;
|
||||
private final String desc;
|
||||
}
|
||||
Reference in New Issue
Block a user