RESTEasy service and ExceptionMapper
arteme Jun 21, 2014 2:47 PMDear SwitchYard community,
I have a problem (yet again). I have a SwitchYard 1.1 application with multiple HTTP and REST services. I'm looking for a way to add custom error messages (in case of error 404, 500 and such). My services talk JSON and are expected to return a JSON reply no matter, what circumstances are.
I would be content if I could just set the HTTP response body and status code myself in all circumstances and JBoss AS would just pass it to the client as-is. I fail completely at getting this kind of behavior from JBoss
I know, with RESTEasy there are exception mappers that can be used, but I cannot seem to get my exception mappers registered.
I have the following exception mapper:
@Provider public class CustomExceptionMapper implements ExceptionMapper<Exception> { @Override public Response toResponse(Exception e) { String body = "{}"; return Response.serverError() .entity(body).type(MediaType.APPLICATION_JSON_TYPE) .build(); } }
and I have the following web.xml:
<?xml version="1.0" encoding="UTF-8"?> <web-app xmlns="http://java.sun.com/xml/ns/javaee" version="3.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"> <context-param> <param-name>resteasy.providers</param-name> <param-value>ru.swisstok.esb.util.rest.CustomExceptionMapper</param-value> </context-param> <context-param> <param-name>resteasy.scan</param-name> <param-value>true</param-value> </context-param> </web-app>
Strictly speaking, I shouldn't need to add any context params to the web.xml file as on JBoss AS the application should be scan for RESTEasy annotations automatically. That is not happening.
I try to register the provider directly by supplying the "resteasy.providers" context-param and it is not being picked up either.
In fact, I don't know why my web.xml is no used at all. I break at org.jboss.resteasy.server.servlet.ConfigurationBootstrap#createDeployment() and see that getParameter() calls dealing with context parameters above don't return anything. In fact both getParameterNames() and getInitParameterNames() return empty lists. Why? How? I have a WAR that has /WEB-INF/web.xml as described above. I know it is parsed because if the XML is malformed, deployment fails with errors.
I have tried different things, but to no avail. Is there any hope for me?