春季QueryDSLPREDICATE-不良类型的例外处理
我没有找到任何适当的解决方案,所以也许您可以提供帮助。基本上问题很简单,我想在使用 querydslpredicate 我拥有ID是数字格式(在数据库方面)的实体时,要进行某种“更好”的错误处理
- 在实体级别的私有长度ID 。
这是我的问题,同时在正常情况下获取所有内容(如预期的场景,例如):
- /testendpoint& pageize = 100& id = 1
- /testendpoint& pageSize = 100& id = 1123123
所有功能都很好。但是,当我输入非数字值以ID = ID = Randomatest时,我得到了numberFormateXception
@GetMapping
public ApiResponsePage listTest(
@RequestParam(defaultValue = "0") final Integer pageNo,
@RequestParam(defaultValue = "10") final Integer pageSize,
@RequestParam(defaultValue = "id") final String sortBy,
@QuerydslPredicate(root = TestEntity.class) final Predicate predicate
) {
final Page<TestEntity> all = testEntityRepo.findAll(predicate);
return all;
}
案例,因为该异常被放在解析级别上,因此它甚至无法达到 testEntityRepo.findall(perseative); 。有什么方法可以以更“优雅”的方式处理此类例外?这样我可以输入一些自定义消息等?
日志:
信息18388 --- [main] dvSaseedEfaultExceptionReporter :get /testendpoint导致无法转换为从类型转换 [java.lang.string]键入[@javax.persistence.id java.lang.long] for 价值“ testrandomvalue”;嵌套异常是 java.lang.numberformatexception:输入字符串:“ testrandomvalue”
I didn't find any proper solution for this so maybe you can assist. Basically issue is ultra simple I want to do some kind of "better looking" error handling when using QuerydslPredicate
I have entity where ID is of Number format (on database side) - on entity level its private Long id.
And here come my problem while fetching everything in normal (expected scenarios like):
- /testEndpoint&pageSize=100&id=1
- /testEndpoint&pageSize=100&id=1123123
All work great. But when I enter non numeric value to id like id=randomTest I got NumberFormatException
@GetMapping
public ApiResponsePage listTest(
@RequestParam(defaultValue = "0") final Integer pageNo,
@RequestParam(defaultValue = "10") final Integer pageSize,
@RequestParam(defaultValue = "id") final String sortBy,
@QuerydslPredicate(root = TestEntity.class) final Predicate predicate
) {
final Page<TestEntity> all = testEntityRepo.findAll(predicate);
return all;
}
Case is that this exception is thrown on parsing level so its not even reaching testEntityRepo.findAll(predicate);. Is there any way to handle such exceptions in more "elegant" way ? So I can put some custom message etc ?
Log:
INFO 18388 --- [ main] d.v.s.a.s.e.DefaultExceptionReporter
: GET /testEndpoint caused Failed to convert from type
[java.lang.String] to type [@javax.persistence.Id java.lang.Long] for
value 'testRandomValue'; nested exception is
java.lang.NumberFormatException: For input string: "testRandomValue"
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
一个选项是使异常:
这是在工作,但是它会遇到这种类型的所有错误,因此,如果您有其他地方,它可能会在开销
The one option is to make exceptionHandler:
This is working, but its taking all errors of this type so if you have any other place where it appear it can be overhead