相关疑难解决方法(0)

杰克逊可以配置为从所有字符串属性修剪前导/尾随空格?

示例JSON(请注意该字符串具有尾随空格):

{ "aNumber": 0, "aString": "string   " }
Run Code Online (Sandbox Code Playgroud)

理想情况下,反序列化的实例将具有值为"string"aString属性(即没有尾随空格).这似乎是可能支持的东西,但我找不到它(例如在DeserializationConfig.Feature中).

我们正在使用Spring MVC 3.x,因此基于Spring的解决方案也可以.

我尝试根据论坛帖子中的建议配置Spring的WebDataBinder,但在使用Jackson消息转换器时它似乎不起作用:

@InitBinder
public void initBinder( WebDataBinder binder )
{
    binder.registerCustomEditor( String.class, new StringTrimmerEditor( " \t\r\n\f", true ) );
}
Run Code Online (Sandbox Code Playgroud)

java json spring-mvc jackson

39
推荐指数
6
解决办法
3万
查看次数

@InitBinder与@RequestBody在Spring 3.2.4中转义XSS

@RequestBody在我的方法中有一个带注释的参数,如下所示:

@RequestMapping(value = "/courses/{courseId}/{name}/comment", method = RequestMethod.POST)
@ResponseStatus(HttpStatus.OK)
public @ResponseBody CommentContainer addComment(@PathVariable Long courseId,
                          @ActiveAccount Account currentUser,
                          @Valid @RequestBody AddCommentForm form,
                          BindingResult formBinding,
                          HttpServletRequest request) throws RequestValidationException {

.....
}
Run Code Online (Sandbox Code Playgroud)

然后我@InitBinder在同一个控制器中有一个带注释的方法:

@InitBinder
public void initBinder(WebDataBinder dataBinder) {
    dataBinder.registerCustomEditor(AddCommentForm.class, new StringEscapeEditor());
}
Run Code Online (Sandbox Code Playgroud)

StringEscapeEditor没跑.但我的initBinder方法是.所以它没有将我的表单映射到转义编辑器.这似乎是在阅读此线程后(似乎@RequestMapping不支持@InitBinder):

处理ajax请求时不调用spring mvc @InitBinder

我测试了映射@PathVariable字符串,然后我的编辑器正在工作.

这在我的应用程序中是一个大问题,因为我的大多数绑定都已完成,@RequestBody如果我可以应用一些自定义绑定,那将是很好的.

解决这个问题最常用的方法是什么?并且为了脚本攻击而逃避我的输入数据.

ajax spring spring-mvc spring-data

4
推荐指数
1
解决办法
9374
查看次数

标签 统计

spring-mvc ×2

ajax ×1

jackson ×1

java ×1

json ×1

spring ×1

spring-data ×1