Spring annotations messages encoding

1.4k Views Asked by At

How do I configure SpringMVC's annotation messages encoding? For example, I annotated a field of the form to be validated like this:

@NotEmpty(message = "ъ")

But when the error occurs, it is encoded in a wrong charset. Encoding filter in web.xml is enabled and *.java files are UTF-8. Tomcat's Connector in server.xml is configured to deal with UTF-8.

1

There are 1 best solutions below

0
On

If you know the original charset, you can try to convert the original string to UTF. For example:

public static String convertFromISO88591ToUTF8( String original )
{
    String converted = original;
    try
    {
        byte[] iso88591Bytes = original.getBytes( "ISO-8859-1" );
        converted = new String( iso88591Bytes, "UTF8" );
    }
    catch ( UnsupportedEncodingException e )
    {
        e.printStackTrace();
    }
    return converted;
}

Actually, I've been using this same method (without changes) on the back-end even regardless the encoding on the front-end and it has been working fine. However, I didn't make a systematic test for all encoding types.

I hope it helps.