Post

Exception Handling With Jersey

Ref: Exception Handling With Jersey

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import TsMgr.response.RestErrorResponse;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;
import jakarta.ws.rs.ext.ExceptionMapper;

import static jakarta.ws.rs.core.Response.Status.NOT_ACCEPTABLE;

/**
 * 空值錯誤
 */
public class NullPointerExceptionMapper implements ExceptionMapper<NullPointerException> {
    @Override
    public Response toResponse(NullPointerException exception) {
        return Response.status(NOT_ACCEPTABLE)
            .entity(new RestErrorResponse("空值錯誤"))
            .type(MediaType.APPLICATION_JSON)
            .build();
    }
}
This post is licensed under CC BY 4.0 by the author.