Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8825539
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T06:54:12+00:00 2026-06-14T06:54:12+00:00

I am writing a REST application using Spring. It is divided into 3 layers,

  • 0

I am writing a REST application using Spring. It is divided into 3 layers, controller, service and repository.

The repository layer are using Spring data and MongoDb. The exceptions from MongoDb are translated to Spring Data exceptions.

Using the @ExceptionHandler annotation I tried to catch those exceptions in the repository layer. This did not work. The only way to catch the exception with @ExceptionHandler is to put the code in the controller layer.

Is there a way for me to catch and handle the exceptions in the repository layer,, without using try/catch blocks.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T06:54:13+00:00Added an answer on June 14, 2026 at 6:54 am

    It could be done with Spring AOP and by creating an @Around advice for all your DAO methods as shown below.

    But I would still like to understand what you plan to do in your catch block. Are you planning to have different logic to handle different types of data access exceptions? If you don’t have any specific logic, it makes sense to just let the exception propagate to the controller layer.

    First Option

    Here is a sample –

        @Aspect
    public class DaoExceptionHandlerAdvice {
        @Around("execution( * com.xyz.daos.*.*(..))")
        public Object invokeService(ProceedingJoinPoint pjp) throws Throwable{
            MethodSignature methodSignature = (MethodSignature)pjp.getSignature();
            Object returnValue = null;
            try {
                returnValue = pjp.proceed();
            } 
            catch(Exception e){
                // handle the exception 
            }
            finally{
            }
            return returnValue;
        }
    }
    

    Add following snippet in your application context file

    <aop:aspectj-autoproxy />    
    <bean id="daoExceptionHandler" class="com.xyz.advice.DaoExceptionHandlerAdvice"  ></bean>
    

    Check out the following link for details –
    Spring AOP

    Second Option

    I’ve not tried this, but it would probably be easier for you to use an exception translator. You could probably extend HibernateExceptionTranslator and have your own logic in there.

    Follow this link for details –
    Exception Translation

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing an app to connect to a third party application using REST
I'm writing a WPF test application against a WCF REST service running on Azure
I want to test my REST-Service using the RestEasy Client Framework. In my application
I'm writing an iPhone Application using the ASIHTTPRequest (http://allseeing-i.com/ASIHTTPRequest/) library for REST interactions to
I'm writing web application that uses Spring MVC to bind Spring beans with REST-like
I am writing an application that is consuming an in-house WCF-based REST service and
I'm writing an application that will migrate a JBoss Guvnor packages using its REST
I'm writing an application using PHP and the Jira REST API which is required
I am writing a WCF REST service, and I am needing to generate my
I'm writing an API in Cake2.1.3 using the REST instructions in the docs. Having

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.