• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Tim Cooke
  • Liutauras Vilda
  • Jeanne Boyarsky
Sheriffs:
  • Paul Clapham
  • Rob Spoor
  • Junilu Lacar
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Piet Souris
  • Carey Brown
Bartenders:

Spring custom formatter to field annotation does not work

 
Ranch Hand
Posts: 126
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hello!

I'm trying to apply formatter-annotation to the field "phone" in next model-class:



Interface for annotation:



Formatter:



Annotation-factory:



FormatterRegistrar:



config:




And it does not work: phone-values saved in database, but in non-formatted form.

Note: when I run test in debug with breakpoint on PhoneFormatter, test passes susseccfully. That means, that my Formatter is out of process. But when I set breakpoint on PhoneFormatAnnotationFormatterFactory.getFieldTypes, test interrupted.

In particular, debug with breakpoint on return Collections.singleton(String.class); in


shows that the class PhoneFormatAnnotationFormatterFactory has no fields...
 
Saloon Keeper
Posts: 14682
330
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Andrey Dmitriev wrote:And it does not work: phone-values saved in database, but in non-formatted form.


Well yes. You're not actually formatting anything. Your formate() method matches the annotated field, and then just returns the value of the annotated field. As a side note, if you intend to write your application in English, spell it 'format', not 'formate'.

Note: when I run test in debug with breakpoint on PhoneFormatter, test passes susseccfully. That means, that my Formatter is out of process. But when I set breakpoint on PhoneFormatAnnotationFormatterFactory.getFieldTypes, test interrupted.


How about you set a breakpoint on the first line of the print() method body?

In particular, debug with breakpoint on [...] shows that the class PhoneFormatAnnotationFormatterFactory has no fields...


What fields were you expecting? Your factory doesn't have fields.

~

I don't understand why you're using your annotation to parse String to String. The entire point of parsing is that you convert a string to a stronger more meaningful data type. If I were you, I'd make a class PhoneNumber and then let your PhoneNumberFormatter implement Formatter<PhoneNumber>.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
reply
    Bookmark Topic Watch Topic
  • New Topic