开发者

RestEasy ExceptionMapper not catching the exceptions

I'm throwing an exception MyCustomException from my application. (EJB Layer)

I've an exception mapper in web service layer which looks like following -

package net.webservices;

import javax.ws.rs.core.Response; 
import javax.ws.rs.ext.Provider; 
import net.common.MyCustomException;

@Provider 
public class E开发者_StackOverflow社区JBExceptionMapper implements 
ExceptionMapper<net.common.MyCustomException>  {

  public Response toResponse(MyCustomException exception) {
    return Response.status(Response.Status.BAD_REQUEST).build();    
}   

  }

I've registered my mapper in web.xml of the web service layer as following -

 <context-param>
    <param-name>resteasy.providers</param-name>
    <param-value>net.webservices.EJBExceptionMapper</param-value>        
</context-param>

The EJBExceptionMapper is not catching the MyCustomException. But instead its being caught by the catch block of the web service implementation.

What could be the problem?

Note: I don't want to register my ExceptionMapper manually using getProviderFactory().addExceptionMapper()


I don't know why your solution doesn't work (but I've never used RESTeasy, only Jersey). In any case, it would probably be simpler to extend WebApplicationException. That way, you don't have to register a provider:

public class MyCustomException extends WebApplicationException {
    public MyCustomException() {
        super(Response.status(Response.Status.BAD_REQUEST).build());
    }
}


You need to throw exception (of type MyCustomException ) in the catch block and add a "Throws MyCustomException" to the method signature

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜