Amdatu:如何使ExceptionMapper(@Provider)工作? [英] Amdatu: How to make ExceptionMapper (@Provider) to work?

查看:129
本文介绍了Amdatu:如何使ExceptionMapper(@Provider)工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

正如我在多个文档和示例中所看到的,我正在尝试使用ExceptionMapper管理我的所有异常.但是,至少在我的条件下,它似乎不起作用.

I'm trying to manage all my exceptions with an ExceptionMapper, as i saw in multiple documentation and examples. However, it doesn't seem to work, at least in my conditions.

我在OSGI环境中,使用Felix Witheboard模式和Amdatu Wink,所以我没有web.xml,所有内容都应该由其自己管理. 我尝试像使用Web服务一样将ExceptionMapper注册为服务,但没有结果.

I'm in a OSGI environment, using the Felix Witheboard pattern, with Amdatu Wink, so i don't have a web.xml and everything is supposed to be managed by itself. I tried to register my ExceptionMapper as a service as i did with my web services, with no results.

@Component(immediate=true, provide={Object.class})
@Provider
public class SessionTimeoutExeptionHandler implements ExceptionMapper<SessionTimeoutException>{

    public Response toResponse(SessionTimeoutException arg0) {
        Response toReturn = Response.status(Status.FORBIDDEN)
                .entity("session_timeout")
                .build();

        return toReturn;
    };
}

我不在乎响应本身,我只是在玩耍.

Don't pay attention to the Response itself, i was just playing around.

我的代码从未被调用过,我应该如何设置该提供程序?

My code is never called, how am i supposed to setup that provider?

推荐答案

您必须在javax.ws.rs.core.Application中注册该提供程序.该应用程序应被注册为具有比Amdatu Wink捆绑包创建的默认服务等级更高的服务等级的服务.

You have to register the Provider in a javax.ws.rs.core.Application. That Application should be registered as a service with a higher service ranking than the default one created by the Amdatu Wink bundle.

以下是一个有效的示例.

The following is a working example.

异常映射器本身:

@Provider
public class SecurityExceptionMapper implements ExceptionMapper<SecurityException>{
  @Override 
  public Response toResponse(SecurityException arg0) {
    return Response.status(403).build();
  }
}

应用程序:

import java.util.HashSet;
import java.util.Set;
import javax.ws.rs.core.Application;
import com.fasterxml.jackson.jaxrs.json.JacksonJsonProvider;

public class MyApplication extends Application {

  @Override
  public Set<Object> getSingletons() {
    Set<Object> s = new HashSet<Object>();

    s.add(new JacksonJsonProvider());
    s.add(new SecurityExceptionMapper());

    return s;
  }
}

激活器设置服务排名属性.

Activator setting the service ranking property.

public class Activator extends DependencyActivatorBase{
  @Override
  public void destroy(BundleContext arg0, DependencyManager arg1) throws Exception {
  }

  @Override
  public void init(BundleContext arg0, DependencyManager dm) throws Exception {

    Properties props = new Properties();
    props.put(Constants.SERVICE_RANKING, 100);

    dm.add(createComponent().setInterface(Application.class.getName(), props).setImplementation(MyApplication.class));
  }
}

这篇关于Amdatu:如何使ExceptionMapper(@Provider)工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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