我有以下問題:我創建了一個Java Web應用程序。另外,我製作了一些REST端點。在web.xml中,我使用標籤定義了404錯誤的重定向。它適用於除了不存在的REST端點以外的所有地址。我實現了RestApplication類:配置的404錯誤頁面在JAX-RS應用程序未顯示
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;
}
}
但它沒有工作。從web.xml中
<error-page>
<error-code>404</error-code>
<location>/WEB-INF/errorpages/404.xhtml</location>
</error-page>
:但是,它工作時,我刪除了。但不幸的是,當然重定向並沒有對非REST地址工作。
誰能幫助我解決這個問題,並建議,讓我提供重定向404錯誤的情況下REST和非REST地址的解決方案?先謝謝你!
編輯:
繼@andih的言論,對於REST服務,我想在一個資源不可用的情況下返回配置的404錯誤頁面。
你能詳細解釋一下你的期望嗎? HTTP狀態碼404意味着「未找到」。沒有重定向。你想重定向誰?標準的http/rest客戶端在獲得404狀態碼時不會遵循重定向。如果你想重定向客戶端,你必須使用http狀態碼3xx。請注意,您可能必須配置您的客戶端以自動遵循重定向。 – andih
的確,標準404不會做重定向,因此我將標籤添加到了web.xml中,該標籤規定當客戶端獲得404時,他將被重定向到404.xhtml。但是如果他從REST獲得404,那麼這是行不通的......這就是我的問題。 –
user2905035
您的應用程序既是'web.xml'也是基於註釋的。您希望基於註釋的部分 - 您稱爲休息地址 - 在資源不可用的情況下返回配置的404錯誤頁面,這與重定向無關。那是對的嗎?你能否以這種方式重新描述你的問題/問題描述?您的註釋(REST)部分是否適用於現有資源? – andih