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

  • Home
  • SEARCH
  • 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 61783
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T18:22:23+00:00 2026-05-10T18:22:23+00:00

I’m investigating an annotation-based approach to validating Spring beans using spring modules . In

  • 0

I’m investigating an annotation-based approach to validating Spring beans using spring modules. In this tutorial, the following bean (getters and setters omitted) is used as an example:

public final class User {      @NotBlank     @Length(max = 80)     private String name;      @NotBlank     @Email     @Length(max = 80)     private String email;      @NotBlank     @Length(max = 4000)     private String text;   } 

The error message that is used if a particular validation rule is disobeyed should follow this format:

bean-class.bean-propery[validation-rule]=Validation Error message 

Examples for the class shown above include:

User.email[not.blank]=Please enter your e-mail address.   User.email[email]=Please enter a valid e-mail address.   User.email[length]=Please enter no more than {2} characters. 

The fact that the message keys contain the class name presents a couple of problems:

  1. If the class is renamed, the message keys also need to be changed
  2. If I have another class (e.g. Person) with an email property that is validated identically to User.email, I need to duplicate the messages, e.g.

    Person.email[not.blank]=Please enter your e-mail address.
    Person.email[email]=Please enter a valid e-mail address.
    Person.email[length]=Please enter no more than {2} characters.

In fact, the documentation claims that is possible to configure a default message for a particular rule (e.g. @Email) like this:

email=email address is invalid 

This default message should be used if a bean-specific message for the rule cannot be found. However, my experience is that this simply does not work.

An alternative mechanism for avoiding duplicate messages is to pass the key of the error message to the rule annotation. For example, assume I have defined the following default error message for the @Email rule

badEmail=Email address is invalid 

This message should be used if I annotate the relevant property like this:

@Email(errorCode='badEmail') private String email; 

However I tried this, out and again, it just doesn’t seem to work. Has anyone found a way to avoid duplicating error messages when using this validation framework?

  • 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. 2026-05-10T18:22:24+00:00Added an answer on May 10, 2026 at 6:22 pm

    I took a quick look at the BeanValidator API, and it looks like you might want to try the errorCodeConverter property.

    You would need to implement your own ErrorCodeConverter, or use one of the provided implementations?

    .... <bean id='validator' class='org.springmodules.validation.bean.BeanValidator'     p:configurationLoader-ref='configurationLoader'     p:errorCodeConverter-ref='errorCodeConverter' />  <bean id='errorCodeConverter' class='contact.MyErrorCodeConverter' /> .... 

    Note: configurationLoader is another bean defined in the config XML used in the tutorial

    Example converter:

    package contact;  import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.springmodules.validation.bean.converter.ErrorCodeConverter;  public class MyErrorCodeConverter implements ErrorCodeConverter {      private Log log = LogFactory.getLog(MyErrorCodeConverter.class);      @Override     public String convertPropertyErrorCode(String errorCode, Class clazz, String property) {         log.error(String.format('Property %s %s %s', errorCode, clazz.getClass().getName(), property));         return errorCode;  // <------ use the errorCode only     }      @Override     public String convertGlobalErrorCode(String errorCode, Class clazz) {         log.error(String.format('Global %s %s', errorCode, clazz.getClass().getName()));         return errorCode;     } } 

    Now the properties should work:

    MyEmailErrorCode=Bad email  class Foo {     @Email(errorCode='MyEmailErrorCode')     String email } 
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 179k
  • Answers 179k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer see if this will help... Best way to hide a… May 12, 2026 at 3:50 pm
  • Editorial Team
    Editorial Team added an answer Does httpd -V tell anything on 1.3? (it does tell… May 12, 2026 at 3:50 pm
  • Editorial Team
    Editorial Team added an answer I'd stick with 1. When you have a part of… May 12, 2026 at 3:50 pm

Related Questions

I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out
I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
In order to apply a triggered animation to all ToolTip s in my app,
I have a French site that I want to parse, but am running into
I have text I am displaying in SIlverlight that is coming from a CMS

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.