在泽西ExceptionMapper中显示图像

时间:2017-01-21 12:10:15

标签: java rest jersey jax-rs

我尝试为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.

当我向Response编写String消息时,它可以正常工作:

@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 个答案:

答案 0 :(得分: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();