我正在使用 ExtJS 4 前端和 Spring 3 作为后端创建文件上传。文件上传有效,但来自服务器的响应内容类型错误。当我发送 {success:true}
使用 Map<String, Object>
由 Jackson 序列化,ExtJS 返回错误
Uncaught Ext.Error: You're trying to decode an invalid JSON String: <pre style="word-wrap: break-word; white-space: pre-wrap;">{"success":true}</pre>
为什么我的回复是
<pre>
标签?我已经搜索和 found out我应该将响应类型更改为 text/html
例如。但是changing content type in servlet response没有帮助@RequestMapping(value = "/upload", method = RequestMethod.POST)
public @ResponseBody Map<String, Object> upload(
FileUpload uploadItem, BindingResult result, HttpServletResponse response) {
response.setContentType("text/html");
// File processing
Map<String, Object> jsonResult = new HashMap<String, Object>();
jsonResult.put("success", Boolean.TRUE);
return jsonResult;
}
当我更改
upload
的返回值时方法 String
,一切正常,但我想返回 Map
并由 jackson 连载@RequestMapping(value = "/upload", method = RequestMethod.POST)
public @ResponseBody String upload(
FileUpload uploadItem, BindingResult result, HttpServletResponse response) {
// File processing
return "{success:true}";
}
我的 Spring 配置
<bean
id="stringHttpMessageConverter"
class="org.springframework.http.converter.StringHttpMessageConverter">
</bean>
<bean
id="jacksonMessageConverter"
class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"/>
<bean
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="jacksonMessageConverter"/>
<ref bean="stringHttpMessageConverter" />
</list>
</property>
</bean>
如何告诉 Spring 返回正确的内容类型? 当其他方法的响应被正确解释时,为什么该方法的响应不正确?
最佳答案
您需要将响应的内容类型设置为“text/html”。
如果内容类型是“application/json”就会有这个问题。这很奇怪。
关于json - ExtJS 4 Spring 3 文件上传。服务器发送错误响应内容类型,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7023531/