2017-06-09 30 views
1

我有以下問題:我創建了一個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錯誤頁面。

+0

你能詳細解釋一下你的期望嗎? HTTP狀態碼404意味着「未找到」。沒有重定向。你想重定向誰?標準的http/rest客戶端在獲得404狀態碼時不會遵循重定向。如果你想重定向客戶端,你必須使用http狀態碼3xx。請注意,您可能必須配置您的客戶端以自動遵循重定向。 – andih

+0

的確,標準404不會做重定向,因此我將標籤添加到了web.xml中,該標籤規定當客戶端獲得404時,他將被重定向到404.xhtml。但是如果他從REST獲得404,那麼這是行不通的......這就是我的問題。 – user2905035

+0

您的應用程序既是'web.xml'也是基於註釋的。您希望基於註釋的部分 - 您稱爲休息地址 - 在資源不可用的情況下返回配置的404錯誤頁面,這與重定向無關。那是對的嗎?你能否以這種方式重新描述你的問題/問題描述?您的註釋(REST)部分是否適用於現有資源? – andih

回答

0

不是很肯定,爲什麼你的其他資源在所有但對於web.xml和註釋複雜的部署找到工作,你就必須做多一點點。

您的RestApplication類必須擴展javax.ws.rs.core.Application以定義RESTful Web服務應用程序部署的組件。有關javax.ws.rs.core.Application更多詳細信息,請參閱http://download.oracle.com/javaee/6/api/javax/ws/rs/core/Application.html

的Javadoc在您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>將優先。

如果像「.../rest/hello」返回「Hello」那樣請求類似 「.../rest/foo」的資源,上述示例將返回配置的404錯誤頁面。

上面的例子用球衣2.26-b03和tomcat 8.5.15進行了測試。

相關問題