5 Common Mistakes Spring Developers Make

Поділитися
Вставка
  • Опубліковано 2 лют 2025

КОМЕНТАРІ • 121

  • @d2k2
    @d2k2 11 місяців тому +26

    thanks! writing interfaces, which are never used are waste of time. good to hear this from an experienced spring developer.

    • @FrankAn-io7bz
      @FrankAn-io7bz 7 місяців тому

      But using interfaces also hides the implementation details, right? The caller can use the functionality without caring about the details.

    • @Sterni1710isshowing
      @Sterni1710isshowing 5 місяців тому

      Yes, buuut: If your interface is exactly used by one class, ever, what difference does it make? You have one unecessary file, which clutters things and makes readability worse.

  • @hirokiminami4194
    @hirokiminami4194 11 місяців тому +8

    0:00 Intro
    1:27 Making everything public
    4:48 Field dependency injection
    7:19 Interface when it’s not needed
    10:33 Improper REST API design
    13:40 Improper exception handling

  • @petritoivari
    @petritoivari 11 місяців тому +9

    Can't wait for exception handling video. It has has always been bit of a mystery to me :) Or more like what is the proper way of doing it.

  • @kassemjenkins
    @kassemjenkins 11 місяців тому +11

    Please continue along these line of topics!

  • @ravimangalagiri
    @ravimangalagiri 11 місяців тому +2

    Thanks!

    • @DanVega
      @DanVega  11 місяців тому

      Wow thank you so much for your generosity. I appreciate you 🤩

    • @ravimangalagiri
      @ravimangalagiri 11 місяців тому

      @@DanVega np. what intellij theme did you use for the demo?

  • @tysonlawrie
    @tysonlawrie 7 місяців тому +2

    This was great. #2 and #3 are what was drilled into years ago and ive never questioned them.

  • @chaitanyashetty6745
    @chaitanyashetty6745 11 місяців тому +3

    Amazing Tutorial Dan! As someone who is exploring Spring, this was a great learning

  • @pacifiquemunzihirwa7054
    @pacifiquemunzihirwa7054 11 місяців тому +16

    Pretty good video.
    I am a pretty stubborn developper and I don't usually implement things that do not improve that much on my coding time or make my code easier to understand.
    I am with you for theses 3 :
    - REST API recommendations should be followed.
    - Using constructor based dependency injection is BAE
    - Custom exceptions should always be implemented. I usually use a Controller Advisor to map my exceptions to Http Responses so that i can still you the Java exceptions and then transform the message into the body of a 4xx error code depending on the exception.
    - Using interfaces when you really need them (50/50) -> if you are doing more than CRUD transactions interfaces are a good way to show what methods you have implemented instead of reading all the code to find out.
    Not really with you on this:
    - For the way to structure your code (you should chose package by layer or package by feature depending on the project)

    • @jmrah
      @jmrah 11 місяців тому

      I'll engage with you. Could you elaborate on your using interfaces point? What's the problem that an interface solves in your example? And why do you disagree with package by layer or package by feature? Do you usually organize your code another way?

  • @fene2899
    @fene2899 11 місяців тому

    You're right. I've never made a mistake, but now if I do, I'll check back here for help. Thanks man. You put out consistently helpful tech content. I recently inherited a Spring project and I have a lot to learn.

  • @ambarishkapil8004
    @ambarishkapil8004 8 місяців тому

    Thanks for creating this video Dan. Looking forward to more additions on the common mistakes series. Cheers!

  • @koladeam
    @koladeam 11 місяців тому +1

    as always, thanks for this Dan! You're helping beginners like me in a massive way.

  • @VerhoevenSimon
    @VerhoevenSimon 11 місяців тому +4

    Thank you for the interesting video, and number 6: not dropping by the Spring office hours.

    • @DanVega
      @DanVega  11 місяців тому +1

      Ohh that’s a good one haha 😂

  • @donaldf.coffin8130
    @donaldf.coffin8130 11 місяців тому +2

    Dan, thanks for sharing. I hope you will make this a series and add more "mistakes" to the repository and UA-cam

  • @jacks808
    @jacks808 11 місяців тому +1

    Very good advice for a novice like me. Thanks a lot!

  • @tobyzieglerrr
    @tobyzieglerrr 11 місяців тому

    Very good basic points made here. Clear and concise. Dan delivers 😊

  • @darkogrozdanovski
    @darkogrozdanovski 11 місяців тому +11

    It would be amazing to see something similar for spring data jpa

    • @JitterTed
      @JitterTed 11 місяців тому +1

      I think using JPA when you don't need the complexity is a mistake. 😄

  • @ewoks42
    @ewoks42 8 місяців тому

    Great tips @DanVega regarding handling errors video you mentioned at 13:40, is that one already published? tnx

  • @marz833
    @marz833 4 місяці тому

    Thanks for the great video. Please continue the posting about common mistakes and best practices. About the interfaces, some architectures (e.g. ports and adapters) require (recommend) the definition of interfaces though, even if they are implemented only once.

  • @Jedimaster36091
    @Jedimaster36091 10 місяців тому +1

    My opinion is that it is wrong for the TodoFiveRepository to throw an exception (even a custom one), but instead, return an Optional. Reason being that it is not the repository's responsibility to determine whether this is an exceptional case or an expected scenario. Instead, it is the caller of the repository (direct or indirect) to figure out that, depending upon the scenario. Exceptions are to be used for exceptional cases, not for expected functional behavior. The repository should only throw an exception when there's a problem accessing the database.

    • @randygeyer6325
      @randygeyer6325 4 місяці тому

      Definitely! Use Optional instead of throwing exception for missing entity.

  • @Skiamakhos
    @Skiamakhos 11 місяців тому

    I've been guilty of most of these because that's how I learned from other developers to do it. You've given me some fuel for thought - thanks Dan!

    • @DanVega
      @DanVega  11 місяців тому +1

      I have done the same by just copying what others do without asking why they did it.

  • @simulbista8846
    @simulbista8846 11 місяців тому +1

    Thank you Dan. Very insightful.

  • @haroldpepete
    @haroldpepete 11 місяців тому +1

    very useful, i think some of those mistakes are more general and not just to spring, be restful, excepcion hanlder, and everything is public is more about encapsulation in object oriented

  • @alandosman5002
    @alandosman5002 11 місяців тому

    I really love the functional way to throw errors, we at our company we use a functional way to do Try and catch, which makes it readable and clean, we also dont throw an error if its needed by the user, we use a type Either and then return to left part which we defined an error with msg etc... we use Vavr for handling things with FP

  • @nasserabbassi7303
    @nasserabbassi7303 6 місяців тому

    thank you first all, it worth also to mention that using controllerAdvice we can handle the exceptions also much better in spring

  • @TonInter
    @TonInter 11 місяців тому

    Great video, thanks Dan! Looking forward to the exception video!

  • @tomislavkovacevic7085
    @tomislavkovacevic7085 7 місяців тому

    So in a package by feature approach, where do you put, for example, your Utility classes? Example DateTimeUtil. Since its not a part of any feature

  • @omaradrian80
    @omaradrian80 11 місяців тому

    Keep sharing good practices about coding with Spring.

  • @zaymaungmaungmyint8142
    @zaymaungmaungmyint8142 8 місяців тому

    Dan, Thank for sharning!
    In exception handling, what if we have the controller, service, and repository. Where would be the best place to handle the exception? What would you recommend?
    Controller or service? My throught is controller though.

  • @anoopktla
    @anoopktla 11 місяців тому +3

    #1 is going to be problematic in the long run IMO. Agree with you about all others!

  • @lengors7327
    @lengors7327 4 місяці тому

    Isnt patch usually the one used to update resources and put the one to add? And post usually for non-standard/more complex operations (e.g. triggering an sms)?

  • @raj-b-gowda
    @raj-b-gowda 8 місяців тому

    What about the "Resource Oriented design principle" used for designing the REST endpoints? It says that the endpoints should be named after the resources that they are dealing with. But when we are dealing with multiple resources, it says we should use nested endpoints following the resource hierarchy? Is it necessary to follow these?

  • @jopadjr
    @jopadjr 10 місяців тому

    Hi Dan, I recently came across an article highlighting Java vulnerabilities. In light of this, I believe it would be beneficial to create a playlist outlining the steps for: (1) Upgrading to the latest version of Java (2) Updating JAR files (3) Transitioning to the most recent dependencies. This playlist would serve as a valuable resource for us, subscribers, to proactively address potential security risks associated with outdated Java environments. Would you be available to create such a playlist? Thanks in advance.

  • @davidleonardobernal61
    @davidleonardobernal61 11 місяців тому

    Great video that tells me that I'm on the right track, but I have a doubt about point 1. By using private classes and having everything in the same package (something that is done a lot in spring projects) if we talk about feature architecture. Wouldn't it be a disadvantage in this case? Since we would surely have a shared domain between all the features and would we break with this encapsulation? What do you think or what alternative do you use for these cases?

  • @Phoenix-od2bp
    @Phoenix-od2bp 10 місяців тому

    Hi Dan, what is your opinion about putting business logic in config files like application yaml to make it extensible. Recently there was a requirement where data had to be filtered based on some fields and these fields could change in future. Someone suggested to put them in configuration files to make it future proof.

  • @alpsavasdev
    @alpsavasdev 6 місяців тому

    As per Interface usage, what about unit testing? How do we test a service if we cannot decouple its dependency via using Interfaces?

  • @FrankAn-io7bz
    @FrankAn-io7bz 7 місяців тому

    #3 I always thought it was right to use interfaces, but there's nothing right or wrong with that, personal habits? I agree with your point. Using interfaces without multiple implementations wastes time.

    • @FrankAn-io7bz
      @FrankAn-io7bz 7 місяців тому

      But using interfaces also hides the implementation details, right? The caller can use the functionality without caring about the details.

  • @marcincylkowski3004
    @marcincylkowski3004 11 місяців тому +1

    of course, it is sounds like very good series

  • @garethbaker5179
    @garethbaker5179 6 місяців тому

    To me number one is a design choice, having all the controllers in the controller package allows the developer to go straight to the package whereas creating packages based on functions lends itself to being able to pull that function out and turn it into a micro service with a lot less hassle. However, you will still end up having some common code in something like a utils package that all or most of the functions leverage it.
    Number five is an improper use of exceptions in that just because a piece of data wasnt found doesnt constitute something exceptional. It would be better to return an Optional and let the calling class figure out what to do. I believe its good practice to avoid throwing exceptions as much as possible because unwinding them and dealing with them slows down processing and Ive seen in high throughput applications where milliseconds count that less "throws" makes a difference.

    • @dinov5347
      @dinov5347 5 місяців тому

      Agree with you on both points. Just because java doesn't have extended ability of hiding classes between packages doesn't mean you should design as such. Having controllers in the same package as the service/models/repo doesn't make design sense to me. Second point is dependent on convention: in the old days of ejb getXXX would throw exceptions whereas findXXX would not. In the end, most usage will use the find approach since it should be left up to the caller to decide on what to do. Throwing the exception here makes no sense to me. It is not exceptional.

  • @rafaband
    @rafaband 3 дні тому

    About the interface mistake. In cases of services, I like to use to have more flexibility on refector. But i noticed some projects that the developers uses interfaces on controllers just to use annotations for springdoc. About the first one, if the project is a micro-service with only one responsibility domain, it still a mistake?! Thinking about one MS that manager a Shopping Cart, it probably have many model class and some external client call. Isn't made the package bigger and difficult to notice it interactions?!

  • @ratikantapradhan1537
    @ratikantapradhan1537 11 місяців тому +1

    In Mistake #1, how to write unit tests if service layer is private ?
    Do we expect to keep Unit test code in same package?

    • @hamzaelbouzidi7720
      @hamzaelbouzidi7720 2 місяці тому

      That's a good question and I hope Dan Vega can give us an answer.

  • @ravimangalagiri
    @ravimangalagiri 11 місяців тому

    Dan what theme are you using for the demo. just curious

  • @Vityoube
    @Vityoube 11 місяців тому

    Concerning the mistake 5: is there a way to also set response message (not only response code) for spring to render by some annotation?

  • @pedrolobo9835
    @pedrolobo9835 11 місяців тому +1

    Great video.
    There is something I changed recently, because I believe it is a better approach, according to Clean Architecture. Instead of making conversions between DTOs and Entities classes in the controller, I started creating a mapper in an isolated package and letting the services layer request these conversions. What do you think of the idea?

    • @JitterTed
      @JitterTed 11 місяців тому

      I put the conversions in the DTOs themselves, as they've got the data and it makes testing easy. According to Clean or Hexagonal architecture, the service layer should not be aware of those DTOs.

    • @pedrolobo9835
      @pedrolobo9835 11 місяців тому

      @@JitterTed Thanks for commenting. I'm not sure if I understood your suggestion correctly, but who asks to convert the DTOs? Is it the controllers or services layer?

    • @JitterTed
      @JitterTed 11 місяців тому +1

      @@pedrolobo9835The Controllers obtain (Domain) Objects from the Service layer, and then ask the DTO to convert them to the DTOs. This way the Service layer doesn't have to conform to any particular Controller's needs (it supplies Domain Objects). Typically I have DTOs specific to a Controller (or set of related Controllers).

    • @pedrolobo9835
      @pedrolobo9835 11 місяців тому

      @@JitterTed OK. Thanks for that! =)

  • @adamm4739
    @adamm4739 11 місяців тому

    Actually none of them are mistakes. I would call them tips and they are great. Thanks!

  • @manjosh1990
    @manjosh1990 11 місяців тому

    thanks, now I know my mistakes :D
    In my org I also see a lot of circular dependencies, all done through field autowiring. Could you explain if it is the correct way? should we avoid it?

  • @JimShingler
    @JimShingler 7 місяців тому

    This is great, ... Keep them coming

  • @kyleryxn
    @kyleryxn 11 місяців тому

    i love the feature package organization idea, can you make a video on it?

  • @ikennashonowo9250
    @ikennashonowo9250 11 місяців тому

    Kindly continue this series.

  • @finncrisp9900
    @finncrisp9900 11 місяців тому

    Looking forward to a video about @Transactional annotation and common mistakes while using it.

  • @feloniousmonkey
    @feloniousmonkey 10 місяців тому

    I agree with most ..a few points of refinement id make though..if you are writing published spring libs that are used elsewhere I would lean in favor of always providing interfaces or coding to a contract.
    I really didn't quite understand the point of arranging code in packages as prescribed although this isn't the first time I've seen this idea... I've never really seen a realistic advantage and if you are doing microservices and have so many different domains that your crossing wires and it's confusing perhaps you've misdrawn your context boundaries ?
    I like this series though and to add to common errors ...I have often come into projects that don't properly use http status codes , a good.example is rest calls that return 200 with an error payload you need to check for..yuck but I see it consistently.
    Exposing dB entities directly and not using dtos is another..and on that note manual mapping between the entity/dto layers when great tools exist for this.
    Another common mistake I see frequently is manual validation and not fully understanding the power of spring validation...
    Prob a dozen others but ...cheers I look forward to your next installment!

  • @Alexander-vov
    @Alexander-vov 11 місяців тому

    Thanks for good examples

  • @HandledToaster2
    @HandledToaster2 11 місяців тому +2

    A previous tech lead I had disagrees on the useless-interface mistake. They believe it's good pattern to ALWAYS have an interface and an impl for everything. I don't see any value on that if there's never going to be alternative implementations.

  • @kyleryxn
    @kyleryxn 11 місяців тому

    I'm a little confused about the same mappings, how will the controller know which one to call?

  • @카라멜땅콩-p2p
    @카라멜땅콩-p2p 11 місяців тому +1

    good topic and easy explanation

  • @austecon6818
    @austecon6818 4 місяці тому

    Great video. Thanks

  • @antonslonkin
    @antonslonkin 11 місяців тому

    thanks!!! please continue!!!

  • @sirfinsaan
    @sirfinsaan 11 місяців тому +1

    feature based is good but still require public , moslty need to talk to different package, very rare package is restricted not needed to talk

  • @isurucumaranathunga
    @isurucumaranathunga 11 місяців тому

    learned a lot, thanks Dan

  • @ficamu2
    @ficamu2 11 місяців тому

    Very useful content!
    Thanks!!

  • @pejko89
    @pejko89 11 місяців тому +1

    I'm so proud this video wasn't useful to me. I'm still not a junior, but I've had 2 internships. This is how I do stuff.
    But all of the bad examples show how I did things at some point in the past two years during my learning path

  • @alper_mulayim
    @alper_mulayim 6 місяців тому

    Hello Dan Vega, thank you amazing video! REST API standard has a lot of problem on developers. We see in corporate projects also. When I describe the problem people does not care. API should be standard and have rules, but people always trying to use method names. Resources seperated by rest api architecture principle but they are accessing via different resources. Also on the courses trainers they using method names in order to explain better. But developers think about it is correct. API structure is needed trained as an course and we need to focus like clean code. Clean API is success of work. Could you please provide the best practices in a video? Perhaps this could be make a recognition on training , tutorial videos.

  • @tamillll
    @tamillll 11 місяців тому +1

    Hi Dan, please add hexagonal architecture video

  • @sadiulhakim7814
    @sadiulhakim7814 8 місяців тому

    Learned some good stuff

  • @Vityoube
    @Vityoube 11 місяців тому

    What so interesting in case of mistake 3 Intellij in case of lone class implementation with only one method will suggest to make an interface from it :) This is the example, that actually not all the time IDE is suggesting the right thing :)

  • @vintage8
    @vintage8 11 місяців тому

    I wonder would it be better instead of throwing a TodoNotFoundException create a NotFoundException with a message property and throw it?

  • @eyasuemana6946
    @eyasuemana6946 11 місяців тому

    please to on package structuring in spring project. thanks!

  • @ismailforeveryone6889
    @ismailforeveryone6889 11 місяців тому +1

    please we need more

  • @joeLinton-w1p
    @joeLinton-w1p 6 місяців тому

    great content thank u

  • @Uttkarsh_8
    @Uttkarsh_8 11 місяців тому

    Great content!!

  • @bilelkhedira
    @bilelkhedira 2 місяці тому

    thanks!

  • @TheSlavaG
    @TheSlavaG 11 місяців тому

    in last example. better to just return optional from repository. And then throw exception from controller.

  • @emo_echoes
    @emo_echoes 11 місяців тому

    what about summer developers?

  • @jackofnotrades15
    @jackofnotrades15 11 місяців тому

    Not using constructor injection for @ConfigurationProperties.

  • @DanteMorius1
    @DanteMorius1 11 місяців тому +2

    Making everything package private and then putting into Spring context is not a great example of "don't make everything public". If something is in the context then it's available anywhere anyway...

  • @emmanuelU17
    @emmanuelU17 11 місяців тому +1

    Have mercy on folks that watch this at night time. Light mode is a no no 😭

  • @Spring_IoC
    @Spring_IoC 5 місяців тому

    Aren't repositories interfaces instead of classes?

  • @fakename1000
    @fakename1000 7 місяців тому

    Wait... some people arent using dark mode?

  • @douglascamiloduartedesousa8349
    @douglascamiloduartedesousa8349 11 місяців тому +6

    Hi, Dan! The github repo is private... =/

  • @ax8635
    @ax8635 11 місяців тому

    Mistake #6. Do not use streams everywhere and always, they are much slower in most cases.

  • @meikuv
    @meikuv 11 місяців тому

    Color theme name please

  • @vrjb100
    @vrjb100 11 місяців тому

    Mistake 6 improper medieatype or missing mediatype. All rest controllers should produce json mediatype and consume it for post/update/patch. There are clever hacks that use improper media type handling.

    • @cosminsel
      @cosminsel 11 місяців тому

      JSON is not a requirement of REST. You can use other formats.

  • @eyasuemana6946
    @eyasuemana6946 11 місяців тому

    love it

  • @nclbaz
    @nclbaz 11 місяців тому +1

    I'd say "5 Common Mistakes Spring WEB Developers Make"

  • @ayushjaiswal4449
    @ayushjaiswal4449 11 місяців тому +1

    great tutorial, I think you should make a LinkedIn Course for spring boot where you cover all aspects of spring such as web, security, data etc

  • @Joseph-oz7tx
    @Joseph-oz7tx 11 місяців тому

    great

  • @jopadjr
    @jopadjr 11 місяців тому

    245th...Thanks Dan

  • @sugankpms6169
    @sugankpms6169 11 місяців тому

    + light mode.

  • @Hnzk10
    @Hnzk10 11 місяців тому

    Hide the pain Dan

  • @alper_mulayim
    @alper_mulayim 6 місяців тому

    unnecessary interfaces we see in the corporate projects everywhere, sometimes people does not put methods too. because they learned by rote. I am disagree and removing when I see. delete like a butcher!

  • @pawelmarkisz8783
    @pawelmarkisz8783 10 місяців тому

    💖 Promo>SM

  • @joachimdietl6737
    @joachimdietl6737 11 місяців тому

    this interface mistake is not really a mistake. I do it all the time

  • @ellolazo
    @ellolazo 5 місяців тому

    this are lousy recommendations. review what you are saying. most of them are bad practices

  • @143aravinth
    @143aravinth 11 місяців тому +1

    The git repo is not available @DanVega

    • @DanVega
      @DanVega  11 місяців тому

      Should be now.

  • @techspinster
    @techspinster 11 місяців тому +1

    Num 2 is why I like lombok's @RequiredArgsConstructor