• ์๋ ํ์ธ์~ ์ด์ ์ ์ด์ํ๋ ๋ธ๋ก๊ทธ ๋ฐ GitHub, ๊ณต๋ถ ๋ด์ฉ์ ์ ๋ฆฌํ๋ Study-GitHub ๊ฐ ์์ต๋๋ค!
• ๐
โ HTTP 406 Not Acceptable Error
Controller.java
package org.juhyun.project.jbookmark.controller.post;
import org.juhyun.project.jbookmark.controller.ApiResult;
import org.juhyun.project.jbookmark.controller.post.dto.PostResponseDto;
import org.juhyun.project.jbookmark.controller.post.dto.PostSaveRequestDto;
import org.juhyun.project.jbookmark.service.post.PostService;
import org.springframework.web.bind.annotation.*;
import static org.juhyun.project.jbookmark.controller.ApiResult.OK;
@RestController
@RequestMapping("api/board")
public class PostController {
private final PostService postService;
public PostController(PostService postService) {
this.postService = postService;
}
@PostMapping("/{boardId}/post")
public ApiResult<PostResponseDto> save(
@RequestBody PostSaveRequestDto request,
@PathVariable Long boardId) {
return OK(
new PostResponseDto(postService.save(request.newPost(boardId)))
);
}
}
ApiResult.java
package org.juhyun.project.jbookmark.controller;
import org.apache.commons.lang3.builder.ToStringBuilder;
import org.springframework.http.HttpStatus;
public class ApiResult<T> {
private final boolean success;
private final T response;
private final int statusCode;
private final String errorMessage;
public ApiResult(boolean success, T response, int statusCode) {
this(success, response, statusCode, null);
}
public ApiResult(boolean success, T response, int statusCode, String errorMessage) {
this.success = success;
this.response = response;
this.statusCode = statusCode;
this.errorMessage = errorMessage;
}
public static <T> ApiResult<T> OK(T response) {
return OK(response, 200);
}
public static <T> ApiResult<T> OK(T response, int statusCode) {
return new ApiResult<>(true, response, statusCode);
}
public static<T> ApiResult<T> ERROR(String errorMessage, HttpStatus status) {
return new ApiResult<>(false, null, status.value(), errorMessage);
}
@Override
public String toString() {
return new ToStringBuilder(this)
.append("success", success)
.append("response", response)
.append("statusCode", statusCode)
.append("message", errorMessage)
.toString();
}
}
์คํ๋ง ๋ถํธ๋ก ๊ฐ๋จํ REST API๋ฅผ ์์ฑํ ๋ค ํฌ์คํธ๋งจ์ ํตํด ํ ์คํธ๋ฅผ ํ๋๋ฐ ์๋์ ๊ฐ์ ์ค๋ฅ๊ฐ ๋ฐ์ํ์ต๋๋ค.
DefaultHandlerExceptionResolver : Resolved [org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation]
406 Not Acceptable
๋ณดํต ์ ์ค๋ฅ๋ก ๊ฒ์์ ํด๋ณด๋ฉด jackson ๋ผ์ด๋ธ๋ฌ๋ฆฌ๊ฐ ์ถ๊ฐ๋์ด์์ง ์๋ค๋ ๋ด์ฉ์ด ๋ง์๋ฐ,,
์ ๋ ์คํ๋ง๋ถํธ์ spring-boot-starter-web ๋ผ์ด๋ธ๋ฌ๋ฆฌ๋ฅผ ์ฌ์ฉํ๋ฏ๋ก,
ํด๋น ๋ผ์ด๋ธ๋ฌ๋ฆฌ์ jackson ๋ผ์ด๋ธ๋ฌ๋ฆฌ๊ฐ ๋ด๋ถ์ ์ผ๋ก ํฌํจ๋์ด ์์ต๋๋ค.
๋ฐ๋ผ์ ํด๋น ๋ฌธ์ ๋ ์๋์๋๋ฐ์, ๊ณ์ํด์ ์ดํด๋ณด๋๋ฐ....
์ปจํธ๋กค๋ฌ์์ ๋ฐํํ์ธ ApiResult์ Getter ๋ฉ์๋๊ฐ ์์๋๊ฒ !!!!!!!!!!!!!!
ํํ ์์ ๊ฐ์ด Getter ๋ฉ์๋๋ฅผ ์ถ๊ฐํ๋ฉด ์ ์์ ์ผ๋ก ์๋ต์ ๋ฑ์ด์ค๋๋ค...
๐คข ์๊น์ด ๋ด ์๊ฐ .............. ๐คข๐คข
๋๊ธ