java - 配置的 404 错误页面未显示在 JAX-RS 应用程序中

标签 java jax-rs

我遇到以下问题:我创建了一个 Java Web 应用程序。此外,我还创建了一些 REST 端点。在 web.xml 中,我使用标签定义了 404 错误的重定向。它适用于除不存在的 REST 端点之外的所有地址。我实现了 RestApplication 类:

import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;

@ApplicationPath("rest")
public class RestApplication extends Application {
}

和一个端点,myView:

@Path("/myView")
public class MyView {

    @GET
    @Path("/")
    public Response myViewPage() {
        //some code goes here...
    }
}

现在,当我尝试输入不存在的端点时,我们说“aaa”,即我输入地址:http://localhost:8080/mysite/rest/aaa ,我收到 404 错误,但重定向不起作用。对于非 REST 地址,例如:http://localhost:8080/mysite/somesitethatdoesnotexist ,重定向工作正常。我的 web.xml 如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
                            xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
                            xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
                            version="3.1">
                            
    <context-param>
       <param-name>resteasy.document.expand.entity.references</param-name>
       <param-value>false</param-value>
    </context-param>

    <servlet>
        <servlet-name>faces</servlet-name>
        <servlet-class>javax.faces.webapp.FacesServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <servlet-mapping>
        <servlet-name>faces</servlet-name>
        <url-pattern>*.xhtml</url-pattern>
    </servlet-mapping>
    <servlet-mapping>
        <servlet-name>faces</servlet-name>
        <url-pattern>*.jsf</url-pattern>
    </servlet-mapping>
    
    <welcome-file-list>
        <welcome-file>welcome.xhtml</welcome-file>
    </welcome-file-list>
    
    <error-page>
        <error-code>404</error-code>
        <location>/WEB-INF/errorpages/404.xhtml</location>
    </error-page>

    <security-constraint>
        <web-resource-collection>
            <web-resource-name>restricted methods</web-resource-name>
            <url-pattern>/*</url-pattern>
            <http-method>PUT</http-method>
            <http-method>DELETE</http-method>
            <http-method>OPTIONS</http-method>
            <http-method>TRACE</http-method>
            <http-method>HEAD</http-method>
        </web-resource-collection>
        <auth-constraint />
    </security-constraint>

</web-app>

我还尝试使用 ExceptionMapper,即我实现了 EntityNotFoundExceptionMapper 类:

@Provider
public class EntityNotFoundExceptionMapper implements ExceptionMapper<NotFoundException> {

    @Override
    public Response toResponse(NotFoundException ex) {
      // some code for redirect
    }
}

并将其添加到 RestApplication 类中:

@ApplicationPath("rest")
public class RestApplication extends Application {
    public Set<Class<?>> getClasses() {
        Set<Class<?>> s = new HashSet<Class<?>>();
        s.add(EntityNotFoundExceptionMapper.class);
        return s;
    }
}

但是没有成功。但是,当我删除以下内容时,它起作用了:

<error-page>
    <error-code>404</error-code>
    <location>/WEB-INF/errorpages/404.xhtml</location>
</error-page>

来自 web.xml。但不幸的是,重定向当然不适用于非 REST 地址。

任何人都可以帮助我解决此问题并提出解决方案,让我能够在 REST 和非 REST 地址出现 404 错误时提供重定向吗?预先感谢您!

编辑:

按照@andih的评论,对于REST服务,我想在资源不可用的情况下返回配置的404错误页面。

最佳答案

不太确定为什么会找到您的其余资源,但对于 web.xml 的复杂部署和注释要工作,你需要做更多的事情。

您的RestApplication类必须扩展 javax.ws.rs.core.Application定义 RESTful Web 服务应用程序部署的组件。更多详情javax.ws.rs.core.Application ,请参阅 Javadoc http://download.oracle.com/javaee/6/api/javax/ws/rs/core/Application.html

在您的Application内子类您必须覆盖 getClasses()getSingletons()方法,根据需要返回 RESTful Web 服务资源列表。

例如

@ApplicationPath("/rest")
public class RestApplication extends Application {
    
    @Override
    public Set<Class<?>> getClasses() {
        Set<Class<?>> s = new HashSet<Class<?>>();
        s.add(HelloResource.class);
        return s;
    }

}

使用 Hello REST 资源。

@Path("/hello")
public class HelloResource {

    @GET
    @Produces(MediaType.TEXT_PLAIN)
    public String getHello() {
        return "Hello!";
    }
}

在您的 web.xml 中您必须定义您的 REST 应用程序。 如果需要,您可以配置多个。

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee
http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" 
id="WebApp_ID" version="3.0">
    <servlet>
        <!-- The servlet name is the full qualified name of your rest application it must be a subclass of java.ws.rs.Application --> 
        <servlet-name>org.example.restexample.RestApplication</servlet-name>
        <!--servlet-class is not needed -->
        <init-param>
            <param-name>javax.ws.rs.Application</param-name>
            <!-- As init parameter pass the full qualified name of the java.ws.rs.Application subclass --> 
            <param-value>org.example.restexample.RestApplication</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <!-- other servlet definitions --> 
    

    <--servlet mapping --> 

    <error-page>
        <error-code>404</error-code>
        <location>/NotFound.html</location>
    </error-page>
</web-app>

您可以为您的 JaxRS 应用程序指定 servlet 映射。如果您这样做,<servlet-mapping>将优先。

如果像这样的资源,上面的示例将返回配置的 404 错误页面 请求“.../rest/foo”,而“.../rest/hello”返回“Hello”。

上面的示例是使用 jersey 2.26-b03 和 tomcat 8.5.15 进行测试的。

关于java - 配置的 404 错误页面未显示在 JAX-RS 应用程序中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44452107/

相关文章:

java - Java中的文本文件解析

java - AdapterViewPager添加urlImage 3次问题 - AndroidX

java - 保存上传图像的最佳实践

java - RestEasyClientBuild 注册 ResteasyJackson2Provider - 关于重复的警告

javascript - CORS - Java 和 Javascript

java - 如何更新然后选择更新的行

java - 如何使用数据库对 Java 代码中的用户进行身份验证

java - 在 Heroku 上运行 maven 项目会出现应用程序错误

java - 我可以从 JAX-RS 调用返回 Date 对象吗?

rest - JAX-RS 2.0 更改默认实现