简体   繁体   中英

ExceptionMapper not handling my Exceptions. JBOSS

I have a custom ExceptionMapper that catches my custom RuntimeException . The problem is that ExceptionMapper doesn't get invoked when i throw my custom Exception .

I have set @Provider at my ExceptionMapper class and also registered it in web.xml as:

<context-param>
    <param-name>resteasy.providers</param-name>
    <param-value>com.package.myMapper</param-value>
</context-param>

And my mapper is :

import javax.ws.rs.core.Response;
import javax.ws.rs.ext.ExceptionMapper;
import javax.ws.rs.ext.Provider;

@Provider
public class ResponseExceptionMapper implements ExceptionMapper<ResponseException>{

    @Override
    public Response toResponse(ResponseException arg0) {
        // TODO Auto-generated method stub
        System.out.println("in my mapper");
        return Response.status(513).entity(arg0.getMessage()).build();  
    }

}

My exception:

    public class ResponseException extends RuntimeException {

    public ResponseException() {
        super();
    }

    public ResponseException(String msg) {
        super(msg);
    }

    public ResponseException(String msg, Exception e) {
        super(msg, e);
    }
}

What am I doing wrong?...

Try to have your ResponseException extend Exception instead of RuntimeException. Seems like resteasy and jboss is catching your exception instead of your provider, I had the exact same problem and it was solved extending Exception instead:


    public class ResponseException extends Exception {

        public ResponseException() {
            super();
        }


        public ResponseException(String msg) {
            super(msg);
        }

        public ResponseException(String msg, Exception e) {
            super(msg, e);
        }
    }

And an example of a exception mapper:


    import org.apache.log4j.Logger;

    import javax.ws.rs.core.Response;
    import javax.ws.rs.ext.ExceptionMapper;
    import javax.ws.rs.ext.Provider;

    @Provider
    public class BadRequestExceptionMapper implements ExceptionMapper {

        private static final Logger log = Logger.getLogger(BadRequestExceptionMapper.class);

        @Override
        public Response toResponse(BadRequestException e) {

            log.info("Bad request: " + e.getMessage());

            return Response.status(Response.Status.BAD_REQUEST).entity(e.getMessage()).build();
        }
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM