spring mvc没有返回json内容 - 错误406

ser*_*ena 26 java jquery spring spring-mvc jackson

我正在使用带有JSON的Spring MVC,如Ajax Simplification Spring 3.0文章中所指定的那样.

根据各种论坛上的建议,在我的代码进行了如此多的尝试和变化之后,我的代码仍然不起作用.

我继续收到以下错误:(406)此请求标识的资源只能根据请求"accept"headers()生成具有不可接受特征的响应.

我根据需要在appconfig.xml中.

APP-config.xml中

    <context:component-scan base-package="org.ajaxjavadojo" />

    <!-- Configures Spring MVC -->
    <import resource="mvc-config.xml" />
Run Code Online (Sandbox Code Playgroud)

MVC-config.xml中

<mvc:annotation-driven />

<!-- Forwards requests to the "/" resource to the "index" view -->
<mvc:view-controller path="/" view-name="index"/>


<!-- Resolves view names to protected .jsp resources within the /WEB-INF/views directory -->
<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>
<property name="viewResolvers">
<list>
  <bean class="org.springframework.web.servlet.view.BeanNameViewResolver"/>
  <bean class="org.springframework.web.servlet.view.InternalResourceViewResolver">
    <property name="prefix" value="/WEB-INF/views/"/>
    <property name="suffix" value=".jsp"/>
  </bean>
</list>
</property>

</bean>
Run Code Online (Sandbox Code Playgroud)

这就是我对我的控制器所拥有的

@Controller
@RequestMapping (value = "/convert")
public class ConversionController {

  @RequestMapping(method=RequestMethod.GET)
  public String getConversionForm(){
    return "convertView";
  }

  @RequestMapping(value = "/working", headers="Accept=application/json", method=RequestMethod.GET)
  public @ResponseBody Conversion getConversion(){
    Conversion d = new Conversion("d");
    return d;
  }
}
Run Code Online (Sandbox Code Playgroud)

jsp jquery调用

  function convertToDecimal(){
    $.getJSON("convert/working", {key: "r"}, function(aConversion){
      alert("it worked.");
      $('#decimal').val(aConversion.input);
    });
  }
Run Code Online (Sandbox Code Playgroud)

我真的很感激在这个问题上的任何意见.谢谢

axt*_*avt 23

要从@ResponseBody-annotated方法返回JSON响应,您需要两件事:

你不需要ContentNegotiatingViewResolverheaders@RequestMapping.


jpr*_*ism 18

我将Spring从4.1.x升级到4.1.x后出现了这个问题.我通过将Jackson从1.9.x升级到2.2.x(更快的xml)来修复

 <dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-core</artifactId>
    <version>2.2.3</version>
</dependency>
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.2.3</version>
</dependency>
<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-annotations</artifactId>
    <version>2.2.3</version>
</dependency>
Run Code Online (Sandbox Code Playgroud)


Boz*_*zho 8

尝试删除标题限制Accept,设置断点并查看实际值是什么.或者使用FireBug执行此操作.

另请看一下这个jquery问题

  • 非常感谢.根据文章,接受标题应设置为*/*.所以我更新了请求映射到:@RequestMapping(value ="/ working",headers ="Accept =*/*",method = RequestMethod.GET),现在它可以工作:)) (3认同)

Tho*_*mas 8

添加org.springframework.http.converter.json.MappingJacksonHttpMessageConverterorg.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter到DispatcherServlet-servlet.xml.并参考第二个使用的第一个

<bean id="jacksonMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"></bean>
<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
    <property name="messageConverters">
        <list>
            <ref bean="jacksonMessageConverter"/>
        </list>
    </property>
</bean>
Run Code Online (Sandbox Code Playgroud)