春季QueryDSLPREDICATE-不良类型的例外处理

发布于 2025-01-28 23:31:46 字数 1237 浏览 3 评论 0原文

我没有找到任何适当的解决方案,所以也许您可以提供帮助。基本上问题很简单,我想在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

情痴 2025-02-04 23:31:46

一个选项是使异常:

@ExceptionHandler(ConversionFailedException.class)
public ResponseEntity conversionFailedException(ConversionFailedException ex) {
     return ResponseEntity
            .badRequest()
            .contentType(APPLICATION_JSON_UTF8)
            .body(createResponseBody(ex.getMessage()));
}

这是在工作,但是它会遇到这种类型的所有错误,因此,如果您有其他地方,它可能会在开销

The one option is to make exceptionHandler:

@ExceptionHandler(ConversionFailedException.class)
public ResponseEntity conversionFailedException(ConversionFailedException ex) {
     return ResponseEntity
            .badRequest()
            .contentType(APPLICATION_JSON_UTF8)
            .body(createResponseBody(ex.getMessage()));
}

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文