Skip to content

FeelingXD/common-api-response

Folders and files

NameName
Last commit message
Last commit date

Latest commit

ย 

History

7 Commits
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 
ย 

Repository files navigation

common-api-response

Purpose โœจ

์ด ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋Š” ResponseEntity ์‘๋‹ต์‹œ ์ผ๊ด€๋œ ํฌ๋งท์„ ์ œ๊ณตํ•˜๊ธฐ์œ„ํ•ด ์ œ์ž‘๋œ ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ์ž…๋‹ˆ๋‹ค. ๐Ÿข

How to use? ๐Ÿค”

ํ•ด๋‹น ๋ผ์ด๋ธŒ๋Ÿฌ๋ฆฌ๋ฅผ ์ž„ํฌํŠธ ํ•˜๊ณ  ์•„๋ž˜์™€ ๊ฐ™์ด ์„ค์ •ํ•ด์ฃผ์‹  ํ›„ ์‚ฌ์šฉํ•  ์ˆ˜ ์žˆ์Šต๋‹ˆ๋‹ค.

Setting

//setting.gradle 
//๋‹ค์Œ ๊ณผ ๊ฐ™์ด public repo์—์„œ ์†Œ์Šค์ปจํŠธ๋กค๋Ÿฌ๋ฅผ ์ถ”๊ฐ€ํ•ด์ค๋‹ˆ๋‹ค.
sourceControl {
    gitRepository("https://github.com/FeelingXD/common-api-response"){
        producesModule("org.feelingxd:common-api-response")
    }
}
//build.gradle 
//setting.gradle ์ถ”๊ฐ€ ์ดํ›„ build.gradle์—์„œ ์ฝ”๋“œ๋ฅผ ์ž„ํฌํŠธํ•ฉ๋‹ˆ๋‹ค.

dependencies {
    implementation 'org.feelingxd:common-api-response:{version}'
}

Response Shape

ApiResponse์˜ ํ˜•ํƒœ๋Š” ๋‹ค์Œ๊ณผ ๊ฐ™์Šต๋‹ˆ๋‹ค.

    public class ApiResponse<T> {
        private String code;
        private HttpStatus status;
        private String msg;
        private T data; // data๋Š” ์˜ต์…˜์ด๋ฉฐ ์ „ํ•ด์ค„ ๋ฐ์ดํ„ฐ ๊ฐ€ ์žˆ์„๊ฒฝ์šฐ ์‚ฌ์šฉํ•ฉ๋‹ˆ๋‹ค.
    }

json Response

{
    code: "{CODE_NAME}",
    status: "{HTTP_STATUS}",
    msg: "{MSG}",
    data: "{DATA}"
}

Example

//Code inteface๋ฅผ ๊ตฌํ˜„ํ•ด์„œ Custom Code๋ฅผ ๊ตฌํ˜„ํ• ์ˆ˜์žˆ์Šต๋‹ˆ๋‹ค.
 public enum ExampleErrorCode implements Code {
  EXAMPLE_ERROR_CODE(HttpStatus.BAD_REQUEST, "ํ…Œ์ŠคํŠธ ์—๋Ÿฌ์ฝ”๋“œ์ž…๋‹ˆ๋‹ค. xD");

  private final HttpStatus status;
  private final String msg;

  public HttpStatus getStatus() {
    return this.status;
  }

  public String getMsg() {
    return this.msg;
  }

  private ExampleErrorCode(HttpStatus status, String msg) {
    this.status = status;
    this.msg = msg;
  }
}
//Api response๋Š” builder ํŒจํ„ด์ด ์ ์šฉ๋˜์–ด์žˆ์œผ๋ฉฐ
// ResponseEntityํƒ€์ž…์œผ๋กœ ๋ณ€ํ™˜๊ฐ€๋Šฅํ•˜๋„๋ก ์„ค๊ณ„๋˜์–ด์žˆ์Šต๋‹ˆ๋‹ค.
// ๋‹ค์Œ ์˜ˆ์ œ๋Š” ๊ฐ„๋‹จํ•˜๊ฒŒ ApiResponse->ResponseEntity๋กœ ๋‹ค์Œ๊ณผ๊ฐ™์ด ๋นŒ๋“œํ• ์ˆ˜์žˆ์Šต๋‹ˆ๋‹ค.
public class Example {

  public static void main(String[] args){
    var responseEntity =ApiResponse.builder()
    .code(YourCode.EXAMPLE_ERROR_CODE)
    .data(YourCode.code) //optional
    .toEntity(); // ResponseEntity class
    
    var apiResponse =ApiResponse.builder()
    .code(YourCode.EXAMPLE_ERROR_CODE)
    .data(YourCode.code) //optional
    .build() // ApiResponse class
  }
}
//code Example
@RestController
@RequestMapping("/")
public class testController {

  @GetMapping("api")
  public ResponseEntity<ApiResponse<String>> commonApiResponse(){
    return ApiResponse.builder()
    .code(ExampleErrorCode.EXAMPLE_ERROR_CODE)
    .toEntity();
  }
}

Expect json

{
    code: "EXAMPLE_ERROR_CODE",
    status: "400",
    msg: "ํ…Œ์ŠคํŠธ ์—๋Ÿฌ์ฝ”๋“œ์ž…๋‹ˆ๋‹ค. xD",
    data: null 
}

Dependency

  • Java > 8
  • Spring-Web 2.7
  • Lombok

About

๐Ÿ‘€ CommonApiResponse

Resources

Stars

Watchers

Forks

Packages

No packages published

Languages