spring 版本:3.2.4
跟踪到spring mvc中查看到的异常:
org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation
spring中抛出异常的方法:
org.springframework.web.servlet.DispatcherServlet.doDispatch(HttpServletRequest request, HttpServletResponse response) throws Exception
错误的方法代码:
@RequestMapping(value="awardByPage",produces="text/plain;charset=UTF-8")
public @ResponseBody JSONResponse awardByPage(@RequestParam int pageIndex) {
return JSONResponse.buildSuccess(awardService.awardLogByPage(pageIndex));
}
解决方案:
1. 删除代码中的:
,produces="text/plain;charset=UTF-8"
2. 网络上查找的其它解决方案:
<!-- 启动JSON格式的配置 --> <bean id="jacksonMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"> <!--解决 HttpMediaTypeNotAcceptableException: Could not find acceptable representation --> <property name="supportedMediaTypes"> <list> <value>text/html;charset=UTF-8</value> </list> </property> </bean>
3. controller中返回的内容没有get方法,
解决办法:添加get方法;
4. 由于设置了@ResponseBody,要把对象转换成json格式,缺少转换依赖的jar包,故此错。
解决办法:加入依赖的jar,jackson-core-asl-1.9.12.jar,jackson-mapper-asl-1.9.12.jar问题解决。