Spring Boot - JSR 303 - The Right Way To Validate Objects

Поделиться
HTML-код
  • Опубликовано: 24 янв 2025

Комментарии • 84

  • @BoualiAli
    @BoualiAli  Год назад

    COUPON Code: *EARLYBIRD20* => Spring Data J PA course: aliboucoding.com/p/the-full-guide-to-master-spring-boot-data-jpa

  • @dotherap
    @dotherap Год назад +1

    Couldn't find anything better about manual validation in Spring without using @Valid. Cheers!

  • @cdev9116
    @cdev9116 Год назад +3

    Great video ! your content is very useful. Thank you!
    Just to mention:
    You can make generic just the 'validate' method and not the full ObjectValidator class
    `public void validate (T t){...}`
    This way you can validate different types of objects with a single validator instance

    • @BoualiAli
      @BoualiAli  Год назад +1

      Totally true.
      Thank you for hi-lighting this

  • @lts8683
    @lts8683 2 года назад +3

    I think the second way to make method validate generic is to change the type of the argument to type Object.
    Thank you very much, you always create new content that is new and different.

    • @BoualiAli
      @BoualiAli  2 года назад +3

      Thank you for your feedback
      Objects are not required (generally speaking) because you need to cast them each time you need to access custom dat

  • @earlpadron6601
    @earlpadron6601 10 месяцев назад

    Very helpful thanks for helping me understand custom validation!

    • @BoualiAli
      @BoualiAli  9 месяцев назад

      Happy to help!

  • @mona9741
    @mona9741 10 месяцев назад

    so happy i found this channel

  • @ramabookstroremalang9131
    @ramabookstroremalang9131 2 года назад +1

    Thank you, Ya Abu Ali.

  • @johnarnett1438
    @johnarnett1438 8 месяцев назад +1

    Great video - clearly explained as ever. Did you create the the follow-up video btw, as promised at the end?

    • @BoualiAli
      @BoualiAli  8 месяцев назад

      I did!
      check the playlists

    • @debendradey8797
      @debendradey8797 7 месяцев назад

      @@BoualiAli I could not find the link for the follow up video, would you please share the link here ?

  • @pranaypaul6361
    @pranaypaul6361 6 месяцев назад

    Great basic core validation concept explained!! where's the next part? and is it still the best practice in industry or something better has come up by now? Thank you.

  • @drakezen
    @drakezen 2 года назад

    Very well explained I must say.

    • @BoualiAli
      @BoualiAli  2 года назад

      Thank you for your feedback

  • @alexv1571
    @alexv1571 4 месяца назад

    Cool, it's amazing.

    • @BoualiAli
      @BoualiAli  4 месяца назад

      Thank you! Cheers!

  • @Sasikumar-kr7xy
    @Sasikumar-kr7xy Год назад

    Great content. Really useful.

  • @gouravsharma-lo4ot
    @gouravsharma-lo4ot 5 месяцев назад

    @BoualiAli in the end you said in the next video you will be showing how we can even remove the validator check in the service layer, but i was not able to find the next video
    can i have the link ?

    • @BoualiAli
      @BoualiAli  5 месяцев назад +1

      Check the Full spring boot course (8 hours) and you will have your answer there

  • @tradeinfo3897
    @tradeinfo3897 Год назад +1

    can anyone help me for validate xml request body against xsd in spring boot application

    • @BoualiAli
      @BoualiAli  Год назад

      Post your issue in the discord server and you will get help there

  • @nikhilraghav9910
    @nikhilraghav9910 7 месяцев назад

    Kindly provide how we can configure error messages from application.properties file. I have tried many approaches but no luck.

  • @shakamuhammed533
    @shakamuhammed533 Год назад

    Hello, can you create a video on Unit Testing mainly on integration testing for spring boot!!
    Thanks

    • @BoualiAli
      @BoualiAli  Год назад

      I already have one, check the playlist

  • @tomaszw6449
    @tomaszw6449 Год назад

    Good stuff bro:)

  • @debkr
    @debkr 6 месяцев назад

    Your response sends back a 2xx http status on validation violations which is not good.

  • @spring8355
    @spring8355 2 года назад

    Thank you 🥰

  • @pritikumari-n7o1h
    @pritikumari-n7o1h Год назад

    Hi Ali, I have one doubts when u should use spring boot starter validation or hibernate validator? can you explain to me?

    • @BoualiAli
      @BoualiAli  9 месяцев назад

      check the ultimate guide and you will find your answer there

  • @MrSMSASS
    @MrSMSASS 2 года назад

    Awesome job!! Thank you! Continuation is hard to find :(

    • @BoualiAli
      @BoualiAli  2 года назад +1

      Check the playlist.

    • @MrSMSASS
      @MrSMSASS 2 года назад +1

      @@BoualiAli Thank you! it’s in exception handling

  • @ArunKumar-tr7ql
    @ArunKumar-tr7ql 2 года назад +1

    May i know the release date of continuation.

    • @BoualiAli
      @BoualiAli  2 года назад

      Check the playlist for all the videos

  • @emersontavera9362
    @emersontavera9362 Год назад

    Hello, thanks for your videos, they are really helpful and insightful, but I have a question:
    I found on the documention that there is an interface 'validator' that we can implement and override the support() and validate() methods.
    In the controller class, we can annotate one method with the @InitBinder annotation and add the validator using the add method from the WebDataBinder which is injected as a parameter to the method.
    Now if an object needs to be validated using the @Valid annotation, the WebDataBinder is going to look for an implementation that supports the type of data in question, if exists, performs validation and the errors are loading in the BindingResult parameter which is accesible in the endpoint that requested the validation
    After this, we can handle the error and return a responseEntity to the client
    But my question is basically comparing your approach to the one I found in the documentation, which way is more commonly found in production code? I request the validation in the controller layer, but you do it in the service layer
    I've been learning spring for 2 months, and the whole thing is about to make click in my head :3 feels good, but also raises many questions xd thanks in advance, sorry for the long comment

    • @BoualiAli
      @BoualiAli  Год назад

      Hello, thanks for the nice feedback and happy that you learn from my channel.
      Actually there are many ways to validate objects including the one mentioned by implementing the interface your own.
      Also you can just use the @Valid annotation on the object you want to validate in the controller and the catch the exception that will be thrown and return a proper response to the user.
      You can also follow my way of validation if you have for example extra logic that you need to implement while / after validating your objects.
      In production (based on my experience) I saw all of them tbh, so it really hard to tell which one is the most used in production. So it always depends on the use cases of your application / team
      I hope this was helpful

  • @michaelcapone8913
    @michaelcapone8913 Год назад

    I don't see the follow up of this video in the playlist :(

  • @erichhc9698
    @erichhc9698 2 года назад

    Excellent explanation Boali ali 😎😎
    About that validation that I didn't know (Y), I have a query. Is it also included as a good practice to use controllerAdice? to handle validations ?
    * Greetings from Peru

    • @BoualiAli
      @BoualiAli  2 года назад +2

      The answer is coming next week 😁😁
      Join the discord and exchange with people

  • @andilesimelane4771
    @andilesimelane4771 Год назад

    Someone pls help i m not able to import javax.Validation.Validation

  • @zqgAFf
    @zqgAFf Год назад

    Hi I never approach this type of content before , so I wonder does it work as well in springboot 3.0 or above ? or we use different strategy to do this validation

    • @BoualiAli
      @BoualiAli  Год назад +1

      It works the same with spring 3

    • @zqgAFf
      @zqgAFf Год назад

      @@BoualiAli that will be awesome , will there be a follow-up video to do the validation which is commonly used in production ? I checked your other video ,it seems that so far it hasn't come out yet

    • @BoualiAli
      @BoualiAli  Год назад

      @@zqgAFf can you explain me what you mean by used in production?

    • @zqgAFf
      @zqgAFf Год назад

      @@BoualiAli no worry thank u so much for the effort I hope I can see the role based version I just saw the refresh token version

    • @BoualiAli
      @BoualiAli  Год назад

      @@zqgAFf I will create a video for that.
      It will published soon

  • @AA-nu7ht
    @AA-nu7ht Год назад

    Hello Ali, as always thank you for the content you're providing us. I just have one point which is a bit blurry to me. Is Greeting the entity or the DTO? Because I heard it's best to work on the DTOs not the Entities since those are the objects we'll recieve into our post method. Right?
    Please enlight me and Ramadan Moubarak

    • @BoualiAli
      @BoualiAli  Год назад +1

      Ramadan Kareem.
      Greeting is the DTO, you should validate the DTO

    • @AA-nu7ht
      @AA-nu7ht Год назад

      @@BoualiAli yes I just figured it out by rewatching the video 😊 I will now watch the second video

    • @BoualiAli
      @BoualiAli  Год назад

      @@AA-nu7ht great 👍

  • @vageeshanvageesh5583
    @vageeshanvageesh5583 2 года назад

    Can’t we use Restcontrolleradvice?

    • @BoualiAli
      @BoualiAli  2 года назад

      Coming in few days
      Working on it

    • @vageeshanvageesh5583
      @vageeshanvageesh5583 2 года назад

      @@BoualiAli thanks man. Before spring boot 3.0, We were extending from ResponseEntityExceptionHandler and override its one the method (ResponseEntity handleMethodArgumentNotValid) this method handles the beans validation errors in globally. We don’t need to handle for every Java beans. It’s kind of generic one. But after spring boot 3.0 this method no longer there.
      @Override
      protected ResponseEntity handleMethodArgumentNotValid(MethodArgumentNotValidException ex, HttpHeaders headers, HttpStatus status, WebRequest request) {
      Map errors = new HashMap();
      ex.getBindingResult().getAllErrors().forEach((objectError) -> {
      String fieldError = ((FieldError) objectError).getField();
      String message = objectError.getDefaultMessage();
      errors.put(fieldError, message);
      });
      return new ResponseEntity(errors, HttpStatus.BAD_REQUEST);
      }
      Is any alternative for this? We are waiting from you!!!

    • @BoualiAli
      @BoualiAli  2 года назад +1

      @@vageeshanvageesh5583 it much easier than that.
      You will see in few days

    • @vageeshanvageesh5583
      @vageeshanvageesh5583 2 года назад

      @@BoualiAli Thanks dude, We are waiting..

  • @xk7zn
    @xk7zn Год назад

    Awesome course Sir ,well explained baraka allaho fik, can you provide source code please ?

    • @BoualiAli
      @BoualiAli  Год назад

      Happy you liked it.
      The source code is in my github profile

  • @jovaniecabatuan6364
    @jovaniecabatuan6364 Год назад

    where is the part 2 sir ?

  • @michaelcapone8913
    @michaelcapone8913 Год назад +1

    Ok, for everyone looking for the next video that improve the code of this one: ruclips.net/video/MhZl4YikM20/видео.html

  • @louaykhammar7268
    @louaykhammar7268 8 месяцев назад

    top

  • @sweets7092
    @sweets7092 2 года назад

    Kinda "fat" way. You can simply create an exception holder class with @ControllerAdvice annotation and extend ResponseEntityExceptionHandler.

    • @BoualiAli
      @BoualiAli  2 года назад

      Thanks for your comment.
      Just check the playlist and you will get your answer 😇

    • @sweets7092
      @sweets7092 2 года назад

      @@BoualiAli nice, I just noticed you have a video about that, added 3 weeks ago. Great 🤩

    • @princessazula8897
      @princessazula8897 Год назад

      @@sweets7092 which video please ? I can't find it.

    • @sweets7092
      @sweets7092 Год назад

      @@princessazula8897 Search for "Spring Boot Exception Handling Made Easy".

  • @darogajee3286
    @darogajee3286 Месяц назад

    Atleast tell from start .. whats in dto , dao , model pkgs .. how a beginner will learn you..bewakoof

  • @michaellebediev6715
    @michaellebediev6715 Год назад

    I suppose @NotEmpty includes @NotNull already