Introduction
Spring Boot is by far one of the most commonly used frameworks in the world of Java today. The speed at which you can build a fully functioning REST API as well as all the other advantages of Java as a language makes Spring Boot a must learn when considering tooling for your next Java project.
Where should you start?
Spring Boot has a great “initializer” to quickly create a Spring Boot application. It gives you the freedom to choose your build tool, naming and any dependencies. See https://start.spring.io/.
Standard dependencies for a Spring Boot REST API, usually sitting on top of a database would be:
- Spring Web
- Spring Data JPA
- A database driver of your choice (E.g. PostgreSQL, MySQL, H2 etc)
How it works?
Magic! No seriously, it might sound strange but Spring Boot is designed to hide a lot of configuration from the developer to speed up and make us more efficient at achieving what we need to so a lot of the functionality is seen as magic. The core of it is, you decorate your classes with special Spring Boot annotations and you can Autowire (inject) that class into another! Each annotation will do something different, see the following examples:
- @RestController — defines a class that will usually be the entry point to your application via a REST API. Methods in this class can be assigned API method annotations to dictate its usage, e.g. POST or GET.
@RestController public class HelloResource { @GetMapping public String helloWorld() { return "hello world"; } @PostMapping public void postHello(@RequestBody String hello) { // do something with the string! }}
- @Repository — defines a class that will act as somewhere to get data from. Usually a Database!
@Repository public interface UesrRepository extends JpaRepository<User, Long> { User findByUsername(String username); }
- @Service — a basic common annotation, usually for classes with the bulk of the business logic for the application.
@Service public class UserService implements IUserService { @Override public User getUserById(String username) { return userRepo.findByUsername(username); } }
Useful libraries
Spring Boot provides a lot of libraries that cover most things you may want to use when creating a Spring Boot application and a lot are available when generating using the Spring initializer. I’ll cover a few common important ones that you may wish to use!
- Spring Web
Spring Web allows a developer to use the @RestController annotation described above and embeds Apache Tomcat into your application which basically starts a web service.
- Spring Security
Spring Security allows a developer to implement access control and authentication into their application. For example, allow a user to access a book API endpoint, but not the admin endpoints.
- Spring Data JPA
Spring Data JPA enables the @Repository annotation described above. Combined with the Java Persistence API to allocate IDs and Column names to a POJO, it becomes a very easy to use library for data persistence and retrieval.
- Spring for Apache ActiveMQ
ActiveMQ is a queuing/topic application that you can easily subscribe to, read from or sent to using this library. It’s easily configurable using the Spring Boot application.properties file. It is extremely useful when trying to design high traffic asynchronous applications.
Closing Thoughts
Spring Boot’s simplicity can make creating and getting a working application incredibly easy, out of the box. Simplicity, however, is not always a good thing and some of the “Magic” we encounter can also be a limitation as it can mask issues. Overall it is an enjoyable and rewarding framework to use, but as with any framework, it is what you make of it that counts!