Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/spring/11.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
SpringMVC3.2应用程序中的JSON映射只适用于我的一个方法_Json_Spring_Maven_Spring Mvc_Jackson - Fatal编程技术网

SpringMVC3.2应用程序中的JSON映射只适用于我的一个方法

SpringMVC3.2应用程序中的JSON映射只适用于我的一个方法,json,spring,maven,spring-mvc,jackson,Json,Spring,Maven,Spring Mvc,Jackson,我正在开发Spring3.2MVC应用程序。这是非常典型的设置,但我的问题是,当我尝试从控制器方法返回JSON对象时,会出现406状态错误 令人沮丧的是,除了我的一个方法之外,我从服务器上得到了响应。我包含的代码是我在web上找到的一个经过修改的SpringMVCJSON示例。示例的带有path变量的控制器方法按预期工作,但我添加到控制器中的其他方法不起作用 我对示例代码所做的唯一其他更改发生在pom中。我将jackson依赖项升级到最新版本 那么我错过了什么?我的类路径上有jackson ma

我正在开发Spring3.2MVC应用程序。这是非常典型的设置,但我的问题是,当我尝试从控制器方法返回JSON对象时,会出现406状态错误

令人沮丧的是,除了我的一个方法之外,我从服务器上得到了响应。我包含的代码是我在web上找到的一个经过修改的SpringMVCJSON示例。示例的带有path变量的控制器方法按预期工作,但我添加到控制器中的其他方法不起作用

我对示例代码所做的唯一其他更改发生在pom中。我将jackson依赖项升级到最新版本

那么我错过了什么?我的类路径上有jackson mapper罐子。我在servlet中使用该指令,并且在我的每个控制器方法中使用@ResponseBody注释

任何帮助都将不胜感激

调度程序servlet:

    <?xml version="1.0" encoding="UTF-8"?>
    <beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xmlns:context="http://www.springframework.org/schema/context"
    xsi:schemaLocation="
    http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd
    http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
    http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <context:component-scan base-package="uk.co.jeeni" />

    <mvc:annotation-driven />

    </beans>

不工作的方法的映射URL以.html结尾。这没有意义,因为您正在返回JSON数据。我建议您更改映射,如下所示:

@RequestMapping(value="/testJson.json", method=RequestMethod.GET)

由于扩展,Spring将请求的媒体类型设置为html,因此不会转发到JSON视图。

未使用.html的方法的映射URL。这没有意义,因为您正在返回JSON数据。我建议您更改映射,如下所示:

@RequestMapping(value="/testJson.json", method=RequestMethod.GET)

由于扩展,Spring将请求的媒体类型设置为html,因此不会转发到JSON视图。

3.2中引入的mvc:annotations可能有问题,这里已经给出了答案:

mvc还有一个额外的变化:Spring3.2中需要注释。它试图根据请求的扩展自动分配媒体类型。我猜可能是/doSomething.html,它没有映射到JSON

更新mvc spring配置以关闭此自动映射

<mvc:annotation-driven content-negotiation-manager="contentNegotiationManager" />
<bean id="contentNegotiationManager"
    class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
    <!-- Turn off working out content type based on URL file extension, should 
        fall back to looking at the Accept headers -->
    <property name="favorPathExtension" value="false" />
</bean>

这可能是mvc的一个问题:3.2中引入的注释在这里已经得到了回答:

mvc还有一个额外的变化:Spring3.2中需要注释。它试图根据请求的扩展自动分配媒体类型。我猜可能是/doSomething.html,它没有映射到JSON

更新mvc spring配置以关闭此自动映射

<mvc:annotation-driven content-negotiation-manager="contentNegotiationManager" />
<bean id="contentNegotiationManager"
    class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
    <!-- Turn off working out content type based on URL file extension, should 
        fall back to looking at the Accept headers -->
    <property name="favorPathExtension" value="false" />
</bean>

哪一个不行?另外,您使用的客户端是什么?浏览器,ajax?@SotiriosDelimanolis我的客户端是firefox,尽管我也用ajax测试过它。使用path变量的方法有效。其他人没有。尝试使用带有URLConnection的java客户端或HttPClient向应用程序发出请求,但将Accept头设置为application/json。HTTP 406错误的发生是因为@SotiriosDelimanolis ok…我可以尝试一下,但它没有解释为什么我的一个方法能像我预期的那样工作,而其他方法却不能。哪一个不工作?另外,您使用的客户端是什么?浏览器,ajax?@SotiriosDelimanolis我的客户端是firefox,尽管我也用ajax测试过它。使用path变量的方法有效。其他人没有。尝试使用带有URLConnection的java客户端或HttPClient向应用程序发出请求,但将Accept头设置为application/json。HTTP 406错误的发生是因为@SotiriosDelimanolis ok…我可以尝试一下,但它没有解释为什么我的一个方法能像我预期的那样工作,而其他方法却不能。谢谢你回答我的问题。映射的扩展不应影响服务器的响应。我在其他项目中使用了.html扩展名。这些项目被配置为映射到扩展名为.html的任何项目。因此,我也不认为这是一个问题,但它足够简单,我可以尝试它,所以我可以完全排除它。我做了一个样本项目,我观察了答案中的行为。也许其他项目的配置有所不同?看看这里:我发现你没有使用ContentNegotingViewResolver。我删除了.html,删除了406错误。非常感谢。但我仍然不知道为什么会这样,因为我在其他spring项目中使用了.html和我的响应体映射。谢谢你回答我的问题。映射的扩展不应影响服务器的响应。我在其他项目中使用了.html扩展名。这些项目被配置为映射到扩展名为.html的任何项目。因此,我也不认为这是一个问题,但它足够简单,我可以尝试它,所以我可以完全排除它。我做了一个样本项目,我观察了答案中的行为。也许其他项目的配置有所不同?看看这里:我发现你没有使用ContentNegotingViewResolver。我删除了.html,删除了406错误。非常感谢。但我仍然不知道为什么这样做有效,因为我在响应体映射中使用了.html 其他spring项目的ping。
<mvc:annotation-driven content-negotiation-manager="contentNegotiationManager" />
<bean id="contentNegotiationManager"
    class="org.springframework.web.accept.ContentNegotiationManagerFactoryBean">
    <!-- Turn off working out content type based on URL file extension, should 
        fall back to looking at the Accept headers -->
    <property name="favorPathExtension" value="false" />
</bean>