我使用的是与在Ajax Simplification Spring 3.0 article中指定的JSON的Spring MVC。春天mvc不返回json的内容 - 错误406
经过这么多的尝试和我的代码的变化取决于在各种论坛上发现的建议,我的代码仍然无法正常工作。
我一直在收到以下错误:(406)此请求标识的资源只能根据请求“accept”标头()生成不可接受的特征的响应。
我必须在我的appconfig.xml中。
APP-config.xml中
<context:component-scan base-package="org.ajaxjavadojo" />
<!-- Configures Spring MVC -->
<import resource="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>
这是我有我的控制器
@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的通话
function convertToDecimal(){
$.getJSON("convert/working", {key: "r"}, function(aConversion){
alert("it worked.");
$('#decimal').val(aConversion.input);
});
}
我真的很感谢在这个问题上的任何意见。 谢谢
太谢谢你了。根据文章,accept头应该被设置为*/*。所以我更新了请求映射到:@RequestMapping(value =“/ working”,headers =“Accept = */*”,method = RequestMethod.GET)现在它工作:)) – serena 2010-11-01 20:12:15