Java 如何在Dropwizard中查看自定义404页面
有人能告诉我如何查看我的自定义404页面吗。我在谷歌上搜索并尝试实现Java 如何在Dropwizard中查看自定义404页面,java,jetty,http-status-code-404,dropwizard,embedded-jetty,Java,Jetty,Http Status Code 404,Dropwizard,Embedded Jetty,有人能告诉我如何查看我的自定义404页面吗。我在谷歌上搜索并尝试实现ExceptionMapper,但这并没有完全奏效。我使用的是0.8.1版本 我的异常映射程序: public class RuntimeExceptionMapper implements ExceptionMapper<NotFoundException> { @Override public Response toResponse(NotFoundException exception) {
ExceptionMapper
,但这并没有完全奏效。我使用的是0.8.1版本
我的异常映射程序:
public class RuntimeExceptionMapper implements ExceptionMapper<NotFoundException> {
@Override
public Response toResponse(NotFoundException exception) {
Response defaultResponse = Response.status(Status.OK)
.entity(JsonUtils.getErrorJson("default response"))
.build();
return defaultResponse;
}
}
公共类RuntimeExceptionMapper实现ExceptionMapper{
@凌驾
公共响应(NotFoundException){
Response defaultResponse=Response.status(status.OK)
.entity(JsonUtils.getErrorJson(“默认响应”))
.build();
返回默认响应;
}
}
这只适用于不正确的API,而不适用于资源调用
我的设置:
@Override
public void initialize(Bootstrap<WebConfiguration> bootstrap) {
bootstrap.addBundle(new AssetsBundle("/webapp", "/", "index.html"));
}
@Override
public void run(WebConfiguration configuration, Environment environment) throws Exception {
environment.jersey().register(RuntimeExceptionMapper.class);
((AbstractServerFactory) configuration.getServerFactory()).setJerseyRootPath("/api/*");
// Registering Resources
environment.jersey().register(new AuditResource(auditDao));
....
}
@覆盖
公共无效初始化(引导引导引导){
addBundle(新资产包(“/webapp”、“/”、“index.html”);
}
@凌驾
public void运行(网络配置配置、环境)引发异常{
environment.jersey().register(RuntimeExceptionMapper.class);
((AbstractServerFactory)configuration.getServerFactory()).setJerseyRootPath(“/api/*”);
//注册资源
register(newauditresource(auditDao));
....
}
现在,
http://localhost:8080/api/rubish
通过重写的ExceptionMapper方法
http://localhost:8080/rubish.html
生成默认的404页面
如何设置,以便在请求未知页面时,dropwizard将显示自定义404页面
我参考了异常映射器的链接如果我理解正确,您想要的是为不匹配的资源请求提供自定义404页面。为此,您可以编写一个单独的资源类,并在其中编写一个单独的资源方法。此资源方法应具有 @路径(“/{默认值:.*}”) 注释。此资源方法捕获不匹配的资源请求。在这种方法中,您可以提供自己的自定义视图 请看下面的代码片段,以了解其含义
@Path("/")
public class DefaultResource {
/**
* Default resource method which catches unmatched resource requests. A page not found view is
* returned.
*/
@Path("/{default: .*}")
@GET
public View defaultMethod() throws URISyntaxException {
// Return a page not found view.
ViewService viewService = new ViewService();
View pageNotFoundView = viewService.getPageNotFoundView();
return pageNotFoundView;
}
}
如果您不知道如何使用dropwizard或ask me为静态资产提供服务,请参阅。要为任何错误配置自定义错误页,可以在应用程序中配置ErrorPageErrorHandler,如下所示:
@Override
public void run(final MonolithConfiguration config,
final Environment env) {
ErrorPageErrorHandler eph = new ErrorPageErrorHandler();
eph.addErrorPage(404, "/error/404");
env.getApplicationContext().setErrorHandler(eph);
}
@Path("/error")
public class ErrorResource {
@GET
@Path("404")
@Produces(MediaType.TEXT_HTML)
public Response error404() {
return Response.status(Response.Status.NOT_FOUND)
.entity("<html><body>Error 404 requesting resource.</body></html>")
.build();
}
然后创建一个如下所示的资源:
@Override
public void run(final MonolithConfiguration config,
final Environment env) {
ErrorPageErrorHandler eph = new ErrorPageErrorHandler();
eph.addErrorPage(404, "/error/404");
env.getApplicationContext().setErrorHandler(eph);
}
@Path("/error")
public class ErrorResource {
@GET
@Path("404")
@Produces(MediaType.TEXT_HTML)
public Response error404() {
return Response.status(Response.Status.NOT_FOUND)
.entity("<html><body>Error 404 requesting resource.</body></html>")
.build();
}
球衣层的url模式映射是否涵盖了
/rubish.html
?或者Jetty本身正在尝试从该路径提供内容?Jetty正在尝试使用defaultResource
imable-catchhttp://localhost:8080/api/rubish
。。。我想抓住http://localhost:8080/rubish.html
。。哪一个是html页面请求您是否有url模式为rubish.html的资源(或资源方法)??如果已经有一个注册的资源具有这个url模式rubish.html,那么默认资源(在我的回答中提到)将无法捕获该请求。只有那些未被任何其他资源捕获的请求才会被默认资源捕获。所有资源都以api
。。。html是对静态html页面的请求。它不是在Resources下注册的,而是返回我使用的HTML字符串,Response.seeOther(新URI(“/index.HTML”)).build()代码>谢谢:)你的评论提醒我,我还有一个问题。上面的代码确实会返回提供的html,但是响应状态将是200,而不是404!我会用正确的代码更新。