簡體   English   中英

在球衣ExceptionMapper中顯示圖像

[英]Show image in jersey ExceptionMapper

我試圖為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 ,將調用toResponse方法,但瀏覽器不會顯示error.png 我收到一個錯誤:

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.

當我只將String消息寫入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();

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM