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 8953125
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T14:01:54+00:00 2026-06-15T14:01:54+00:00

I seek the best way to handle error validation in the @service layer in

  • 0

I seek the best way to handle error validation in the @service layer in a spring mvc application.

I have a @Controller and a @Service class.

My controller :

public String saveProduct(Product product) {
    myService.saveProduct(product);
    return "view";
}

My service :

public void saveProduct(Product product) {
    // some validation here
    myDao.save(product);
}

Now let’s say that the validation leads to 2 or 3 errors (the color is not appropriate, the supplier can’t deliver the product right now, and so on…) and I have to display all of them at the same time.

How do I transmit those errors to my controller, and then to my jsp ? Using an “Errors” object ? An exception containing all those errors ?

I’ve already looked here : Passing errors back to the view from the service layer but I can’t find a convenient answer. Do I really have to create unchecked exceptions for EACH error ? And what if I have a 20 or 30 errors ?

  • 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-15T14:01:56+00:00Added an answer on June 15, 2026 at 2:01 pm

    Finally I created a custom exception to handle the functional errors :

    public class ServiceException extends Exception {  
        private List<String> errors = new ArrayList<>();
        (...)
    }
    

    I also created a ValidationErrorsHelper class with the following method :

    public static void rejectErrors(final BindingResult result, final List<String> errors) {
        if (errors != null) {
            for (final String error : errors) {
                result.reject(error);
            }
        }
    }
    

    And in my controller class, I catch the exception :

    try {
        this.myService.myFunction(...);
    } catch (final ServiceException e) {
        ValidationErrorsHelper.rejectErrors(result, e.getErrors());
    }
    

    This is the best way I could find. If there’s any better suggestion I’m all ears !

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

Sidebar

Related Questions

I am using MFC CFile Seek function. I have a problem about Seek out
What is the best way to verify/test that a text string is serialized to
I'm trying to figure out what would be the best way to deal with
What is the best way to check that all numbers in the array (or
I have an application that logs information to a daily text file every second
I have an application that works with physical memory snapshots (for example, VMware VMEM
What is the best way to convert the C# object to XmlEmenet ? Do
In my Grails application the relationship between SecUser (Spring Security plugin is used) and
I'am a c# developper and I seek the best language to choose to be
I have a class that is wrapping a stream, with the intention of that

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.