springmvc @ResponseBody HttpMediaTypeNotAcceptableException

[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)也不行.

转载于:https://my.oschina.net/liuchangng/blog/889062

原文地址:https://www.cnblogs.com/twodog/p/12140754.html