0

HttpMediaTypeNotAcceptableException在进行 jQuery ajax 调用时得到了。

我有以下配置。

在上下文中我有(这是在类路径中)

<context:component-scan base-package=" group package" />
<mvc:annotation-driven />

我在 servlet-config

<context:component-scan base-package="controller pacakges alone" />
<mvc:annotation-driven />

jackson-mapper-asl 1.9.13在 pom.xml 中添加并使用spring core 3.2.4security 3.1.4

我有一个 jQuery ajax 调用

$.ajax({
    url : "checkUser.html",
    cache : false,
    type : "post",
    data : "email=" + $('#email').val(),
    success : function(response) {
        success callback;
    }
});

我得到了这个例外:

org.springframework.web.HttpMediaTypeNotAcceptableException: Could not find acceptable representation
4

2 回答 2

2

这是一个更好的解释:http ://spring.io/blog/2013/05/11/content-negotiation-using-spring-mvc

您可能希望设置可接受的媒体类型,例如:

<bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
  <property name="mediaTypes">
  <map>
    <entry key="html" value="text/html"/>
    <entry key="json" value="application/json"/>
  </map>
 </property>

此外,您可以简单地发送参数,例如

var url = "/checkUser/" + $('#email').val() + ".htm";
    $.ajax({
        type: "POST",
        url: url,
        success: function(msg){

在控制器中

    @RequestMapping(value = "/checkUser/{email}",method = RequestMethod.POST)
于 2013-10-26T10:45:36.750 回答
0

唉,我明白了,spring 3.2 版本对配置内容协商的更改导致了这个问题,因为默认情况下通过文件扩展名完成内容协商,我的页面在升级到 3.2 版本后停止工作。

http://docs.spring.io/spring-framework/docs/3.2.x/spring-framework-reference/html/mvc.html#mvc-config-content-negotiation

我已禁用上述文件媒体类型扩展

<bean id="contentNegotiationManager"
    class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
    <property name="favorPathExtension" value="false" />
    <property name="favorParameter" value="true" />
    <property name="mediaTypes">
        <value>
            json=application/json
            xml=application/xml
        </value>
    </property>
</bean>

这篇文章了解更多信息。

升级到 Spring 3.2 后出现 HttpMediaTypeNotAcceptableException

于 2013-10-26T10:35:15.387 回答