handling IOException in controller

In my controller i have the following code :
 @RequestMapping(value = "/snippets/{id}", method = RequestMethod.PUT)
    public void showSnippet(@RequestBody String jsonString, @PathVariable String id, HttpServletResponse response) {
        Snippet snippet = null;
        try {
            snippet = new ObjectMapper().readValue(jsonString, Snippet.class);
        } catch (IOException e) {


Open in new window

I feel probably this is not right way to handle exception directly inside the controller ?
Should i use @ExceptionHandler in a base controller for this and catching IOException directly ?
But then there could be multiple ways i can get IOException that may be needed to handled differently.

Rohit BajajAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

mccarlIT Business Systems Analyst / Software DeveloperCommented:
I really think it would be worthwhile getting the automatic JSON conversion using Jackson working, rather than manually doing this. That way your code will look MUCH cleaner and Spring will handle the above issue automatically for you.

You said in the other question where I mentioned this, that you didn't try getting the Jackson stuff working after fixing your other library issues. So can you try again now? For the record the above would look like this...

 @RequestMapping(value = "/snippets/{id}", method = RequestMethod.PUT, consumes = "application/json")
    public void updateSnippet(@RequestBody Snippet snippet, @PathVariable String id) {

Open in new window

Notice how clean that is? No dealing with IOExceptions, since Spring will handle it by returning a "400 Bad Request" response automatically if there are errors converting the JSON into the Snippet object. Also, I have removed the line setting the status code to 200 as that will already be the default.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today

From novice to tech pro — start learning today.