JSONValidationInterceptor的一个bug, 返回的json数据竟然被注释了
/* { "fieldErrors": {"password":["密码长度为3-20字符"]}} */
上面就是校验出错时JSONValidationInterceptor返回给我的结果,竟然被/* {} */包围。
这样产生的问题是:使用JQuery这样的ajax框架时,如果设置了返回数据类型dataType=json, jQuery会自动解析返回的字符串成为json对象,倒霉的是,非良构的json字串会被jQuery忽略掉,从而导致回调方法永远不会被执行。
查看源码发现问题在buildResponse方法中:
protected String buildResponse(ValidationAware validationAware) { //should we use FreeMarker here? StringBuilder sb = new StringBuilder(); sb.append("/* { "); if (validationAware.hasErrors()) { //action errors if (validationAware.hasActionErrors()) { sb.append("\"errors\":"); sb.append(buildArray(validationAware.getActionErrors())); } //field errors if (validationAware.hasFieldErrors()) { if (validationAware.hasActionErrors()) sb.append(","); sb.append("\"fieldErrors\": {"); Map<String, List<String>> fieldErrors = validationAware .getFieldErrors(); for (Map.Entry<String, List<String>> fieldError : fieldErrors .entrySet()) { sb.append("\""); //if it is model driven, remove "model." see WW-2721 sb.append(validationAware instanceof ModelDriven ? fieldError.getKey().substring(6) : fieldError.getKey()); sb.append("\":"); sb.append(buildArray(fieldError.getValue())); sb.append(","); } //remove trailing comma, IE creates an empty object, duh sb.deleteCharAt(sb.length() - 1); sb.append("}"); } } sb.append("} */"); /*response should be something like: * { * "errors": ["this", "that"], * "fieldErrors": { * field1: "this", * field2: "that" * } * } */ return sb.toString(); }
请看
sb.append("/* { ");
和
sb.append("} */");
这确实是个bug,倒不是开发人员故意而为之,在struts2.2.2会被fix掉。
如果你只能用2.2.2以前的版本,那就重新自定义这个interceptor,把上面的代码改为
sb.append(" { ");
和
sb.append(" } ");
即可。
相关推荐
yangkang 2020-11-09
lbyd0 2020-11-17
sushuanglei 2020-11-12
85477104 2020-11-17
KANSYOUKYOU 2020-11-16
wushengyong 2020-10-28
lizhengjava 2020-11-13
星月情缘 2020-11-13
huangxiaoyun00 2020-11-13
luyong0 2020-11-08
腾讯soso团队 2020-11-06
Apsaravod 2020-11-05
PeterChangyb 2020-11-05
gaobudong 2020-11-04
wwwjun 2020-11-02
gyunwh 2020-11-02
EchoYY 2020-10-31
dingyahui 2020-10-30