user1751547
user1751547

Reputation: 2321

Cxf exception mapper not being invoked

I have created a custom exception and I have an exception mapper that I want to be invoked when my exception is thrown, so that I can return proper http return codes and messages, but for some reason it doesn't work for my exception.

Here is the relevant code

My Exception class:

public class MyException extends ApplicationRuntimeException
{

    private static final long serialVersionUID = 1L;

    public MyException ()
    {
        super();
    }

    public MyException (String message)
    {
        super(message);
    }

    public MyException (String message, ErrorCode errorCode)
    {
        super(message, errorCode);
    }

    public MyException (Throwable t)
    {   
        super(t);
    }

    public MyException (String message, Throwable t)
    {
        super(message, t);
    }

}

My exception mapper class

@Provider
public class MyExceptionMapper implements ExceptionMapper<Exception>
{
    @Override
    public Response toResponse(Exception exception)
    {
        //code
        return response;
    }
}

cxf-config

<jaxrs:server id="serviceId" address="/">
        <jaxrs:providers>
            <bean class="com.package.MyExceptionMapper"/>
        </jaxrs:providers>
</jaxrs:server>

This is essentially what my code is like, is there anything I'm missing or any other information I need to provide in order to resolve this issue?

The exception and exception mapper are located in two different projects, but I don't think that should matter because they are both in the class path.

Thanks

Upvotes: 3

Views: 5214

Answers (2)

khoi nguyen
khoi nguyen

Reputation: 409

The exception mapper just handle exceptions thrown by service beans.

One option in this case is to throw a new WebApplicationException(Throwable, Response) in which the response should be built the same way as what've been done in your exception mapper.

If you are trying to do some validation, creating a custom message body reader is one good option, I think. By doing this, inside the message body reader, you also be able to access to other providers/exception mappers.

Good luck!

Upvotes: 1

fpmoles
fpmoles

Reputation: 1219

If this is your full CXF, you are missing the service bean in this server being executed.

The provider is tied to the service and will only execute for the service beans also defined in the server.

Aside from that everything looks fine so I would check and make sure your exception is really being thrown.

Upvotes: 5

Related Questions