-1

I have a Spring application with a JPA Repository. Now I'd like to add some validations. I found several solutions but none works perfect and I don't know which methode should be used in which case:

1.) Im using validation annotations (e.g. @NotNull) in my model object. But this generates a not usefull rest error response like ""Internal Server Error","message":"Could not commit JPA transaction; "

2.) I'm using the 'Validator' interface for custom validations but I get the spring "compiler" error "Validator has incorrect spelling"

3.) Some tutorials use 'ConstraintValidator' interface

4.) Some tutorials write custom rest methods for validations.

When should I use which and how can I solve my problems?

Franken
  • 419
  • 1
  • 4
  • 14
  • What exactly are your problems? You should choose the one that is the least bad for you. – Aulis Ronkainen Oct 05 '18 at 13:40
  • yep, choose one which is suitable for you. Just like choose framework/tool which you love to work – Huy Nguyen Oct 05 '18 at 13:42
  • In general your object will pass multiple validations. If you have a rest endpoint, you should have validation before calling the repository save method and return a HTTP response. This validation should be doubled by the constraints, to make sure that that is consistent in the database. No worries, in a web application in general you have an additional validation on client side. – Adina Rolea Oct 05 '18 at 14:13

1 Answers1

1

This is how you can manage it and easy is:

  1. Define all your custom message in /messages/messages.properties under resources folder.

    so if you error property is: error.user.name = User name can not be null.

  2. then call your specific property in your pojo for that property.

     @NotNull(message = "error.user.name")
    
Community
  • 1
  • 1
kj007
  • 6,073
  • 4
  • 29
  • 47