HTTP 406错误发现使用JSON升级到Spring MVC 4.1

时间:2014-10-17 04:30:33

标签: java json spring-mvc

升级到Spring MVC 4.1.1后,当我使用JSON时,会出现HTTP 406错误。

的pom.xml

<!-- JSON Support -->
<dependency>
    <groupId>org.codehaus.jackson</groupId>
    <artifactId>jackson-core-asl</artifactId>
    <version>1.9.13</version>
</dependency>
<dependency>
    <groupId>org.codehaus.jackson</groupId>
    <artifactId>jackson-mapper-asl</artifactId>
    <version>1.9.13</version>
</dependency>

Controller.java

    @RequestMapping(value = "/login.action", method = RequestMethod.POST)
public @ResponseBody HashMap<String, Boolean> loginAction(
        @RequestParam(value="username", required=true) String username,
        @RequestParam(value="password", required=true) String password,
        HttpServletRequest request,
        HttpSession session,
        Model model) {
    String ipAddress = request.getRemoteAddr();
    HashMap<String, Boolean> result = getLoginResult(username, password);
    logger.info(String.format("User: [Username=%s] tried to log in at %s", new Object[] {username, ipAddress}));
    if ( result.get("isSuccessful") ) {
        getSession(request, session, this.user);
    }
    return result;
}

的login.jsp

<script type="text/javascript">
    function doLoginAction(username, password) {
        var postData = 'username=' + username + '&password=' + password;
        $.ajax({
                type: 'POST',
                url: '<c:url value="/accounts/login.action" />',
                data: postData,
                dataType: 'JSON',
                success: function(result){
                    console.log(result);
                    return processLoginResult(result);
            }
        });
    }
</script>

日志消息将正常输出到控制台。 并且代码在Spring MVC 4.0.5中运行良好。

你知道怎么解决吗?非常感谢。

1 个答案:

答案 0 :(得分:2)

升级杰克逊的版本应解决这个问题。

<dependency>
  <groupId>com.fasterxml.jackson.core</groupId>
  <artifactId>jackson-databind</artifactId>
  <version>2.4.3</version>
</dependency>

请参阅Spring migration wiki on github