[英]Amdatu: How to make ExceptionMapper (@Provider) to work?
I'm trying to manage all my exceptions with an ExceptionMapper, as i saw in multiple documentation and examples. 正如我在多个文档和示例中所看到的,我正在尝试使用ExceptionMapper管理所有异常。 However, it doesn't seem to work, at least in my conditions.
但是,至少在我的条件下,它似乎不起作用。
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. 我在OSGI环境中,使用带有Amdatu Wink的Felix Witheboard模式,所以我没有web.xml,所有内容都应该由它自己管理。 I tried to register my ExceptionMapper as a service as i did with my web services, with no results.
我尝试像使用Web服务一样将ExceptionMapper注册为服务,但没有结果。
@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. 不要关注Response本身,我只是在玩。
My code is never called, how am i supposed to setup that provider? 我的代码从未被调用过,我应该如何设置该提供程序?
You have to register the Provider in a javax.ws.rs.core.Application. 您必须在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.
该应用程序应被注册为服务等级高于Amdatu Wink捆绑包创建的默认服务等级的服务。
The following is a working example. 以下是一个工作示例。
The Exception Mapper itself: 异常映射器本身:
@Provider
public class SecurityExceptionMapper implements ExceptionMapper<SecurityException>{
@Override
public Response toResponse(SecurityException arg0) {
return Response.status(403).build();
}
}
The Application: 应用程序:
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));
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.