JAX-RS异常映射器不处理参数转换时抛出的异常?
我使用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好的找到了答案。事实证明,尝试从字符串转换为对象时抛出的任何异常都将被包装在 QueryParamException 中,因此将由 QueryParamException ExceptionMapper 处理(如果有的话)。解决这个问题的一种方法是实际识别 QueryParamException 包装的异常,并使用某种映射将 QueryParamException 的原因与正确的处理程序相匹配
,
当然,您需要使用正确的异常及其异常加载您的映射映射器。我通过让 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
and
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.
尝试使用不同的 JAX-RS 框架。 (实际上你用的是哪个框架?)
我认为在 Wink 中你的场景会起作用。
Try to use different JAX-RS framework. (Actually which framework do you use?)
I think in Wink your scenario will work.