So I'm building a web application, we are using JPA and Jersey to consume/produces JSON data.
I have a custom "EntityException" aswell as a custom "EntityExceptionMapper"
Here's the mapper:
@Provider
public class EntityExceptionMapper implements ExceptionMapper<EntityException> {
public EntityExceptionMapper() {
System.out.println("Mapper created");
}
@Override
public Response toResponse(EntityException e) {
System.out.println("This doesnt print!");
return Response.serverError().build();
}
}
My Exception:
public class EntityException extends Exception implements Serializable{
public EntityException(String message) {
super(message);
System.out.println("This prints...");
}
}
And I'm calling it from a REST call:
@POST
@Path("/test")
@Produces(MediaType.APPLICATION_JSON)
public String test() throws EntityException{
throw new EntityException("This needs to be send as response!!");
//return "test";
}
My problem is that, when the above exception is thrown, I get in the constructor (prints: "This prints...") Edit: I also get the: "Mapper created!"
But my response is empty, and I don't get to the sys out of my toResponse method. This is really similar to the example on the jersey website:
https://jersey.java.net/nonav/documentation/1.12/jax-rs.html#d4e435
What am I missing??
I used spring to wire up jersey app and used @Component with @Provider.
When I moved to jersey v > 2.5, it stopped working.
I resolved this very issue by putting @Singleton annotation instead of @Component alongside @Provider, like this:
I have encountered the same issue while develop sample REST API. While creating REST API i have given base package name like org.manish.rest.message, I supposed to create every other packages under the base package like this
org.manish.rest.message.model
org.manish.rest.message.database
org.manish.rest.message.resource
in web.xml init param was given like this
It means, i have registered my base package in web.xml, what ever package i will create under this; will be consider by JAX-RS based on my call and requirement. But when i created my exception package by mistake i put package name org.manish.rest.exception. Since this was not registered in web.xml so my complete exception class was not considered to handle exception by JAX-RS. As a correction, i have just modified my exception package name from
org.manish.rest.exception
toorg.manish.rest.message.exception
After that i executed once in post man and i got expected result.
Hope this can solve your query.
Thanks Manish
I'm using the Jersey
JdkHttpServerFactory
, and I just had to add theExceptionMapper
class as a resource, just like my other controller resources:I also face the same issue.Just add the package name that have the ExceptionMappperHandler classes.
Here,service contain all service classes and Utilities.ExceptionMapper contains all exceptionMapper. Hope its help
I had the same problem. I just had to modify the web.xml. Previously in my web.xml file param-value was
com.two95.restful.resource
I just changed to root packagecom.two95.restful
. Then it started working like a charm with just the@Provider
annotation.I am using deployment agnostic application model so the following worked for me: