Spring MVC控制器抛出不支持JSON请求的错误媒体

时间:2014-01-31 13:45:05

标签: jquery json spring spring-mvc

当我尝试将JSON对象发送到服务器时,我收到此响应。我测试了所有stackoverflow的帖子,但没有任何作用:

客户方:

function submitWOZ(){
var sub = {
     "idNextexercise": parseInt($('#exList').val()), 
     "user": $('#user').val()   
    };
$.ajax({
    type: 'POST',
    headers: { 
        'Accept': 'application/json',
        'Content-Type': 'application/json' 
    },
    dataType : 'json',
    url: "insertNextID",
    //data: JSON.stringfy(sub),
    data: sub,
    success: function(data){
        alert('Great!');
    },
    error : function(jqXHR, status, error) {
       alert('Sorry!, there was a problem');
    },
    complete : function(jqXHR, status) {
       alert('Done!');
    }
});

}

服务器端:

@RequestMapping(value = "/insertNextID", method = RequestMethod.POST, consumes = {"application/xml", "application/json"})
public @ResponseBody String insertNextExercise(@RequestBody ExerciseVO messageForm, HttpServletRequest req){}

上下文配置:

<mvc:annotation-driven/>

<mvc:resources mapping="/resources/**" location="/, classpath:/META-INF/web-resources/" />
<mvc:default-servlet-handler />

<!-- Thymeleaf template resolver -->
<bean id="templateResolver"
      class="org.thymeleaf.templateresolver.ServletContextTemplateResolver">
    <property name="prefix" value="/WEB-INF/" />
    <property name="templateMode" value="HTML5" />
</bean>

<!-- Thymeleaf Template Engine -->
<bean id="templateEngine"
      class="org.thymeleaf.spring3.SpringTemplateEngine">
    <property name="templateResolver" ref="templateResolver" />
    <property name="additionalDialects">
        <set>
            <bean class="org.thymeleaf.extras.tiles2.dialect.TilesDialect"/>
            <bean class="org.thymeleaf.extras.springsecurity3.dialect.SpringSecurityDialect"/>
            <bean class="org.thymeleaf.extras.conditionalcomments.dialect.ConditionalCommentsDialect"/>
        </set>
    </property>
</bean>

<!-- Resolves logical view names returned by Controllers to Tiles; a view name to resolve is treated as the name of a tiles definition -->
<bean id="tilesViewResolver" class="org.thymeleaf.spring3.view.AjaxThymeleafViewResolver">
    <property name="viewClass" value="org.thymeleaf.extras.tiles2.spring.web.view.FlowAjaxThymeleafTilesView"/>
    <property name="templateEngine" ref="templateEngine"/>
</bean>

<!-- Configures the Tiles layout system using a specific thymeleaf-enabled Tiles Configurer -->
<bean id="tilesConfigurer" class="org.thymeleaf.extras.tiles2.spring.web.configurer.ThymeleafTilesConfigurer">
    <property name="definitions">
        <list>
            <value>/WEB-INF/**/views.xml</value>
        </list>
    </property>
</bean>

<bean id="jsonHttpMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
    <property name="prefixJson" value="false" />
    <property name="supportedMediaTypes" value="application/json" />
</bean>

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

    <property name="defaultViews">
        <list>
            <bean
                class="org.springframework.web.servlet.view.json.MappingJacksonJsonView">
                <property name="objectMapper">
                    <ref bean="JacksonObjectMapper" />
                </property>
            </bean>
        </list>
    </property>
    <property name="favorPathExtension" value="false" />
    <property name="favorParameter" value="true" />
    <property name="useNotAcceptableStatusCode" value="true" />
</bean>

<bean id="JacksonObjectMapper" class="org.codehaus.jackson.map.ObjectMapper" />

任何人都可以帮助我吗?因为我测试了我在网上找到的所有内容。

3 个答案:

答案 0 :(得分:0)

您可以通过向web.xml添加新过滤器来调试它,然后检查到达服务器的请求是否与您期望的一样。如果没问题,那么尝试覆盖jsonHttpMessageConverter将其添加到您的上下文配置文件中,并检查您的呼叫被拒绝的原因。可能有很多原因,我认为解决问题的唯一方法是在服务器端跟踪您的请求并找到卡住的位置。

答案 1 :(得分:0)

我认为问题可能是必需的JSON / XML解析器不在类路径上,因此spring MVC无法生成请求内容。

为了在JSON / XML中返回响应,必须在类路径中包含一组jar。

例如对于JSON,我们通常使用这些工件为版本2.1将Jackson解析器放在类路径中:

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

确切的工件取决于所使用的解析器,而杰克逊的情况也取决于版本。更古老的版本使用了不同的工件名称。

答案 2 :(得分:0)

  1. 尝试从控制器中删除(临时)整个消耗参数,如果可行的话......
  2. 恢复consumes参数并尝试将contentType: "application/json"添加到$ .AJAX调用而不是标题部分。