We have two version of APIs in a monolith architecture. Each one of them has a GenericExceptionHandler which extends JAX-RS's ExceptionMapper. Once I bind the ExceptionMappers as providers for the APIs using Guice, the second ExceptionMapper takes over the mapping of exceptions for the first one, which is not desired.
Is there any way to make them co-exist, for example all the exceptions that are thrown inside the v1 api to be dealt with by the first Exception Mapper and all the exceptions that are thrown inside the v2 api to be dealt with by the first ExceptionMapper.
//Guice configuration Class
private static final List<Class> v2Providers = Arrays.asList(
...,
GenericExceptionMapperV2.class,
...,
);
private static final List<Class> v1Providers = Arrays.asList(
...,
GenericExceptionMapperV1.class,
...,
);
private void bindV2() { // same for v1
install(new v2ImplModule());
List<Class> providers = getProvidersBound(v2Providers);
String resources = Stream.of(... // classes)
.map(Class::getName)
.collect(Collectors.joining(","));
final Map<String, String> initParams = computeInitParams(resources, providers, v2path);
serve(v2path).with(new HttpServletDispatcher(), initParams);
}
private Map<String, String> computeInitParams(String resources, List<Class> providers, String path) {
final Map<String, String> initParams = new HashMap<>();
String prods = providers.stream().map(Class::getName).collect(Collectors.joining(","));
initParams.put(PREFIX_KEY, path);
initParams.put(RESOURCE_KEY, resources);
initParams.put(PROVIDER_KEY, prods);
return initParams;
}
I've tried
Multibinder<ExceptionMapper<Exception>> multibinder =
Multibinder.newSetBinder(binder(), new TypeLiteral<ExceptionMapper<Exception>>() {});
multibinder.addBinding().to(GenericExceptionMapperV1.class);
multibinder.addBinding().to(GenericExceptionMapperV2.class);
but it still doesn't work