๋ณธ๋ฌธ ๋ฐ”๋กœ๊ฐ€๊ธฐ
Trouble Shooting

[Spring] - HTTP 406 Not Acceptable

by ์ฃผ๋ฐœ2 2021. 3. 17.
๋ฐ˜์‘ํ˜•

 ์•ˆ๋…•ํ•˜์„ธ์š”~ ์ด์ „์— ์šด์˜ํ•˜๋˜ ๋ธ”๋กœ๊ทธ ๋ฐ GitHub, ๊ณต๋ถ€ ๋‚ด์šฉ์„ ์ •๋ฆฌํ•˜๋Š” Study-GitHub ๊ฐ€ ์žˆ์Šต๋‹ˆ๋‹ค!

 ๋„ค์ด๋ฒ„ ๋ธ”๋กœ๊ทธ

 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 ๋ฉ”์„œ๋“œ๋ฅผ ์ถ”๊ฐ€ํ•˜๋ฉด ์ •์ƒ์ ์œผ๋กœ ์‘๋‹ต์„ ๋ฑ‰์–ด์ค๋‹ˆ๋‹ค...

 

 

๐Ÿคข ์•„๊นŒ์šด ๋‚ด ์‹œ๊ฐ„ .............. ๐Ÿคข๐Ÿคข

๋ฐ˜์‘ํ˜•

๋Œ“๊ธ€