ユーザーにログインするために 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();
}
}
私がスローしているエラーとマッパークラスで定義しているステータスコードに関係なく、サーバーは定義された409ではなく、常に500内部サーバーエラーで応答します。前もって感謝します。