跳转至

1.解决SpringMVC注解返回参数 (406 not acceptable)的错误

可能出现问题的地方

注:我的问题出现在没有jar包,以及jar包版本


  1. springMvc配置文件的开头,需要有:版本是3.0以上的

    xmlns:mvc="http://www.springframework.org/schema/mvc"
    http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd
    
  2. SpringMVC 配置文件中要有

    <!-- 注解驱动-->
    <mvc:annotation-driven />
    
    1. 要有Json解析的jar包,不同版本需要的不一样,tomcat注意清空服务器上的缓存。

      我的springMVC是4.1.3的

      org.springframework spring-webmvc 4.1.3.RELEASE

    我的JSON依赖包

    <!-- JSON -->
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-core</artifactId>
        <version>2.4.1</version>
    </dependency>
    <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
        <artifactId>jackson-databind</artifactId>
        <version>2.4.1.1</version>
    </dependency>
    
  3. 要在方法上面加上 @ResponseBody 注解

    @RequestMapping("/transmissionParam/receiveParamByGetOrPost")
    @ResponseBody
    public ReturnMessage receiveParamByGetOrPost(String userId) {
        ReturnMessage retMsg = new ReturnMessage();
        logger.info("接收成功!");
        retMsg.setSuccess(true);
        retMsg.setData(userId);
        return retMsg;
    }
    

参考链接:

  1. Spring MVC + JSON = 406 Not Acceptable

  2. 406 Spring MVC Json, not acceptable according to the request “accept” headers