Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weโ€™ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

โšก Feat : Define common api response #9

Merged
merged 5 commits into from
Jul 1, 2024
Merged

Conversation

loveysuby
Copy link
Contributor

๐Ÿš€ Related Issue

close: #6

๐Ÿ“Œ Tasks

  • ๊ณตํ†ต์ ์œผ๋กœ ์‚ฌ์šฉ๋˜๋Š” API ์‘๋‹ต ํ˜•์‹์„ ์ •์˜ํ–ˆ์Šต๋‹ˆ๋‹ค.

๐Ÿ“ Details

  • PageInfoResponse๋ฅผ ๋ณ„๋„ ๋ถ„๋ฆฌํ•ด์„œ ํŽ˜์ด์ง• ์‘๋‹ต ๊ด€๋ จ ์ •๋ณด๊ฐ€ ๊ณตํ†ต ์‘๋‹ต์˜ ํ•˜์œ„ ํ•„๋“œ๋กœ ์ถ”๊ฐ€๋ฉ๋‹ˆ๋‹ค.
@Getter
@JsonPropertyOrder({"code", "message", "data", "pageInfo"})
public class ApiResponse<T> {

	private final String code;

	private final String message;

	@JsonInclude(Include.NON_NULL)
	private final T data;

	private final PageInfoResponse pageInfo;

๐Ÿ“š Remarks

Points or opinions to share teams

  • Nullable response์ธ PageInfoResponse๊ฐ€ ์žˆ์–ดLombok ์œผ๋กœ ์ƒ์„ฑ์ž ์ง€์ •ํ•˜์ง€ ์•Š์•˜๋Š”๋ฐ ์ด์— ๋”ฐ๋ฅธ ๋ฌธ์ œ๊ฐ€ ์žˆ๋‹ค๋ฉด ์ˆ˜์ •ํ•ด์•ผ ํ•ฉ๋‹ˆ๋‹ค.

@loveysuby loveysuby added ๐Ÿงพ API API feature โœจ feat Improve performance or feature ๐Ÿฅต hart labels Jul 1, 2024
@loveysuby loveysuby linked an issue Jul 1, 2024 that may be closed by this pull request
@loveysuby loveysuby changed the base branch from main to develop July 1, 2024 10:37
Copy link
Contributor

@woosung1223 woosung1223 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@loveysuby

๊น”๋”ํ•˜๊ฒŒ ์ž˜ ๊ตฌํ˜„ํ•ด์ฃผ์…จ๋„ค์š” ์—ญ์‹œ ์ œ ์ˆ˜์ œ์ž์ž…๋‹ˆ๋‹ค. ๐Ÿ˜Ž

๋‹ค๋งŒ ๋ช‡ ๊ฐ€์ง€ ์ œ์•ˆ๋“œ๋ฆฌ๊ณ  ์‹ถ์€ ๋ถ€๋ถ„์ด ์žˆ์Šต๋‹ˆ๋‹ค!

  1. ์ปค๋ฐ‹, PR ์ปจ๋ฒค์…˜์— ๊ด€ํ•ด

์ €๋Š” ๊ฐœ์ธ์ ์œผ๋กœ ๋ณต์žกํ•œ ๊ธฐ๋Šฅ์„ ์‰ฝ๊ฒŒ ํ‘œํ˜„ํ•˜๊ธฐ ์œ„ํ•ด ํ•œ๊ตญ์–ด๋กœ ์ปค๋ฐ‹ ๋ฉ”์„ธ์ง€, PR ์ œ๋ชฉ ๋ฐ ๋ณธ๋ฌธ์„ ์ž‘์„ฑํ•˜๋Š” ํŽธ์ž…๋‹ˆ๋‹ค.
์˜คํ”ˆ์†Œ์Šคํ™”๋ฅผ ๊ณ ๋ คํ•œ๋‹ค๋ฉด ์˜์–ด๋„ ๊ดœ์ฐฎ๊ธด ํ•œ๋ฐ, ํ•œ๊ตญ์–ด๋กœ ํ•˜๋Š” ๊ฑด ์–ด๋– ์‹ ์ง€.. ์—ฌ์ญค๋ณด๊ณ  ์‹ถ์Šต๋‹ˆ๋‹ค. (์˜์–ด๋ฅผ ์ž˜ ๋ชปํ•ด์„œ;)

Gitmoji๋„ ํ˜น์‹œ ์ •ํ•ด๋†“์€ ์ปจ๋ฒค์…˜์ด ์žˆ์œผ์‹ค๊นŒ์š”? (์–ด๋–ค ์ƒํ™ฉ์— ์–ด๋–ค ์ด๋ชจํ‹ฐ์ฝ˜์„ ์“ด๋‹ค ๋“ฑ)

  1. ํ•ด๋‹น PR ๋‚ด์šฉ์— ๊ด€ํ•ด

์ž˜ ๊ตฌํ˜„ํ•ด์ฃผ์…จ๋Š”๋ฐ, ์ปจํŠธ๋กค๋Ÿฌ์—์„œ ์‚ฌ์šฉํ•  ๋•Œ์—๋Š” ํ˜•์‹์ด ์–ด๋–ป๊ฒŒ ๋˜๋‚˜์š”?
์ €๋„ ์ด๋Ÿฐ ์˜ˆ์™ธ์ฒ˜๋ฆฌ ์‘๋‹ต ํ˜•์‹์„ ๊ณ ๋ คํ•˜์ง€ ์•Š์€ ๊ฑด ์•„๋‹Œ๋ฐ ResponseEntity์™€ ํ•จ๊ป˜ ์‚ฌ์šฉํ•˜๋‹ค๋ณด๋‹ˆ
ResponseEntity<ApiResponse<UserResponse>> ์ฒ˜๋Ÿผ ๋ณด๊ธฐ ์ด์˜์ง€๊ฐ€ ์•Š๋”๋ผ๊ณ ์—ฌ..

๊ณ ๋ฏผํ•˜๋‹ค๊ฐ€ ์ž˜ ๋– ์˜ค๋ฅด์ง€ ์•Š์•„์„œ ๋„˜์–ด๊ฐ”๋Š”๋ฐ ํ˜น์‹œ ์ข‹์€ ์•„์ด๋””์–ด ์žˆ์œผ์‹œ๋ฉด ์ถ”์ฒœํ•ด์ฃผ์‹œ๋ฉด ๊ฐ์‚ฌํ•˜๊ฒ ์Šต๋‹ˆ๋‹ค..
์ด๊ฑด ์ˆ˜์ • ์š”์ฒญ์ด ์•„๋‹ˆ๋ผ ๋ฐ˜์˜ ์—ฌ๋ถ€์— ์ƒ๊ด€์—†์ด ๋จธ์ง€๋Š” ํ•  ์˜ˆ์ •์ž…๋‹ˆ๋‹ค!

this.message = message;
this.data = data;
this.pageInfo = pageInfo;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์ƒ์„ฑ์ž ์ฒด์ด๋‹์œผ๋กœ ๋‘๋ฉด ๊น”๋”ํ•  ๊ฒƒ ๊ฐ™์Šต๋‹ˆ๋‹ค!

return new ApiResponse<>(errorType.getCode(), message);
}

public static <T> ApiResponse<T> error(ErrorType errorType, String message, T data) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

์—ฌ๊ธฐ๋„์š”!

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

success, error ์ผ€์ด์Šค๊ฐ€ ๋„ˆ๋ฌด ๋‚œ์žกํ•˜๊ธฐ๋„ ํ•˜๊ณ , ์‘๋‹ต ์ฒ˜๋ฆฌ๋ฅผ ApiResponseAdvice์—์„œ ์ผ๊ด„ ์ฒ˜๋ฆฌํ•˜๋„๋ก ๋ฐ”๊ฟ”๋ดค์Šต๋‹ˆ๋‹ค. ๐Ÿค”

@woosung1223 woosung1223 merged commit 7d07867 into develop Jul 1, 2024
1 check passed
Copy link
Contributor Author

@loveysuby loveysuby left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@woosung1223

#10 ์ดํ›„์— ๋ฐ”๊พธ๊ธฐ ์–ด๋ ค์šด ๋ถ€๋ถ„์ด๋ผ ์กฐ๊ธˆ ๋” ์‹ ๊ฒฝ์จ์„œ ์ˆ˜์ •ํ•ด๋ดค์Šต๋‹ˆ๋‹ค. ๐Ÿ™€

ApiResponse์—์„œ๋Š” ์„ฑ๊ณต ๋ฐ ์˜ˆ์™ธ ์‘๋‹ต์„ ๋ชจ๋‘ ๋‹ด์„ ์ˆ˜ ์žˆ์–ด์•ผ ํ•˜๊ณ , ์ด๋Š” ์‘๋‹ต body๋ฅผ ๊ณต๋™์œผ๋กœ advice์—์„œ ์ฒ˜๋ฆฌํ•˜๋„๋ก ๋ฐ”๊ฟจ์Šต๋‹ˆ๋‹ค.

ApiResponseAdvice์—์„œ๋Š” end-point๋Š” ๋งž์ง€๋งŒ ์š”์ฒญ ํ˜•์‹์ด ์ž˜๋ชป ๋˜์—ˆ์„ ๋•Œ createResponseByHttpStatus์—์„œ ๋ถ„๊ธฐํ•˜๋„๋ก ์ž‘์„ฑํ–ˆ๋Š”๋ฐ, ์ด๊ฒŒ ํ•„์š”ํ• ์ง€๋Š” ์ž˜ ๋ชจ๋ฅด๊ฒ ์Šต๋‹ˆ๋‹ค ๐Ÿค”

์‘๋‹ต ๊ฐ์ฒด ์‚ฌ์šฉ ์˜ˆ์‹œ๋ฅผ ํ…Œ์ŠคํŠธ๋กœ ๊ฐ™์ด ๋„ฃ์–ด๋ดค๋Š”๋ฐ, ์ฝ”๋“œ๊ฐ€ ์‹คํšจ์„ฑ์ด ๋ถ€์กฑํ•ด ๋ณด์—ฌ์„œ ํ™•์ธ ๋ถ€ํƒ๋“œ๋ฆฝ๋‹ˆ๋‹ค.

return new ApiResponse<>(errorType.getCode(), message);
}

public static <T> ApiResponse<T> error(ErrorType errorType, String message, T data) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

success, error ์ผ€์ด์Šค๊ฐ€ ๋„ˆ๋ฌด ๋‚œ์žกํ•˜๊ธฐ๋„ ํ•˜๊ณ , ์‘๋‹ต ์ฒ˜๋ฆฌ๋ฅผ ApiResponseAdvice์—์„œ ์ผ๊ด„ ์ฒ˜๋ฆฌํ•˜๋„๋ก ๋ฐ”๊ฟ”๋ดค์Šต๋‹ˆ๋‹ค. ๐Ÿค”


private final int totalElements;

public static PageInfoResponse of(int currentPage, int totalPage, int totalElements) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

๋ชจ๋“  ์š”์ฒญ์ด paging์ด ๋“ค์–ด๊ฐ€์ง€๋Š” ์•Š์•„์„œ, PageInfoResponse๋ฅผ ๊ณตํ†ต ์‘๋‹ต์— ํ•„๋“œ๋กœ ๋‘๋Š” ๊ฒƒ๋ณด๋‹ค data์˜ ํ•˜์œ„๋กœ ๋‘๊ณ  ํ•„์š”ํ•  ๋•Œ๋งŒ PageInfoResponse ๋“ฑ์œผ๋กœ ๋‹ด์•„์„œ ์ฃผ๋Š” ๋ฐฉ์‹๋„ ๊ดœ์ฐฎ์€ ๊ฒƒ ๊ฐ™์•„ ์ˆ˜์ •ํ–ˆ์Šต๋‹ˆ๋‹ค.

์‘๋‹ต ํ˜•์‹์€ ์•„๋ž˜์™€ ๊ฐ™์ด ์ „๋‹ฌ๋ฉ๋‹ˆ๋‹ค. ์–ด๋– ์‹ค๊นŒ์š” ? ๐Ÿค”

{
    "timestamp": "2024.07.06 00:35:58",
    "code": "TYU-200",
    "message": "์‘๋‹ต ์„ฑ๊ณต",
    "data": {
        "id": 1,
        "name": "health",
        "pageInfo": {
            "currentPage": 1,
            "totalPage": 1,
            "totalElements": 1
        }
    }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
๐Ÿงพ API API feature โœจ feat Improve performance or feature ๐Ÿฅต hart
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Feat] Define common api response
2 participants