JAX-RS中特定于URI的ExceptionMapper [英] URI-specific ExceptionMapper in JAX-RS

查看:75
本文介绍了JAX-RS中特定于URI的ExceptionMapper的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Jersey和Guice作为我的IOC容器.我想知道是否可以将ExceptionMapper与特定的URI关联.原因是我想根据访问的URI不同地映射相同的异常.例如,假设我为自定义异常使用了以下两个异常映射器:

I'm using Jersey, and Guice as my IOC-container. I'd like to know if it is possible to associate an ExceptionMapper with a specific URI. The reason for this is that I want to map the same exception differently based on what URI was visited. For example, suppose I've got the following two exception mappers for my custom exception:

public class MyExceptionMapperForFirstURI implements
  ExceptionMapper<MyException> {..return response based on first URI..}

public class MyExceptionMapperForSecondURI implements
  ExceptionMapper<MyException> {..return response based on second URI..}

据我了解,您在ServletModule中绑定了ExceptionMapper,如下所示:

As far as I understand you bind an ExceptionMapper in your ServletModule as follows:

public class MyModule extends ServletModule {

    @Override
    public void configureServlets() {
        super.configureServlets();
        bind(MyCustomExceptionMapper.class);
    }
}

我该如何绑定MyExceptionMapperForFirstURIMyExceptionMapperForSecondURI,以便它们与正确的URI关联.这可能吗?如果可能的话:这是正确的方法吗?

How would I go about binding MyExceptionMapperForFirstURI and MyExceptionMapperForSecondURI so that they get associated with the correct URIs. Is this possible, and if possible: is this the correct way to do this?

推荐答案

这是很晚的答案;-),但是您始终可以注入UriInfo并在其上分支.所以,

This is quite late answer ;-) but you can always inject the UriInfo and branch on that. So,

@Context
UriInfo uriInfo;

.....

if (matchesA(uriInfo.getAbsolutePath())) {
    // do something
}

这篇关于JAX-RS中特定于URI的ExceptionMapper的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆