RESTEasy + spring,未发现异常未被映射器捕获

时间:2014-02-21 00:15:31

标签: java spring rest exception-handling resteasy

我有一些休息资源可以很好地工作但是当我对一个无效的URL进行REST调用时,JBoss会返回一个404 html异常。我想将其更改为JSON异常响应。我尝试过创建映射器,但控件没有到达那里。我正在为ref。添加我的映射器代码。

@Provider
@Produces(MediaType.APPLICATION_JSON)
public class NotFoundExceptionMapper implements ExceptionMapper<NotFoundException> {

/**
 * Map an exception to a {@link javax.ws.rs.core.Response}.
 *
 * @param exception the exception to map to a response.
 * @return a response mapped from the supplied exception.
 */
@Override
public Response toResponse(final NotFoundException exception) {
    Map<String, Object> info = new HashMap<>();
    info.put("msg", exception.getMessage());
    info.put("date", new Date());
    info.put("details", "The requested resource hasn't been found");

    return Response
            .status(Response.Status.INTERNAL_SERVER_ERROR)
            .entity(info)
            .type(MediaType.APPLICATION_JSON)
            .build();
}

1 个答案:

答案 0 :(得分:1)

您可以通过多种不同的方式注册@Provider注释类:

  1. Resteasy自动扫描为ServletContextListener。
  2. 在web.xml中显式配置为ServletContextListener。
  3. 明确配置为独立javax.ws.rs.Application
  4. 由像Spring这样的IOC容器自动扫描。
  5. 由Resteasy自动扫描

    通过设置@Provider上下文参数,将您的应用程序配置为自动扫描web.xml文件中的resteasy.scan.providers带注释的类。

    <web-app>
       <listener>
          <listener-class>
             org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap
          </listener-class>
       </listener>
    
        <context-param>
            <param-name>resteasy.scan.providers</param-name>
            <param-value>true</param-value>
        </context-param>
    
       <servlet>
          <servlet-name>Resteasy</servlet-name>
          <servlet-class>
             org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher
          </servlet-class>
       </servlet>
    
       <servlet-mapping>
          <servlet-name>Resteasy</servlet-name>
          <url-pattern>/resteasy/*</url-pattern>
       </servlet-mapping>
    
    </web-app>
    

    注意:如果要在JBoss 7或更高版本上部署,请不要使用此选项。在JBoss的版本7及更高版本中自动启用@Provider,并且使用此值可能会在应用程序启动期间导致错误。


    在web.xml中显式配置

    通过设置resteasy.providers上下文参数,可以在web.xml文件中向Resteasy显式注册提供者。

    <web-app>
    
        <context-param>
            <param-name>resteasy.providers</param-name>
            <param-value>com.foo.NotFoundExceptionMapper,com.foo.SomeOtherProvider</param-value>
        </context-param>
    
       ... All of the other HttpServletDispatcher and ResteasyBootstrap stuff like above.
    </web-app>
    

    显式配置为独立应用

    如果您使用javax.ws.rs.Application类引导Resteasy服务,可以按如下方式添加提供程序实现:

     public class FooApplication extends Application
     {
         private Set<Object> singletons = new HashSet<Object>();
         private Set<Class> classes = new HashSet<Class>();
    
         public FooApplication()
         { 
             classes.put(NotFoundExceptionMapper.class);
         }
    
         public Set<Class<?>> getClasses()
         {
             return classes;
         }
    
         public Set<Object> getSingletons()
         {
             return singletons;
         }
     }
    

    Spring自动扫描

    如果您使用Spring扫描您的Resteasy提供程序,您需要告诉Spring您对@Provider注释感兴趣,并且您需要配置Spring以告诉Resteasy它已扫描的bean。

    <强> ExceptionMapper

    您需要向提供者添加@Component注释,以便Spring知道扫描它。

    @Component
    @Provider
    @Produces(MediaType.APPLICATION_JSON)
    public class NotFoundExceptionMapper implements ExceptionMapper<NotFoundException> 
    {
       //Implementation removed for brevity...
    }
    

    <强> Web.xml中

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app version="3.0"
             xmlns="http://java.sun.com/xml/ns/javaee"
             xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
             xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd">
    
        <!-- Spring Configuration -->
        <context-param>
            <param-name>contextClass</param-name>
            <param-value>org.springframework.web.context.support.AnnotationConfigWebApplicationContext</param-value>
        </context-param>
    
        <context-param>
           <param-name>contextConfigLocation</param-name>
           <param-value>com.foo.SpringConfig</param-value>
        </context-param>
    
        <!-- RESTEasy Configuration -->
        <listener>
            <listener-class>org.jboss.resteasy.plugins.server.servlet.ResteasyBootstrap</listener-class>
        </listener>
    
        <context-param>
            <param-name>resteasy.servlet.mapping.prefix</param-name>
            <param-value>/v1</param-value>
        </context-param>
    
        <!-- RESTEasy <-> Spring Connector (Needed so that RESTEasy has access to Spring managed beans!) -->
        <listener>
            <listener-class>org.jboss.resteasy.plugins.spring.SpringContextLoaderListener</listener-class>
        </listener>
    
        <!-- RESTEasy HTTP Request Processor Servlet -->
        <servlet>
            <servlet-name>resteasy-servlet</servlet-name>
            <servlet-class>org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher</servlet-class>
        </servlet>
    
        <servlet-mapping>
            <servlet-name>resteasy-servlet</servlet-name>
            <url-pattern>/v1/*</url-pattern>
        </servlet-mapping>
    
    </web-app>