JAX-RS异常映射器不处理参数转换时抛出的异常?

发布于 2024-10-16 09:16:50 字数 1042 浏览 6 评论 0原文

我使用 fromString 方法创建了自己的类,我相信 JAXB 将使用该方法将参数值字符串转换为我的对象。然而,在这个 fromString 方法中,我有一个 try catch 块,它会引发异常。就像下面这样。

public class Animal{
    public static Animal fromString(final String input){ 

        try{ 
            ... 
        }catch(IllegalArgumentException ae){ 
            throw new CannotConvertAnimalException(); //this is my custom exception 
        } 
    }
}

然后我有一个 CannotConvertAnimalException 的映射器,如下所示:

@Component
@Provider
public class CannotConvertAnimalExceptionHandler implements ExceptionMapper<CannotConvertAnimalException>

问题是在我的资源方法中。我使用 Animal 数据类型

@GET
@Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
public Response showAnimalInfo(Animal animal){....}

事实证明,当要转换为 Animal 的参数字符串抛出 CannotConvertAnimalException 时,我的自定义异常映射器无法处理它。它继续抛出 ParamException.QueryParamException 并将其交给我的 QueryParamException 处理程序来处理响应。

你们知道如何做到这一点,以便当转换出错时,当抛出 CannotConvertAnimalException 时,正确的映射器可以处理它吗?

I created my own class with a fromString method which I believe JAXB will use to convert the parameter value string to my object. However, inside this fromString method, I have a try catch block which bubbles up the exception. Like the following.

public class Animal{
    public static Animal fromString(final String input){ 

        try{ 
            ... 
        }catch(IllegalArgumentException ae){ 
            throw new CannotConvertAnimalException(); //this is my custom exception 
        } 
    }
}

And then I have a mapper for CannotConvertAnimalException like the following:

@Component
@Provider
public class CannotConvertAnimalExceptionHandler implements ExceptionMapper<CannotConvertAnimalException>

The thing is that in my resource method. I use the data type Animal

@GET
@Produces({MediaType.APPLICATION_XML, MediaType.APPLICATION_JSON})
public Response showAnimalInfo(Animal animal){....}

It turns out that when the parameter string that is to be converted to Animal throws the CannotConvertAnimalException my custome exception mapper does not handle it. It goes on to throw ParamException.QueryParamException instead and gives it to my QueryParamException handler to handle the response.

Do you guys have any idea how to make it so that when the conversion goes bad, and when the CannotConvertAnimalException gets thrown, the correct mapper handles it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

临风闻羌笛 2024-10-23 09:16:50

好的找到了答案。事实证明,尝试从字符串转换为对象时抛出的任何异常都将被包装在 QueryParamException 中,因此将由 QueryParamException ExceptionMapper 处理(如果有的话)。解决这个问题的一种方法是实际识别 QueryParamException 包装的异常,并使用某种映射将 QueryParamException 的原因与正确的处理程序相匹配

Map<Exception, ExceptionMapper> mapper;

catch(Exception e){
    exMapper = mapper.get(exception.getCause);
}

当然,您需要使用正确的异常及其异常加载您的映射映射器。我通过让 spring 向该变量注入一个映射来做到这一点。

Ok found the answer to this. It turns out that whatever exception you throw when trying to convert from string to object will be wrapped inside QueryParamException and thus will be handled by QueryParamException ExceptionMapper (if you have one). One way to get around this is to actually identify what exception is wrapped by QueryParamException and use some kind of Map to match the cause of the QueryParamException to the correct handler

Map<Exception, ExceptionMapper> mapper;

and

catch(Exception e){
    exMapper = mapper.get(exception.getCause);
}

of course you would need to load up yor map with the right exception and its mapper. I do this by having spring injects a map to this variable.

烦人精 2024-10-23 09:16:50

尝试使用不同的 JAX-RS 框架。 (实际上你用的是哪个框架?)
我认为在 Wink 中你的场景会起作用。

Try to use different JAX-RS framework. (Actually which framework do you use?)
I think in Wink your scenario will work.

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