Json extjs4spring3文件上传。服务器发送错误的响应内容类型
我使用ExtJS4前端和Spring3作为后端创建文件上传。文件上载工作正常,但来自服务器的响应具有错误的内容类型。当我使用Jackson序列化的Json extjs4spring3文件上传。服务器发送错误的响应内容类型,json,spring,extjs,upload,response,Json,Spring,Extjs,Upload,Response,我使用ExtJS4前端和Spring3作为后端创建文件上传。文件上载工作正常,但来自服务器的响应具有错误的内容类型。当我使用Jackson序列化的Map发送{success:true}时,ExtJS返回错误 Uncaught Ext.Error: You're trying to decode an invalid JSON String: <pre style="word-wrap: break-word; white-space: pre-wrap;">{"success":tr
Map
发送{success:true}
时,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>
当我将upload
方法的返回值更改为String
时,一切正常,但我希望返回Map
,并由Jackson对其进行序列化
<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配置
@RequestMapping(value = "/upload", method = RequestMethod.POST)
public @ResponseBody boolean upload(
FileUpload uploadItem, BindingResult result, HttpServletResponse response) {
return true; //or false
}
如何告诉Spring返回正确的内容类型?当其他方法的响应被正确解释时,为什么此方法的响应不正确?您需要将响应的内容类型设置为“text/html”。
如果内容类型为“application/json”,则会出现此问题。这很奇怪。如果只需要返回成功值,则可以返回布尔值:
public class ExtJSJsonResponse {
/** Parameters to serialize to JSON */
private Map<String, Object> params = new HashMap<String, Object>();
/**
* Add arbitrary parameter for JSON serialization.
* Parameter will be serialized as {"key":"value"};
* @param key name of parameter
* @param value content of parameter
*/
@JsonIgnore
public void addParam(String key, Object value) {
params.put(key, value);
}
/**
* Gets all parameters. Also is annotated with <code>@JsonValue</code>.
* @return all params with keys as map
*/
@JsonValue
public Map<String, Object> getParams() {
return params;
}
/**
* Returns specified parameter by <code>key</code> as string "key":"value"
* @param key parameter key
* @return "key":"value" string or empty string when there is no parameter
* with specified key
*/
private String paramToString(String key) {
return params.containsKey(key)
? "\"" + key + "\":\"" + params.get(key) + "\""
: "";
}
/**
* Manually transforms map parameters to JSON string. Used when ExtJS fails
* to decode Jackson response. i.e. when uploading file.
* @return
*/
@Override
@JsonIgnore
public String toString() {
StringBuilder sb = new StringBuilder("{");
String delimiter = "";
for (String key : params.keySet()) {
sb.append(delimiter);
sb.append(paramToString(key));
delimiter = ",";
}
sb.append("}");
return sb.toString();
}
}
虽然这不是最好的解决方案,但它解决了问题。我已经创建了一个类,这个类的内部有
Map
,以及向Map
添加参数的方法。还有一个实现方法toString()
因此,当出现uncaughtext.Error:您试图解码无效的JSON字符串时,您只需执行此操作
@RequestMapping(value = "/upload", method = RequestMethod.POST, produces = MediaType.TEXT_HTML_VALUE)
public @ResponseBody Map<String, Object> upload(
FileUpload uploadItem, BindingResult result, HttpServletResponse response) {
// File processing
Map<String, Object> jsonResult = new HashMap<String, Object>();
jsonResult.put("success", Boolean.TRUE);
return jsonResult;
}
在其他序列化没有问题的方法中,您只需调用returnresponse代码>并将自动序列化
方法toString()。对于更复杂的类,您必须更改它。我认为您可以使用Spring的@RequestMapping注释的“products”属性:
<bean id="jacksonMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
<property name="supportedMediaTypes">
<array>
<value>text/html</value>
<value>application/json</value>
</array>
</property>
</bean>
@RequestMapping(value=“/upload”,method=RequestMethod.POST,products=MediaType.TEXT\u HTML\u value)
public@ResponseBody地图上传(
FileUpload uploadItem、BindingResult结果、HttpServletResponse){
//文件处理
Map jsonResult=newhashmap();
jsonResult.put(“success”,Boolean.TRUE);
返回jsonResult;
}
在配置文件中,应使此内容类型可用:
文本/html
应用程序/json
这在Spring 3.1.1.RELEASE中提供,可能在旧版本中不起作用。我需要其他返回类型(Map)的解决方案。您解决了这个问题吗?你会分享你的解决方案吗?ty@astrocybernaute看看我的答案。我希望这将有助于“此请求标识的资源只能根据请求“accept”头()生成具有不可接受特征的响应。”当指定“products=MediaType.TEXT\u HTML\u VALUE”时,它将返回此错误
@RequestMapping(value = "/upload", method = RequestMethod.POST)
public @ResponseBody String upload(
FileUpload uploadItem, BindingResult result, HttpServletResponse response) {
ExtJSJsonResponse response = new ExtJSJsonResponse();
// File processing
response.addParam("success", true);
response.addParam("message", "All OK");
return response.toString();
}
@RequestMapping(value = "/upload", method = RequestMethod.POST, produces = MediaType.TEXT_HTML_VALUE)
public @ResponseBody Map<String, Object> upload(
FileUpload uploadItem, BindingResult result, HttpServletResponse response) {
// File processing
Map<String, Object> jsonResult = new HashMap<String, Object>();
jsonResult.put("success", Boolean.TRUE);
return jsonResult;
}
<bean id="jacksonMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter">
<property name="supportedMediaTypes">
<array>
<value>text/html</value>
<value>application/json</value>
</array>
</property>
</bean>