开发者

RestEasy ExceptionMapper not catching the exceptions

开发者 https://www.devze.com 2023-04-04 16:14 出处:网络
I\'m throwing an exception MyCustomException from my application. (EJB Layer) I\'ve an exception mapper in web service layer which looks like following -

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

精彩评论

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