spring mvc没有返回json内容 - 错误406 [英] spring mvc not returning json content - error 406

查看:127
本文介绍了spring mvc没有返回json内容 - 错误406的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用带有JSON的Spring MVC,如 Ajax简化Spring 3.0文章

I am using Spring MVC with JSON as specified in Ajax Simplification Spring 3.0 article.

根据各种论坛上的建议,我的代码经过了如此多的尝试和变化,我的代码仍然没有工作。

After so many attempts and variations of my code depending on advice found on various forums, my code still doesn't work.

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

I keep on getting the following error: (406) The resource identified by this request is only capable of generating responses with characteristics not acceptable according to the request "accept" headers ().

我根据需要在appconfig.xml中。

I have in my appconfig.xml as required.

app-config。 xml

app-config.xml

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

    <!-- Configures Spring MVC -->
    <import resource="mvc-config.xml" />

mvc-config.xml

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>

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

This is what I have for my controller

@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;
  }
}

jsp jquery call

jsp jquery call

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

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

I would really appreciate any input on this issue. Thank you

推荐答案

尝试删除的接头限制,设置一个断点,看看实际值是多少。或者使用FireBug执行此操作。

Try remove the header limitation for Accept, put a breakpoint and see what's the actual value. Or do this with FireBug.

还可以查看这个jquery问题

这篇关于spring mvc没有返回json内容 - 错误406的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆