Skip to content

Commit

Permalink
Merge pull request #92 from Team-Going/feature/91
Browse files Browse the repository at this point in the history
[chore] 여행 TODO 관련 Swagger 설정
  • Loading branch information
SunwoongH authored Jan 15, 2024
2 parents 28b5b7e + 8fd53c9 commit b0f45e3
Show file tree
Hide file tree
Showing 10 changed files with 327 additions and 63 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.doorip.common.ApiResponse;
import org.doorip.common.BaseResponse;
import org.doorip.common.Constants;
import org.doorip.exception.UnauthorizedException;
import org.doorip.message.ErrorMessage;
Expand Down Expand Up @@ -48,6 +48,6 @@ private void setResponse(HttpServletResponse response, HttpStatus httpStatus, Er
response.setCharacterEncoding(Constants.CHARACTER_TYPE);
response.setStatus(httpStatus.value());
PrintWriter writer = response.getWriter();
writer.write(objectMapper.writeValueAsString(ApiResponse.of(errorMessage)));
writer.write(objectMapper.writeValueAsString(BaseResponse.of(errorMessage)));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import lombok.extern.slf4j.Slf4j;
import org.doorip.common.ApiResponse;
import org.doorip.common.BaseResponse;
import org.doorip.common.Constants;
import org.doorip.message.ErrorMessage;
import org.springframework.http.HttpStatus;
Expand Down Expand Up @@ -35,6 +35,6 @@ private void setResponse(HttpServletResponse response, HttpStatus httpStatus, Er
response.setCharacterEncoding(Constants.CHARACTER_TYPE);
response.setStatus(httpStatus.value());
PrintWriter writer = response.getWriter();
writer.write(objectMapper.writeValueAsString(ApiResponse.of(errorMessage)));
writer.write(objectMapper.writeValueAsString(BaseResponse.of(errorMessage)));
}
}
12 changes: 6 additions & 6 deletions doorip-api/src/main/java/org/doorip/common/ApiResponseUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,18 @@
import org.springframework.http.ResponseEntity;

public interface ApiResponseUtil {
static ResponseEntity<ApiResponse<?>> success(SuccessMessage successMessage) {
static ResponseEntity<BaseResponse<?>> success(SuccessMessage successMessage) {
return ResponseEntity.status(successMessage.getHttpStatus())
.body(ApiResponse.of(successMessage));
.body(BaseResponse.of(successMessage));
}

static <T> ResponseEntity<ApiResponse<?>> success(SuccessMessage successMessage, T data) {
static <T> ResponseEntity<BaseResponse<?>> success(SuccessMessage successMessage, T data) {
return ResponseEntity.status(successMessage.getHttpStatus())
.body(ApiResponse.of(successMessage, data));
.body(BaseResponse.of(successMessage, data));
}

static ResponseEntity<ApiResponse<?>> failure(ErrorMessage errorMessage) {
static ResponseEntity<BaseResponse<?>> failure(ErrorMessage errorMessage) {
return ResponseEntity.status(errorMessage.getHttpStatus())
.body(ApiResponse.of(errorMessage));
.body(BaseResponse.of(errorMessage));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,22 @@
@Builder(access = AccessLevel.PRIVATE)
@AllArgsConstructor(access = AccessLevel.PRIVATE)
@Getter
public class ApiResponse<T> {
public class BaseResponse<T> {
private final int status;
private final String code;
private final String message;
@JsonInclude(value = JsonInclude.Include.NON_NULL)
private final T data;

public static ApiResponse<?> of(SuccessMessage successMessage) {
public static BaseResponse<?> of(SuccessMessage successMessage) {
return builder()
.status(successMessage.getHttpStatus().value())
.code(successMessage.getCode())
.message(successMessage.getMessage())
.build();
}

public static <T> ApiResponse<?> of(SuccessMessage successMessage, T data) {
public static <T> BaseResponse<?> of(SuccessMessage successMessage, T data) {
return builder()
.status(successMessage.getHttpStatus().value())
.code(successMessage.getCode())
Expand All @@ -35,7 +35,7 @@ public static <T> ApiResponse<?> of(SuccessMessage successMessage, T data) {
.build();
}

public static ApiResponse<?> of(ErrorMessage errorMessage) {
public static BaseResponse<?> of(ErrorMessage errorMessage) {
return builder()
.status(errorMessage.getHttpStatus().value())
.code(errorMessage.getCode())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,37 +15,37 @@
@ControllerAdvice
public class GlobalExceptionHandler {
@ExceptionHandler(MethodArgumentNotValidException.class)
protected ResponseEntity<ApiResponse<?>> handleMethodArgumentNotValidException(final MethodArgumentNotValidException e) {
protected ResponseEntity<BaseResponse<?>> handleMethodArgumentNotValidException(final MethodArgumentNotValidException e) {
log.error(">>> handle: MethodArgumentNotValidException ", e);
return ApiResponseUtil.failure(ErrorMessage.BAD_REQUEST);
}

@ExceptionHandler(BindException.class)
protected ResponseEntity<ApiResponse<?>> handleBindException(final BindException e) {
protected ResponseEntity<BaseResponse<?>> handleBindException(final BindException e) {
log.error(">>> handle: BindException ", e);
return ApiResponseUtil.failure(ErrorMessage.BAD_REQUEST);
}

@ExceptionHandler(MethodArgumentTypeMismatchException.class)
protected ResponseEntity<ApiResponse<?>> handleMethodArgumentTypeMismatchException(final MethodArgumentTypeMismatchException e) {
protected ResponseEntity<BaseResponse<?>> handleMethodArgumentTypeMismatchException(final MethodArgumentTypeMismatchException e) {
log.error(">>> handle: MethodArgumentTypeMismatchException ", e);
return ApiResponseUtil.failure(ErrorMessage.BAD_REQUEST);
}

@ExceptionHandler(HttpRequestMethodNotSupportedException.class)
protected ResponseEntity<ApiResponse<?>> handleHttpRequestMethodNotSupportedException(final HttpRequestMethodNotSupportedException e) {
protected ResponseEntity<BaseResponse<?>> handleHttpRequestMethodNotSupportedException(final HttpRequestMethodNotSupportedException e) {
log.error(">>> handle: HttpRequestMethodNotSupportedException ", e);
return ApiResponseUtil.failure(ErrorMessage.METHOD_NOT_ALLOWED);
}

@ExceptionHandler(BusinessException.class)
protected ResponseEntity<ApiResponse<?>> handleBusinessException(final BusinessException e) {
protected ResponseEntity<BaseResponse<?>> handleBusinessException(final BusinessException e) {
log.error(">>> handle: BusinessException ", e);
return ApiResponseUtil.failure(e.getErrorMessage());
}

@ExceptionHandler(Exception.class)
protected ResponseEntity<ApiResponse<?>> handleException(final Exception e) {
protected ResponseEntity<BaseResponse<?>> handleException(final Exception e) {
log.error(">>> handle: Exception ", e);
return ApiResponseUtil.failure(ErrorMessage.INTERNAL_SERVER_ERROR);
}
Expand Down
13 changes: 11 additions & 2 deletions doorip-api/src/main/java/org/doorip/config/SwaggerConfig.java
Original file line number Diff line number Diff line change
@@ -1,11 +1,21 @@
package org.doorip.config;

import io.swagger.v3.oas.models.Components;

import io.swagger.v3.oas.annotations.enums.SecuritySchemeIn;
import io.swagger.v3.oas.annotations.enums.SecuritySchemeType;
import io.swagger.v3.oas.annotations.security.SecurityScheme;
import io.swagger.v3.oas.models.OpenAPI;
import io.swagger.v3.oas.models.info.Info;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

@SecurityScheme(
name = "Authorization",
type = SecuritySchemeType.HTTP,
in = SecuritySchemeIn.HEADER,
bearerFormat = "JWT",
scheme = "Bearer"
)
@Configuration
public class SwaggerConfig {
@Bean
Expand All @@ -15,7 +25,6 @@ public OpenAPI api() {
.version("v1.0")
.description("Doorip 서비스 API 명세서 입니다.");
return new OpenAPI()
.components(new Components())
.info(info);
}
}
Loading

0 comments on commit b0f45e3

Please sign in to comment.