2017-01-21 40 views
0

我試圖爲CompletionException編寫自定義異常映射器,並在這種情況下顯示圖像。這是我的映射:當我的代碼拋出CompletionException在澤西島中顯示圖像ExceptionMapper

@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(); 
    } 

方法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. 

當我寫的只是字符串消息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(); 
    } 

可能是什麼問題呢?

回答

1

在響應中設置的媒體類型錯誤。
它應該是.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();