gpt4 book ai didi

java - spring mvc 不返回 json 内容 - 错误 406

转载 作者:IT老高 更新时间:2023-10-28 13:46:40 26 4
gpt4 key购买 nike

我正在使用 Spring MVC 和 Ajax Simplification Spring 3.0 article 中指定的 JSON .

根据各种论坛上的建议对我的代码进行了多次尝试和修改后,我的代码仍然无法正常工作。

我不断收到以下错误:(406)此请求标识的资源只能生成具有根据请求“接受” header () Not Acceptable 特征的响应。

我的 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 所拥有的

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

非常感谢您对这个问题的任何意见。谢谢

最佳答案

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

你不需要 ContentNegotiatingViewResolverheaders@RequestMapping .

关于java - spring mvc 不返回 json 内容 - 错误 406,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4069903/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com