【发布时间】:2017-06-13 16:07:09
【问题描述】:
我有以下问题:我创建了一个 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错误页面。
【问题讨论】:
-
您能否更详细地解释您的期望。 Http 状态码 404 表示“未找到”。没有重定向。你想重定向谁?标准的 http / rest 客户端在收到 404 状态码时不会遵循重定向。如果要重定向客户端,则必须使用 http 状态代码 3xx。请注意,您可能必须将客户端配置为自动跟踪重定向。
-
标准404确实不做重定向,所以我在web.xml中添加了
标签,规定当客户端得到404时,他会被重定向到404 .xhtml。但是如果他从 REST 中得到 404 就不行了……这就是我的问题。 -
您的应用程序既是
web.xml,也是基于注释的。您希望基于注释的部分(称为休息地址)在资源不可用的情况下返回您配置的 404 错误页面,这与重定向无关。那是对的吗?您能否以这种方式重新表述您的问题/问题描述?您的注释 (REST) 部分是否适用于现有资源? -
是的,你是对的。所以我要编辑我的问题。是的,注解 (REST) 对现有资源正常工作。