7 Simple Steps to Write a Better API Using Spring Boot that You Should Know

Why no one taught me that?

Image for post
Image for post
Why no one taught me that? Photo by Ben White on Unsplash

Hi there! I hope that you are having a great day, today we will talk about some advice that I consider essentials when programming a Spring Boot application. All of them are based on my experience working with that and are subjective of course, let’s go for it.

1. Organize the project properly

2. Use @RestController

Image for post
Image for post
Endpoint example in a @RestController class (Made with https://carbon.now.sh)

The endpoint definition looks very clean this way, you can specify the HTTP code returned with another annotation and a lot of more details if you want.

3. Use Optional as a return parameter if necessary on the repositories, entities and models.

Image for post
Image for post
Using a repository with optional return value (Made with https://carbon.now.sh)

This is a huge improvement, we can get the result and throw an exception or use a default value very easily using the Optional tools.

Also, another thing that I recommend very much is to return Optionals on the getters of your models or your entities if the attributes can be null, just to be sure that the person that codes with that later is treating the value as it should be. With collections I prefer to return an empty list, so the person that uses it doesn’t have to unwrap the optional, just use the list normally. This simple approach can save us a lot of time in the future, I’m used to doing it this way:

Image for post
Image for post
Example of getters with optional values (Made with https://carbon.now.sh)

You can read a little more about optionals in my last article here if you want :)

4. Use dependency injection in constructor, avoid dependency injection on fields

  • Hidden dependencies: if someone wants to create an object from a class that uses field-based dependency injection they couldn’t do it properly because the @Autowired annotated variable is not required by the constructor and it will be null. Using dependency injection through the constructor we can clearly see what is needed to create an object.
  • Can’t define autowired fields as final because the dependency injector container has to modify it after the instance is created.

5. Keep models and entities separate

6. Validate the attributes of the models and entities with annotations

Image for post
Image for post
Example of a model with validations (Made with https://carbon.now.sh)

You can use the same approach to validate your entities and your application will be free of validations in the business layer. You can even create your own annotations for custom validations, I always use one to avoid null values inside of lists, just like this:

Image for post
Image for post
Example using the @NotNullValues annotation (Made with https://carbon.now.sh)

7. Use @ControllerAdvice to control exceptions

Image for post
Image for post
Controller advice example (Made with https://carbon.now.sh)

Using the @ControllerAdvice you only have to catch the exceptions that you want and specify the response that you want to return in every case. This can be complemented with a good exception hierarchy and you will have a great exception management.

I hope that you enjoyed reading! If you want to read further about the terms that I used here I’ll give you some links to great tutorials:

empathy.co

Helping brands provide irresistible search.

Cristian González Morante

Written by

Backend engineer at empathy.co — I’m passionate about technology and software development. Love spending time learning and sharing knowledge with everyone ^^

empathy.co

Helping brands provide irresistible search. Pairing software with interfaces to combine function and beauty in one. From mere results to meaningful relations and engaging interactions.

Cristian González Morante

Written by

Backend engineer at empathy.co — I’m passionate about technology and software development. Love spending time learning and sharing knowledge with everyone ^^

empathy.co

Helping brands provide irresistible search. Pairing software with interfaces to combine function and beauty in one. From mere results to meaningful relations and engaging interactions.

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store