Javax异常映射器仅抛出500

时间:2018-10-04 08:14:53

标签: java rest exception jax-rs exceptionmapper

我有一个使用javax实现的REST接口来登录用户。

@Path("/login")
@GET
@Produces(MediaType.APPLICATION_JSON)
public UserId login(@QueryParam("email") String email, @QueryParam("password") String password) throws InvalidUserDataException {
    User user;
    try {
        user = loadUserFromDatabase(null, email);
    } catch (MissingDatabaseEntryException e) {
        e.printStackTrace();
        throw  new InvalidUserDataException("Email and password do not match.");
    }
    if(!user.confirmed)
        throw new InvalidUserDataException("We have sent you a confirmation mail. Please confirm your email first.");
    if(!user.password.equals(password))
        throw  new InvalidUserDataException("Email and password do not match.");

    return new UserId(user.userId);
}

此外,还有一个Exception Mapper可以将抛出的异常映射到特定的HTTP响应。

@Provider
public class InvalidUserDataExceptionHandler implements ExceptionMapper<InvalidUserDataException> {
    @Override
    public Response toResponse(InvalidUserDataException exception)
    {
        return Response.status(Response.Status.CONFLICT).entity(exception.getMessage()).build();
    }
}

无论我抛出什么错误以及在mapper类中定义什么状态代码,服务器始终仅以500 Internal Server Error(内部服务器错误)而不是定义的409进行响应。有人知道我在这里缺少什么吗?预先感谢。

0 个答案:

没有答案
相关问题