본문 바로가기
마주쳤던 에러 해결 모음

HttpMediaTypeNotSupportedException: Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported]

by 문자메일 2022. 6. 13.




에러 문구 : HttpMediaTypeNotSupportedException: Content type 'application/x-www-form-urlencoded;charset=UTF-8' not supported]

 

에러 재현 상황 : 

HTML Form에서 Request 날린 것을 @RequestBody DTOObject로 받으려고 할 때 Exception 발생함.

form 예시
예외 발생 controller 예시

 

문제 원인과 해결

 Form으로 Request 할 때는 Content-Type 요청 타입은 'application/x-www-form-urlencoded' 이다. (application/json X)

포맷은 key1=value1&key2=value2 포맷으로 전달 됨 (JSON 포맷이 아님)

그런데 Object Mapper는 데이터 포맷을 

Text(Json) -> Object

Object -> Text(Json) 

으로 변환해주는데 json 포맷으로 들어오지 않아서 변환할 수 없어서 415Exception 이 발생하였던 것임.

 

따라서 Content-Type 'application/x-www-form-urlencoded' 로 request 오는 것 처리하려면 아래 방법 중 선택해서 사용하면 됨.

@PostMapping(value = "/comment")
public void writeHandlerFormRequest( CommentDto commentDto) {
    log.info("commentDto = {}", commentDto.toString());
}

댓글