java spring请求正文中的转义引号

编程入门 行业动态 更新时间:2024-10-20 03:28:53
本文介绍了java spring请求正文中的转义引号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个 Java Spring 控制器.我想转义我的请求中的所有引号(例如在 SQL 查询中使用它来清理它).

I have a Java Spring controller. I want to escape all quotes in my request (sanitize it for using it in SQL queries for example).

有没有办法用 Spring 做到这一点?

Is there a way to do that with Spring ?

示例:

@RequestMapping(method = RequestMethod.POST) public List<String[]> myEndpoint(@RequestBody Map<String, String> params, @AuthenticationPrincipal Account connectedUser) throws Exception{ return myService.runQuery(params, connectedUser); }

推荐答案

如果要验证控制器中的所有请求参数,可以使用自定义验证器.如需完整信息,请查看完整示例

If you want to validate all your request parameters in controllers, you can use custom validators. For Complete info, check Complete Example

简要概述:

@Component public class YourValidator implements Validator { @Override public boolean supports(Class<?> clazz) { return clazz.isAssignableFrom(YourPojoType.class); } @Override public void validate(Object target, Errors errors) { if (target instanceof YourPojoType) { YourPojoType req = (YourPojoType) target; Map<String, String> params = req.getParams(); //Do your validations. //if any validation failed, errors.rejectValue("yourFieldName", "YourCustomErrorCode", "YourCustomErrorMessage"); } } }

控制器

@RestController public class YourController{ @Autowired private YourValidator validator; @RequestMapping(method = RequestMethod.POST) public List<String[]> myEndpoint(@Valid YourPojoType req, BindingResult result, @AuthenticationPrincipal Account connectedUser) throws Exception{ if (result.hasErrors()) { //throw exception } return myService.runQuery(params, connectedUser); } @InitBinder private void initBinder(WebDataBinder binder) { binder.setValidator(validator); }

}

更多推荐

java spring请求正文中的转义引号

本文发布于:2023-10-12 23:55:43,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1486230.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:引号   文中   java   spring

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!