서머노트에 작성한 내용을 컨트롤러로 가져오는 과정에서 에러가 발생했다.
{"readyState":4,"responseText":"{\"timestamp\":\"2023-03-31T06:44:06.120+00:00\",\"status\":415,\"error\":\"Unsupported Media Type\",\"path\":\"/api/v1/save\"}","responseJSON":{"timestamp":"2023-03-31T06:44:06.120+00:00","status":415,"error":"Unsupported Media Type","path":"/api/v1/save"},"status":415,"statusText":"error"}
status":415 Unsupported Media Type으로 보니, 지원하지 않는 미디어 타입이라고 나온다.
그전에 공부했던 기록을 살펴보니 JSON을 사용할 때 컨트롤러에 @Controller 대신 @RestController을 사용하였다.
@RestController는 JSON을 반환하는 컨트롤러로 만들어주는 어노테이션이다. @ResponseBody를 각 메서드마다 선언했던 것을 한 번에 사용할 수 있게 해 준다고 생각하면 된다.
하지만 @RestController을 추가하여도 여전히 같은 에러가 발생했다.
그래서 스크립트를 살펴보았다.
$.ajax({
type: 'POST',
url: '/api/v1/save',
data: JSON.stringify(data)
}).done(function () {
alert('성공');
window.location.href = '/api/v1/search';
}).fail(function (error) {
alert(JSON.stringify(error));
});
코드를 살펴보니 dataType과 contentType을 지정하는 코드가 없었다.
$.ajax({
type: 'POST',
url: '/api/v1/save',
dataType: 'json',
contentType: 'application/json; charset=utf-8',
data: JSON.stringify(data)
}).done(function () {
alert('성공');
window.location.href = '/api/v1/search';
}).fail(function (error) {
alert(JSON.stringify(error));
});
그 둘을 추가하였더니 값을 컨트롤러로 가져오는 데 성공했다.