zoukankan      html  css  js  c++  java
  • 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

  • 相关阅读:
    linux-nohup后台运行
    linux-友好显示文件大小
    System.exit(0)会跳过finally块的执行
    Spark-scala-API
    Lua协程-测试3
    Lua协程-测试2
    Lua协程
    费马大定理
    Spring事务超时、回滚的相关说明
    springboot测试service层的单元测试
  • 原文地址:https://www.cnblogs.com/twodog/p/12140756.html
Copyright © 2011-2022 走看看