Skip to content

Commit

Permalink
[Fix] #163 - 카페 생성 API 오류 수정
Browse files Browse the repository at this point in the history
Unsupported Media type 오류로 인해 dto에 관한 정보는 @RequestParam을 이용하여 받아서 사용
  • Loading branch information
parkrootseok committed Feb 7, 2023
1 parent 4223df6 commit 4487464
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 11 deletions.
16 changes: 12 additions & 4 deletions src/main/java/shop/cazait/domain/cafe/api/CafeController.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
package shop.cazait.domain.cafe.api;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import org.springframework.data.domain.Pageable;
import org.springframework.data.web.PageableDefault;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import shop.cazait.domain.cafe.dto.GetCafeRes;
Expand All @@ -30,14 +34,18 @@ public class CafeController {

private final CafeService cafeService;

@PostMapping(value = "/add/master/{masterId}", consumes = {"multipart/form-data"})
@PostMapping(value = "/add/master/{masterId}", consumes = {MediaType.APPLICATION_JSON_VALUE, MediaType.MULTIPART_FORM_DATA_VALUE})
@ApiOperation(value = "카페 등록", notes = "master가 카페를 등록한다.")
@ApiImplicitParam(name = "masterId", value = "마스터 ID")
public SuccessResponse<String> addCafe(@PathVariable Long masterId,
@RequestPart @Valid PostCafeReq postCafeReq,
@RequestPart(required = false) List<MultipartFile> imageFiles) throws JsonProcessingException {
cafeService.addCafe(masterId, postCafeReq, imageFiles);
@RequestParam String json,
@RequestPart(value = "cafeImages", required = false) List<MultipartFile> cafeImage) throws JsonProcessingException {

ObjectMapper objectMapper = new ObjectMapper().registerModule(new JavaTimeModule());
PostCafeReq postCafeReq = objectMapper.readValue(json, new TypeReference<>() {});
cafeService.addCafe(masterId, postCafeReq, cafeImage);
return new SuccessResponse<>("카페 등록 완료");

}

@GetMapping("/all/user/{userId}")
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/shop/cazait/domain/cafe/dto/GetCafeRes.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ public class GetCafeRes {
@Schema(description = "이름", example = "롬곡")
private String name;
@JsonProperty
@Schema(description = "위치", example = "서울시 중구")
@Schema(description = "위치", example = "서울특별시 광진구 군자동 광나루로17길 18")
private String address;
@JsonProperty
@Schema(description = "경도", example = "127.543215")
Expand Down
2 changes: 1 addition & 1 deletion src/main/java/shop/cazait/domain/cafe/dto/GetCafesRes.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ public class GetCafesRes {
@Schema(description = "이름", example = "롬곡")
private String name;
@JsonProperty
@Schema(description = "위치", example = "서울시 중구")
@Schema(description = "위치", example = "서울특별시 광진구 군자동 광나루로17길 18")
private String address;
@JsonProperty
@Schema(description = "경도", example = "127.543215")
Expand Down
4 changes: 2 additions & 2 deletions src/main/java/shop/cazait/domain/cafe/dto/PostCafeReq.java
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
import javax.validation.constraints.NotBlank;

@Schema(description = "카페 정보 등록 및 수정 Request : 카페 등록 및 수정 시 필요한 정보")
@Getter
@Data
@NoArgsConstructor(access = AccessLevel.PROTECTED)
public class PostCafeReq {

@Schema(description = "이름", example = "롬곡")
@NotBlank(message = "카페 이름을 입력해주세요.")
private String name;

@Schema(description = "주소", example = "서울시 중구")
@Schema(description = "주소", example = "서울특별시 광진구 군자동 광나루로17길 18")
@NotBlank(message = "카페 위치를 입력해주세요.")
private String address;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
package shop.cazait.global.error.exception;

import org.springframework.validation.BindingResult;
import org.springframework.validation.FieldError;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;
import shop.cazait.domain.cafe.exception.CafeException;
Expand All @@ -10,6 +13,7 @@
import shop.cazait.domain.review.exception.ReviewException;
import shop.cazait.domain.user.exception.UserException;
import shop.cazait.global.common.dto.response.FailResponse;
import shop.cazait.global.error.status.ErrorStatus;

@RestControllerAdvice
public class GlobalExceptionHandler {
Expand Down Expand Up @@ -54,10 +58,21 @@ protected FailResponse handlerReviewException(ReviewException exception) {
return new FailResponse(exception.getError());
}

@ExceptionHandler({ ValidException.class })
protected FailResponse handleValidException(ValidException exception) {
@ExceptionHandler({ MethodArgumentNotValidException.class })
protected FailResponse handleValidException(MethodArgumentNotValidException exception) {
BindingResult bindingResult = exception.getBindingResult();
StringBuilder description = new StringBuilder();

return new FailResponse(exception.getError(), exception.getDescription().toString());
for (FieldError fieldError : bindingResult.getFieldErrors()) {
description.append("[");
description.append(fieldError.getField());
description.append("](은)는");
description.append(fieldError.getDefaultMessage());
description.append(" 입력된 값: [");
description.append(fieldError.getRejectedValue());
description.append("]\n");
}
return new FailResponse(ErrorStatus.INVALID_REQUEST, description.toString());

}

Expand Down

0 comments on commit 4487464

Please sign in to comment.