본문 바로가기
TIL (Today I Learned)

TIL) HttpMediaTypeNotSupportedException: Content-Type 'text/plain;charset=UTF-8' is not supported]

by Won's log 2023. 6. 27.

서버에서 개인 블로그에 게시물을 올리는 Post Method가 작동하는지 확인하기 위해 Postman을 사용하여 데이터를 확인하였다.

오류가 발생했다.

HttpMediaTypeNotSupportedException: Content-Type 'text/plain;charset=UTF-8' is not supported]

Postman에서는 Content-Type을 자동으로 text/plain로 부여한다고 하여 application/json으로 수정을 해보았다.

수정 전
수정 후

그랬더니 이번엔 오류 500이 떴다. 뭐지!!!!

오류메시지에 의하면 Post entity 내에 content 임을 확인하였다.

 

[오류 메시지]

Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed: org.springframework.dao.DataIntegrityViolationException: not-null property references a null or transient value : com.example.post.entity.Post.content] with root cause

 

not-null property references a null or transient value의 경우, 다양한 원인이 존재하지만 나의 경우,

오타와 변경된 메서드로 build를 수행한 것이 문제였다.

 

특별히, RepsonseDto와 RequestDto에서 사용되는 이름을 혼동하여 postman에서 입력을 잘못한 결과가 컸다.

예를 들어, 

package com.example.post.dto;

import lombok.Getter;

@Getter
public class PostRequestDto {
    private String title;
    private String username;
    private String password;
    private String content;
}

RequestDto 코드에는 content를 적은 반면, Postman에서는 contents라고 작성하였고

Postman

결과로는 500 서버에러가 확인되었다.

 

content로 수정하고 포스트 리퀘스트를 해보니 정상적으로 작동하였다.

사실 이번 오류는 이 오류뿐만 아니라 앞서 말한 build를 삭제하고 다시 만들어야 하는 과정도 필요했다.

이유는 기존에 createdAt으로 데이터를 저장하다가 createAt으로 변경한 일이 있었는데 Database table에 createdAt와 createAt가 모두 존재하고 있어서 오류가 발생하였었다.

그래서 잘못 저장된 데이터를 완전삭제하기 위해

1. Database에 있는 table에서 불필요한 column을 Modify Table(Old UI)에서 삭제(-)하였고, Modify Table... 에서 실제 존재하는 칼럼을 확인할 수 있었다.

2. 완전 삭제를 하기 위해서는 좌측 Build 폴더를 삭제해야 했다. 코드를 재실행하면 변경된 디비를 바탕으로 빌드 폴더가 재생성되므로 삭제 시 큰 걱정을 할 필요는 없었다.

새앎:

1. ResponseDto와 RequestDto에서 사용된 content를 이해하고 있었다면 content 오류를 예상하지 않았을까 싶다.

2. 모든 개발자들이 보편적으로 갖는 오류이지만 이 오류 때문에 흘러 보낸 시간이 참 길다. 다음부터는 오류가 있을 때 시도해보지 않은 각도(build, table로 데이터 조회해 보기 등)도 생각하며 오류를 줄여나가야겠다.

**오류를 발견해서 너무 기뻤다.**