[Spring] WARN : org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver - Resolved [org.springframework.http.converter.HttpMessageNotWritableException: No converter for [class java.lang.Integer] with preset Content-Type 'null']

2022. 12. 7. 18:19·🚀 from error to study/Java

 


💡 해결 방법

bean으로 Gson이 등록되어 있는지 확인해 볼 것

<dependency>
    <groupId>com.google.code.gson</groupId>
    <artifactId>gson</artifactId>
    <version>2.10</version>
</dependency>

 

 

 

checkbox 선택 후 버튼 클릭 시 삭제할 수 있도록 메소드를 만들었는데

DB의 REVIEW_STATUS = 'N'까지 바뀌는데도 ajax 통신 실패가 뜸

콘솔에 뜬 내용은 아래와 같음

WARN : org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver
- Resolved [org.springframework.http.converter.HttpMessageNotWritableException: No converter for [class java.lang.Integer] with preset Content-Type 'null']

 

 

 

 

자 보세요

 

 

코멘트 선택했죠?

 

 

 

삭제 버튼 눌러서 alert 띄웠죠? 확인 누르면

 

 

 

ajax 통신 실패죠?

 

 

 

 

컨트롤러에 리뷰 번호는 잘 찍혔는데 오류 떴죠?

 

 

 

근데 이제 DB에는 반영된......

 

 

 

https://thinkground.studio/httpmessagenotwritableexception-no-converter-for-vo-class-with-preset-content-type-null/

 

HttpMessageNotWritableException: No converter for [VO Class] with preset Content-Type 'null'] - ThinkGround

안녕하세요. No converter for [VO Class] with preset Content-Type 'null'] 오류와 관련된 해결 포스트입니다. Spring은 기본적으로 MessageConvertor가 탑재되어 있습니다.

thinkground.studio

비슷한 오류를 겪은 사람의 게시글을 봤더니 VO 객체에 어노테이션을 주고 해결했다고 함

 

 

 

그분 왈

Spring은 기본적으로 MessageConvertor가 탑재되어 있어

HTTP 통신을 할때 어떤 타입의 메세지가 ‘request’되고 ‘response’되던 Client에서 받아볼 수 있게 변환을 해줍니다.


대중적인 타입이 아닌 경우에는 커스터마이징을 통해 추가도 가능하며, 매우 편리하게 만들어 두었죠.

 

근데, 저렇게 Content-Type을 ‘null’로 찍어 버리면 여러 경우의 수

(예를 들자면.. if, else if, else if 도배를 한)에서도 else나 exception 처리를 하는 경우로 빼는 상황이기에

아무도 처리해 주지 않습니다.

 

 

 

후... 모라는 거지...

 

 

 

그때 문득 내가 json으로 응답 데이터를 보내겠다고 했음을 알게 됐고

(produces="application/json; charset=UTF-8" 이라고 당당하게 적어 놓음)

@ResponseBody
@RequestMapping(value="deleteComment.ad", produces="application/json; charset=UTF-8")
public int deleteAdminComment(HttpServletRequest request, @RequestParam(value="reviewNoArr[]") List<String> reviewNoArr) {

 

 

 

ajax는 controller에서 응답 정보를  문자열로밖에 못 받고,
그러므로 컨트롤러에서 객체를 문자열 형태로 만들어서 전송해야 하고,

그걸 만들어 주는 게 gson임을 알게 됨

 

 

 

사실 이걸 깨달은 것보다

Maven Dependencies를 확인해 보다가

json-simple만 있고 gson은 없어서 먼저 깔아보긴 한 거지만 어쨌든

 

 

 

💻 pom.xml에 gson을 등록해 주자

 

 

 

자 이제 다시 체크했죠?

 

 

 

삭제 눌렀죠? 확인할 거죠?

 

 

 

! 성공했죠?

 

 

 

! 리스트 다 뜨죠?

 

 

 

저작자표시 비영리 변경금지 (새창열림)
'🚀 from error to study/Java' 카테고리의 다른 글
  • [java] controller에서 jsp로 값을 보내지 못함
  • [Spring] Error - The method get/set필드명() is undefined for the type VO클래스
  • [Spring] 스프링에서 select, option 태그 값 넘기기(jsp 👈🏻👉🏻 controller 👉🏻 Service)
  • TO_CHAR(컬럼이름, 'FM0.0')을 이용해 별점 포맷 '★0.0' 소수점 1자리까지 나오게 하기
천재강쥐
천재강쥐
  • 천재강쥐
    디버거도 버거다
    천재강쥐
  • 전체
    오늘
    어제
    • Category (467)
      • 진짜 너무 궁금한데 이걸 나만 몰라...? (0)
      • 💾 Portfolio (2)
      • 🐤 CodingTest (28)
        • Java (20)
        • ᕕ(ꐦ°᷄д°᷅)ᕗ❌ (5)
      • 🚀 from error to study (142)
        • AI (1)
        • Cloud (2)
        • DB (12)
        • Front-End (16)
        • Github (14)
        • Java (39)
        • Mac (7)
        • Normal (29)
        • Server (22)
      • 📘 certificate (44)
        • 📘 리눅스마스터1급 (1)
        • 📘⭕️ 정보처리기사 (40)
        • 📘⭕️ SQLD (3)
      • 📗 self-study (234)
        • 📗 inflearn (35)
        • 📗 생활코딩 (8)
        • 📗 KH정보교육원 당산지원 (190)
      • 🎨 Scoop the others (0)
        • 📖 Peeking into other people.. (0)
        • 🇫🇷 (0)
        • 📘⭕️ 한국사능력검정시험 심화 (11)
        • 오블완 (4)
  • 인기 글

  • hELLO· Designed By정상우.v4.10.1
천재강쥐
[Spring] WARN : org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver - Resolved [org.springframework.http.converter.HttpMessageNotWritableException: No converter for [class java.lang.Integer] with preset Content-Type 'null']
상단으로

티스토리툴바