RestController 报错No converter for class
时间:2021-06-11
Spring Boot项目@RestController,produces 配置错误会导致2种异常
一、Could not find acceptable representation
o.s.w.s.m.support.DefaultHandlerExceptionResolver : Resolved [org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation]
经过仔细检查,原来是produces = "text/html; charset=utf-8"配置错误,发出请求的类型和controller可接收的类型不匹配
二、另外一种情况No converter for [class 。。。
o.s.w.s.m.support.DefaultHandlerExceptionResolver : Resolved [org.springframework.http.converter.HttpMessageNotWritableException: No converter for [class com...] with preset Content-Type 'null']
这种情况是方法定义了produces = "text/html; charset=utf-8"属性,导致不能把返回对象转成json字符串,去掉produces 属性即可