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

  • 相关阅读:
    全排列(next_permutation)
    Codeforces Round #321 (Div. 2)C(tree dfs)
    cf_ducational Codeforces Round 16_D(gcd)
    cf455a(简单dp)
    cf584a(水题)
    cf112a(水题)
    冒泡排序
    Python内置类型性能分析
    常见时间复杂度
    MongoDB 备份
  • 原文地址:https://www.cnblogs.com/twodog/p/12140756.html
Copyright © 2011-2022 走看看