如何自定义处理 Quarkus 中未找到的资源/URL? [英] How to customly handle Recourse/URLs Not Found in Quarkus?
本文介绍了如何自定义处理 Quarkus 中未找到的资源/URL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我对 Quarkus 非常陌生,我想知道如何覆盖提供错误日志的默认 404 页面,或者如何巧妙地将所有无法识别的 URL 重定向到 META-INF/recourses 目录中的自定义 HTML.
I'm very new to Quarkus and I will like to know how I can override default 404 page which provides the error logs or how I can neatly redirect all not recognized URLs to a custom HTML in the META-INF/recourses directory.
推荐答案
使用 quarkus 0.20+ 你可以像这样创建一个 ExceptionMapper:
Using quarkus 0.20+ you can create an ExceptionMapper like this:
import java.util.Scanner;
import javax.ws.rs.NotFoundException;
import javax.ws.rs.core.Response;
import javax.ws.rs.ext.ExceptionMapper;
import javax.ws.rs.ext.Provider;
/**
* NotFoundExepptionMapper
*/
@Provider
public class NotFoundExeptionMapper implements ExceptionMapper<NotFoundException> {
@Override
public Response toResponse(NotFoundException exception) {
String text = new Scanner(this.getClass().getResourceAsStream("/META-INF/resources/notfound.html"), "UTF-8").useDelimiter("\\A").next();
return Response.status(404).entity(text).build();
}
}
将页面保存在/META-INF/resources/notfound.html 上即可.
Save the page on /META-INF/resources/notfound.html and it's done.
这篇关于如何自定义处理 Quarkus 中未找到的资源/URL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文