您的位置:首页 > 理论基础 > 计算机网络

springmvc @ResponseBody HttpMediaTypeNotAcceptableException

2017-04-28 00:00 2695 查看
摘要: [ERROR]org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation

[ERROR]org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation

解决方法:

private static final ObjectMapper OBJECT_MAPPER = new ObjectMapper();
/**
* 清除数据
*/
@RequestMapping(value = "/reset")
@ResponseBody
public void reset(HttpServletResponse response) throws Exception{
response.setContentType("application/json; charset=UTF-8");
/*业务逻辑*/
//将对象转为json字符串输出到body中
response.getWriter().print(OBJECT_MAPPER.writeValueAsString(new Response().success("清除数据成功")));
}

注意:1,试过在xml中配置也不能解决.

<!-- 启动JSON格式的配置 -->
<bean  class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter">
<!-- 解决 HttpMediaTypeNotAcceptableException: Could not find acceptable representation -->
<property name="supportedMediaTypes">
<list>
<value>application/json;charset=UTF-8</value>
</list>
</property>
</bean>

2,将@RequestMapping(value = "/reset")改为@RequestMapping(value = "/reset",produces = MediaType.APPLICATION_JSON_VALU)也不行.
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
相关文章推荐