我尝试为 CompletionException 编写自定义异常映射器,并在出现此异常时显示图像。这是我的映射器:
@Provider
public class CustomCompleteExceptionManager implements ExceptionMapper<CompletionException> {
@Override
public Response toResponse(CompletionException e) {
return Response
.status(Response.Status.BAD_REQUEST)
.entity(Paths.get("error.png").toFile())
.type("image/img")
.build();
}
当我的代码抛出 CompletionException
但浏览器不显示 error.png
时,会调用 toResponse 方法。我收到错误:
This site can’t be reached
The webpage at http://localhost:8080/cf/point might be temporarily down or it may have moved permanently to a new web address.
当我只将字符串消息写入 Response
时,它工作正常:
@Provider
public class CustomCompleteExceptionManager implements ExceptionMapper<CompletionException> {
@Override
public Response toResponse(CompletionException e) {
return Response
.status(Response.Status.BAD_REQUEST)
.entity(e.getMessage())
.build();
}
可能是什么问题?
最佳答案
响应中设置的媒体类型错误。
它应该是 .type("image/png")
而不是 image/img
。
这是我在本地测试时的代码。
return Response
.status(Response.Status.BAD_REQUEST)
.entity(Paths.get("/home","kishore","Pictures","Meme.png").toFile())
.type("image/png")
.build();
关于java - 在 Jersey 中显示图像 ExceptionMapper,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41779169/