Ask Questions and Find Answers
Important:
Ask is now read-only. You can review any existing questions and answers, but not add anything new.
But - don't panic! While ask is no more, we've replaced it with discuss - the new Liferay Discussion Forum! Read more here here or just visit the site here:
discuss.liferay.com
RE: How use javax.ws.rs.ext.ExceptionMapper in osgi module
The problem I think is tha the providers exception mapper are not registered as the jax-rs context. But I dont know how to config it ok.
The code is something like this:
@Provider
public class UnauthorizedUserExceptionMapper implements ExceptionMapper<unauthorizeduserexception> {
@Override
public Response toResponse(UnauthorizedUserException e) {
return Response
.status(Response.Status.UNAUTHORIZED)
.type(MediaType.APPLICATION_JSON)
.entity(e.getMessage())
.build();
}
}
</unauthorizeduserexception>
The rest services are working ok. I tryed to use the Exceptoin mapper as osgi @Component and also add the object to the Rest application singletons.
But is not working yet.
Any helo on this, how to cacth the exception inside this mapper?
Thanks in advance!
Hello Joaquin,
I came across your question today when investigating the same requirement. Sadly there has been no response on your question yet. Perhaps you have solved it already given it has been 4 years?
After some research I found an answer to our problem. Using the example in this pull request from last year: https://github.com/liferay-appsec/liferay-portal/pull/546/files#diff-4ab4c23659f671466b78053e7413e5d2a1e53a069a3976a77e7ff9022060713d
Configure the @Component annotation as below and your custom ExceptionMapper is used for the specific exception. You also don't need to provide the @Provider annotation.
@Component(
property = {
"osgi.jaxrs.application.select=(!(liferay.access.control.disable=true))",
"osgi.jaxrs.extension=true"
},
service = ExceptionMapper.class
)
Next step in my research: is it possible to combine multiple
ExceptionMappers in one global class/configuration (iso having
separate components per exceptionmapper/exceptiontype). Sort of like
how in Spring Boot the @ControllerAdvice works with multiple
@ExceptionHandler methods.
Maybe this will bring me to a
solution:
https://github.com/eugenp/tutorials/blob/master/jersey/src/main/java/com/baeldung/jersey/exceptionhandling/ExceptionHandlingConfig.java.
But I still have to investigate, will come back to you on this.
Or maybe you already have some kind of solution for this?
Powered by Liferay™