次の問題があります。私はJava Webアプリケーションを作成しました。さらに、いくつかのRESTエンドポイントを作成しました。 web.xmlでは、タグを使用して404エラーのリダイレクトを定義しました。これは、存在しないRESTエンドポイントを除くすべてのアドレスに対して正しく機能します。私はRestApplicationクラスを実装:JAX-RSアプリケーションに404エラーページが表示されない
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>
私も、つまり、私はEntityNotFoundExceptionMapperクラスを実装し、ExceptionMapperを使用しようとしました:
@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以外のアドレスでは機能しませんでした。
誰でもこの問題を助け、RESTアドレスと非RESTアドレスの両方に404エラーが発生した場合にリダイレクトを提供できるソリューションを提案できますか?前もって感謝します!
EDIT:@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