Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public Entity getEntityWithOptions(@PathParam("id") String id, @QueryParam("option") String optValue) {
- if (optValue != null) {
- // Option is an enum
- try {
- Option option = Option.valueOf(optValue);
- } catch (IllegalArgumentException e) {
- throw new BadRequestException(e.getMessage());
- }
- return new Entity(option);
- }
- return new Entity();
- }
- public class CustomException extends RuntimeException {
- public CustomException(Throwable throwable) {
- super(throwable);
- }
- public CustomException(String string, Throwable throwable) {
- super(string, throwable);
- }
- public CustomException(String string) {
- super(string);
- }
- public CustomException() {
- super();
- }
- }
- @Provider
- public class CustomExceptionMapper implements ExceptionMapper<CustomException> {
- private static Logger logger = Logger.getLogger(CustomExceptionMapper.class.getName());
- /**
- * This constructor is invoked when exception is thrown, after
- * resource-method has been invoked. Using @provider.
- */
- public CustomExceptionMapper() {
- super();
- }
- /**
- * When exception is thrown by the jersey container.This method is invoked
- */
- public Response toResponse(CustomException ex) {
- logger.log(Level.SEVERE, ex.getMessage(), ex);
- Response.ResponseBuilder resp = Response.status(Response.Status.BAD_REQUEST)
- .entity(ex.getMessage());
- return resp.build();
- }
- }
- public Entity getEntityWithOptions(@PathParam("id") String id,
- @QueryParam("option") String optValue)
- throws CustomException {
- if (optValue != null) {
- // Option is an enum
- try {
- Option option = Option.valueOf(optValue);
- } catch (IllegalArgumentException e) {
- throw new CustomException(e.getMessage(),e);
- }
- return new Entity(option);
- }
- return new Entity();
- }
- Response.ResponseBuilder resp_builder=Response.status(Response.Status.BAD_REQUEST);
- resp_builder.entity(e.getMessage());//message you need as the body
- throw new WebApplicationException(resp_builder.build());
- public class CustomBadReq extends WebApplicationException {
- public CustomBadReq(String message) {
- super(Response.status(Response.Status.BAD_REQUEST)
- .entity(message).type(MediaType.TEXT_PLAIN).build());
- }
- }
- String msg = e.getMessage();
- throw new BadRequestException(Response.status(BAD_REQUEST)
- .entity(msg).build());
Add Comment
Please, Sign In to add comment