From 66f829cd8d4ef029c6b5275cf9bd82dfcfd74c82 Mon Sep 17 00:00:00 2001 From: Ulisses Lima Date: Mon, 18 Apr 2022 21:37:21 -0300 Subject: [PATCH] BAEL-5157 - Exception Handling With Jersey (#12082) * BAEL-5157 - Exception Handling with Jersey First draft: https://drafts.baeldung.com/wp-admin/post.php?post=131880&action=edit * BAEL-5157 * more meaningful exception messages --- .../jersey/exceptionhandling/rest/StocksResource.java | 2 +- .../rest/exceptions/ServerExceptionMapper.java | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/jersey/src/main/java/com/baeldung/jersey/exceptionhandling/rest/StocksResource.java b/jersey/src/main/java/com/baeldung/jersey/exceptionhandling/rest/StocksResource.java index 94ce329ad0..64b645a1c6 100644 --- a/jersey/src/main/java/com/baeldung/jersey/exceptionhandling/rest/StocksResource.java +++ b/jersey/src/main/java/com/baeldung/jersey/exceptionhandling/rest/StocksResource.java @@ -34,7 +34,7 @@ public class StocksResource { @Produces(MediaType.APPLICATION_JSON) public Response get(@PathParam("ticker") String id) { Optional stock = stocks.findById(id); - stock.orElseThrow(IllegalArgumentException::new); + stock.orElseThrow(() -> new IllegalArgumentException("ticker")); return Response.ok(stock.get()) .build(); diff --git a/jersey/src/main/java/com/baeldung/jersey/exceptionhandling/rest/exceptions/ServerExceptionMapper.java b/jersey/src/main/java/com/baeldung/jersey/exceptionhandling/rest/exceptions/ServerExceptionMapper.java index a6e9cc7f39..adfac000e8 100644 --- a/jersey/src/main/java/com/baeldung/jersey/exceptionhandling/rest/exceptions/ServerExceptionMapper.java +++ b/jersey/src/main/java/com/baeldung/jersey/exceptionhandling/rest/exceptions/ServerExceptionMapper.java @@ -7,7 +7,7 @@ import javax.ws.rs.core.Response.Status; import javax.ws.rs.ext.ExceptionMapper; public class ServerExceptionMapper implements ExceptionMapper { - public static final String HTTP_405_MESSAGE = "METHOD_NOT_ALLOWED"; + public static final String HTTP_405_MESSAGE = "use one of"; @Override public Response toResponse(final WebApplicationException exception) { @@ -18,7 +18,7 @@ public class ServerExceptionMapper implements ExceptionMapper