How to catch a lot of Jackson Exceptions in Spring?












0















I have a Spring Boot REST application that uses ControllerAdvice and ExceptionHandlers. I'm using Jackson as my serialization/deserialization. I'm using PostMan as my client and when I send in different errors such as invalid inputs, bad JSON syntax etc... Jackson throws certain exceptions. Currently, I have an (1) ExceptionHandler that explicitly states each type of exception such as MismatchedInputException, InvalidFormatException, InvalidDentinitionException...these are all forms of JsonProcsessingException.



Is there a way to just catch JsonProcessingException and all its children? I return different messages/status codes depending on the types of exceptions. So if exception related to serialization is thrown I want a certain error message sent back.










share|improve this question

























  • Have you ever caught one exception, which mentioned above? from my understanding spring will wrap it with some spring specific exceptions. If you only want to catch exception and its children, then you just catch the parent exception and you will be able to catch all of them.

    – Jaiwo99
    Nov 14 '18 at 14:24











  • Can you show your code?

    – user7294900
    Nov 14 '18 at 14:29
















0















I have a Spring Boot REST application that uses ControllerAdvice and ExceptionHandlers. I'm using Jackson as my serialization/deserialization. I'm using PostMan as my client and when I send in different errors such as invalid inputs, bad JSON syntax etc... Jackson throws certain exceptions. Currently, I have an (1) ExceptionHandler that explicitly states each type of exception such as MismatchedInputException, InvalidFormatException, InvalidDentinitionException...these are all forms of JsonProcsessingException.



Is there a way to just catch JsonProcessingException and all its children? I return different messages/status codes depending on the types of exceptions. So if exception related to serialization is thrown I want a certain error message sent back.










share|improve this question

























  • Have you ever caught one exception, which mentioned above? from my understanding spring will wrap it with some spring specific exceptions. If you only want to catch exception and its children, then you just catch the parent exception and you will be able to catch all of them.

    – Jaiwo99
    Nov 14 '18 at 14:24











  • Can you show your code?

    – user7294900
    Nov 14 '18 at 14:29














0












0








0








I have a Spring Boot REST application that uses ControllerAdvice and ExceptionHandlers. I'm using Jackson as my serialization/deserialization. I'm using PostMan as my client and when I send in different errors such as invalid inputs, bad JSON syntax etc... Jackson throws certain exceptions. Currently, I have an (1) ExceptionHandler that explicitly states each type of exception such as MismatchedInputException, InvalidFormatException, InvalidDentinitionException...these are all forms of JsonProcsessingException.



Is there a way to just catch JsonProcessingException and all its children? I return different messages/status codes depending on the types of exceptions. So if exception related to serialization is thrown I want a certain error message sent back.










share|improve this question
















I have a Spring Boot REST application that uses ControllerAdvice and ExceptionHandlers. I'm using Jackson as my serialization/deserialization. I'm using PostMan as my client and when I send in different errors such as invalid inputs, bad JSON syntax etc... Jackson throws certain exceptions. Currently, I have an (1) ExceptionHandler that explicitly states each type of exception such as MismatchedInputException, InvalidFormatException, InvalidDentinitionException...these are all forms of JsonProcsessingException.



Is there a way to just catch JsonProcessingException and all its children? I return different messages/status codes depending on the types of exceptions. So if exception related to serialization is thrown I want a certain error message sent back.







spring-mvc spring-boot jackson






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 14 '18 at 17:06









Thomas Fritsch

5,351122133




5,351122133










asked Nov 14 '18 at 14:16









Dixon IveyDixon Ivey

194




194













  • Have you ever caught one exception, which mentioned above? from my understanding spring will wrap it with some spring specific exceptions. If you only want to catch exception and its children, then you just catch the parent exception and you will be able to catch all of them.

    – Jaiwo99
    Nov 14 '18 at 14:24











  • Can you show your code?

    – user7294900
    Nov 14 '18 at 14:29



















  • Have you ever caught one exception, which mentioned above? from my understanding spring will wrap it with some spring specific exceptions. If you only want to catch exception and its children, then you just catch the parent exception and you will be able to catch all of them.

    – Jaiwo99
    Nov 14 '18 at 14:24











  • Can you show your code?

    – user7294900
    Nov 14 '18 at 14:29

















Have you ever caught one exception, which mentioned above? from my understanding spring will wrap it with some spring specific exceptions. If you only want to catch exception and its children, then you just catch the parent exception and you will be able to catch all of them.

– Jaiwo99
Nov 14 '18 at 14:24





Have you ever caught one exception, which mentioned above? from my understanding spring will wrap it with some spring specific exceptions. If you only want to catch exception and its children, then you just catch the parent exception and you will be able to catch all of them.

– Jaiwo99
Nov 14 '18 at 14:24













Can you show your code?

– user7294900
Nov 14 '18 at 14:29





Can you show your code?

– user7294900
Nov 14 '18 at 14:29












1 Answer
1






active

oldest

votes


















0














You should create this method in @ControllerAdvice class and verify what exceptions you want to manage in order to return different messages/status codes .



@ExceptionHandler({InvalidFormatException.class, MismatchedInputException.class})
public void handlerIllegalArgumentException(JsonProcessingException exception,
ServletWebRequest webRequest) throws IOException {
if(exception instanceof InvalidFormatException) {
LOGGER.error(exception.getMessage(), exception);
webRequest.getResponse().sendError(HttpStatus.CONFLICT.value(), exception.getMessage());
} else if (exception instanceof MismatchedInputException) {
LOGGER.error(exception.getMessage(), exception);
webRequest.getResponse().sendError(HttpStatus.BAD_REQUEST.value(), exception.getMessage());
}
}





share|improve this answer

























    Your Answer






    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "1"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    StackExchange.ready(
    function () {
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53302286%2fhow-to-catch-a-lot-of-jackson-exceptions-in-spring%23new-answer', 'question_page');
    }
    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    0














    You should create this method in @ControllerAdvice class and verify what exceptions you want to manage in order to return different messages/status codes .



    @ExceptionHandler({InvalidFormatException.class, MismatchedInputException.class})
    public void handlerIllegalArgumentException(JsonProcessingException exception,
    ServletWebRequest webRequest) throws IOException {
    if(exception instanceof InvalidFormatException) {
    LOGGER.error(exception.getMessage(), exception);
    webRequest.getResponse().sendError(HttpStatus.CONFLICT.value(), exception.getMessage());
    } else if (exception instanceof MismatchedInputException) {
    LOGGER.error(exception.getMessage(), exception);
    webRequest.getResponse().sendError(HttpStatus.BAD_REQUEST.value(), exception.getMessage());
    }
    }





    share|improve this answer






























      0














      You should create this method in @ControllerAdvice class and verify what exceptions you want to manage in order to return different messages/status codes .



      @ExceptionHandler({InvalidFormatException.class, MismatchedInputException.class})
      public void handlerIllegalArgumentException(JsonProcessingException exception,
      ServletWebRequest webRequest) throws IOException {
      if(exception instanceof InvalidFormatException) {
      LOGGER.error(exception.getMessage(), exception);
      webRequest.getResponse().sendError(HttpStatus.CONFLICT.value(), exception.getMessage());
      } else if (exception instanceof MismatchedInputException) {
      LOGGER.error(exception.getMessage(), exception);
      webRequest.getResponse().sendError(HttpStatus.BAD_REQUEST.value(), exception.getMessage());
      }
      }





      share|improve this answer




























        0












        0








        0







        You should create this method in @ControllerAdvice class and verify what exceptions you want to manage in order to return different messages/status codes .



        @ExceptionHandler({InvalidFormatException.class, MismatchedInputException.class})
        public void handlerIllegalArgumentException(JsonProcessingException exception,
        ServletWebRequest webRequest) throws IOException {
        if(exception instanceof InvalidFormatException) {
        LOGGER.error(exception.getMessage(), exception);
        webRequest.getResponse().sendError(HttpStatus.CONFLICT.value(), exception.getMessage());
        } else if (exception instanceof MismatchedInputException) {
        LOGGER.error(exception.getMessage(), exception);
        webRequest.getResponse().sendError(HttpStatus.BAD_REQUEST.value(), exception.getMessage());
        }
        }





        share|improve this answer















        You should create this method in @ControllerAdvice class and verify what exceptions you want to manage in order to return different messages/status codes .



        @ExceptionHandler({InvalidFormatException.class, MismatchedInputException.class})
        public void handlerIllegalArgumentException(JsonProcessingException exception,
        ServletWebRequest webRequest) throws IOException {
        if(exception instanceof InvalidFormatException) {
        LOGGER.error(exception.getMessage(), exception);
        webRequest.getResponse().sendError(HttpStatus.CONFLICT.value(), exception.getMessage());
        } else if (exception instanceof MismatchedInputException) {
        LOGGER.error(exception.getMessage(), exception);
        webRequest.getResponse().sendError(HttpStatus.BAD_REQUEST.value(), exception.getMessage());
        }
        }






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Nov 15 '18 at 2:31

























        answered Nov 14 '18 at 14:50









        Jonathan JohxJonathan Johx

        1,7761317




        1,7761317
































            draft saved

            draft discarded




















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid



            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.


            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function () {
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53302286%2fhow-to-catch-a-lot-of-jackson-exceptions-in-spring%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            The Sandy Post

            Danny Elfman

            Pages that link to "Head v. Amoskeag Manufacturing Co."