RESTEASY -Spring异常映射器,用于javax.ws.rs.NotFoundException未获得Involed

时间:2015-06-03 11:16:31

标签: spring resteasy

您好我在春季3使用Reasteasy 3。 我为我的项目定义了多个Exception映射器,并且每当引发异常时都会调用它们中的大多数。 但是我遇到**“javax.ws.rs.NotFoundException”的问题,当引发此异常时,它的处理程序不会被调用。** 包含“NotFoundException”异常映射的所有异常映射器都以相同的方式定义/配置,并且除了映射器之外,它们被调用以用于NotFoundException。

是否有任何不同的方法为JAX-RS异常配置异常映射器..请帮助我被困在这里。

code for Exception mappers
package com.xyz.exception;
import javax.ws.rs.NotFoundException;
import javax.ws.rs.core.Response;
import javax.ws.rs.ext.ExceptionMapper;
import javax.ws.rs.ext.Provider;

@Provider
public class CCDBNotFoundExceptionMapper implements ExceptionMapper<NotFoundException> {
        @Override
        public Response toResponse(NotFoundException e) {       
          // Logs and code          
        }
}

以Excatly类似方式配置和定义的其他异常映射器正在调用...请参阅快照以获取工作异常映射器

package com.xyz.exception;
import javax.ws.rs.core.Response;
import javax.ws.rs.ext.ExceptionMapper;
import javax.ws.rs.ext.Provider;
import org.jboss.resteasy.plugins.providers.jaxb.JAXBUnmarshalException;

@Provider
public class CCDBJAXBUnmarshalExceptionMapper implements ExceptionMapper<JAXBUnmarshalException> {
        @Override
        public Response toResponse(NotFoundException e) {       
          // Logs and code          
        }
}

package com.xyz.exception;
import javax.ws.rs.core.Response;
import javax.ws.rs.ext.ExceptionMapper;
import javax.ws.rs.ext.Provider;

@Provider
public class CCDBApplicationExceptionMapper implements ExceptionMapper<CCDBApplicationException>{
        @Override
        public Response toResponse(NotFoundException e) {       
          // Logs and code          
        }
}

提供者注释的Spring配置

 <!-- Auto Reegistry of RESTEasy providers -->
   <context:component-scan base-package="com.xyz">
    <context:include-filter type="annotation" expression="javax.ws.rs.ext.Provider"/>
   </context:component-scan>

1 个答案:

答案 0 :(得分:0)

我尝试了所有&#34; javax.ws.rs。*&#34; 例外的异常处理程序,但是它们正在注册时才会被调用。

为了克服这个问题,我使用了servlet过滤器...我发布了我的代码来帮助其他遇到同样问题的人

在web.xml文件中注册以下过滤器。

过滤代码:

public class CCDBExceptionFilter implements Filter {
    Logger logger = Logger.getLogger(CCDBExceptionFilter.class);
    private static final String NOTFOUND_MESSAGE =  "{\"ErrorResponse\":{\"errorCode\":1,\"errorMessage\":\"Invalid request, There is no service configured to handle provided request\"}}";
    public void init(FilterConfig filterConfig) throws ServletException {
        logger.debug("CCDBExceptionFilter got initiated" );
    }

    public void doFilter(ServletRequest request, ServletResponse response,FilterChain filterChain)throws IOException, ServletException {            
        HttpServletResponse res =  (HttpServletResponse) response;
        HttpServletRequest req =  (HttpServletRequest) request;     
        logger.debug("Request recived to process " + req.getPathInfo());        
        DummyResponse dummyResponse = new DummyResponse((HttpServletResponse)response);  
        filterChain.doFilter(request, dummyResponse);                
        logger.debug("Request handled for  " + req.getPathInfo());              
        if(404 == dummyResponse.getErrorcode()){
            logger.debug("Found 404 error code" );
            res.setStatus(400);
            PrintWriter out = response.getWriter();  
            out.println(NOTFOUND_MESSAGE);            
            out.close();             
        }
    }

    public void destroy() {
    }

}

虚拟响应类

public class DummyResponse extends HttpServletResponseWrapper {

    Logger logger = Logger.getLogger(DummyResponse.class);

    private CharArrayWriter buffer; // This can be used as an Writer
    int errorcode;

    public int getErrorcode() {
        return errorcode;
    }

    public void setErrorcode(int errorcode) {
        this.errorcode = errorcode;
    }

    public DummyResponse(HttpServletResponse response) {
        super(response);
        buffer = new CharArrayWriter();
    }

    public String toString() {
        return buffer.toString();
    }

    public PrintWriter getWriter() {
        return new PrintWriter(buffer);
    }

    public void setStatus(int sc) {
        if (404 == sc) {
            setErrorcode(404);
            super.setStatus(400);
        } else {
            super.setStatus(sc);
        }

    }

    public void sendError(int sc, String msg) throws IOException {
        if (404 == sc) {
            setErrorcode(404);
            super.setStatus(400);
        } else {
            super.setStatus(sc);
        }
    }

    public void sendError(int sc) throws IOException {
        if (404 == sc) {
            setErrorcode(404);
            super.setStatus(400);
        } else {
            super.setStatus(sc);
        }

    }

}