applan의 개발 이야기

[오류해결] Invalid media type "x-www-form-urlencoded; charset=UTF-8": does not contain '/' 본문

개발/Dev.

[오류해결] Invalid media type "x-www-form-urlencoded; charset=UTF-8": does not contain '/'

applan 2022. 9. 22. 11:03
728x90

💥 발생

서버 실행했더니 발생한 많은.. 붉은 색 오류들

다음과 같은 테스트 컨트롤러를 생성한 뒤 로컬에서 Tomcat 을 실행시켰더니 발생한 현상

( Content-Type에 따른 테스트를 진행하기 위해 두개의 컨트롤러 각각 생성 )

@RequestMapping(method= RequestMethod.POST, value="/test")
public @ResponseBody String test(HttpServletRequest request, HttpServletResponse response) {
    logger.info(response.getContentType());
    logger.info(request.getContentType());
    logger.info(request.getQueryString());
    logger.info(request.getRequestURL().toString());
    logger.info(request.getRequestURI());
    logger.info(request.getHeader("subscription"));
    logger.info(request.getHeader("subscriptionInfo"));
    logger.info(request.toString());
    return JsonObjectConverter.getJSONFromObject(null);
}

@RequestMapping(method= RequestMethod.POST, value="/testForm", produces = "x-www-form-urlencoded; charset=UTF-8")
public @ResponseBody String testForm(HttpServletRequest request, HttpServletResponse response) {
    logger.info(response.getContentType());
    logger.info(request.getContentType());
    logger.info(request.getQueryString());
    logger.info(request.getRequestURL().toString());
    logger.info(request.getRequestURI());
    logger.info(request.getHeader("subscription"));
    logger.info(request.getHeader("subscriptionInfo"));
    logger.info(request.toString());
    return JsonObjectConverter.getJSONFromObject(null);
}

🧑‍💻 문제 확인

에러는 항상 메시지가 왜 에러가 발생하고 있는지 말해주고 있다. 해당 오류의 메시지를 유심히 살펴보면 다음 메시지를 확인할 수 있었다. Invalid media type "x-www-form-urlencoded; charset=UTF-8": does not contain '/'

🤔 문제 해결 과정

Invalid media type "x-www-form-urlencoded; charset=UTF-8": does not contain '/' 

즉 '/'이 포함되어있지 않은 잘못된 미디어 타입 유형이라는 것이다. 

에러의 순서를 잘 보면 맨 위에 at org.springframework.http.MediaType.parseMediaType(MediaType.java:698) 쓰여있는 것을 확인할 수 있다. 

해당 메소드를 Spring document에서 검색해보니 타입을 변환해줄 수 없는 경우 발생하는 오류라고 기록되어있는 것을 확인할 수 있다.

😃 문제 해결

x-www-form-urlencoded 앞에 application/ 만 넣어주면 문제없이 동작하는 것을 확인할 수 있다.
-> application/x-www-form-urlencoded

🎓 깨닭은 점

사소한 실수가 애플리케이션 실행에까지 영향을 미친다. 실수를 최대한 방지하고 최대한 신속 정확하게 디버깅할 수 있도록 노력해야겠다.

 

728x90
Comments