Augmenting Spring Data REST with Additional Endpoints

Spring Data REST combines the features of Spring Data with Spring HATEOAS to make it easy to build hypermedia-driven REST APIs on top of Spring Data repositories. The basic functionality provided out of the box creates and exposes simple REST endpoints for performing CRUD operations on Spring Data repositories. For a lot of use cases, this is entirely enough functionality to meet your needs. In other cases, you need to extend the REST API to include additional functionality that isn’t provided by Spring Data REST and it can be difficult to determine exactly how to do this. In this blog post, I show you how to augment a Spring Data REST API with additional endpoints to turn a basic CRUD API into a full featured web service. ...

May 19, 2021 · 3 min · Kevin Sookocheff

Understanding Spring's Environment Abstraction

When working with Spring Boot, some auto-configuration can happen seemingly by magic. For example, in a traditional Spring application with Java-based configuration, you might configure an H2 database using the following block of code to set the type of the database and run some scripts to initialize it: @Bean public DataSource dataSource() { return new EmbeddedDataSourceBuilder() .setType(H2) .addScript("taco_schema.sql") .addScripts("user_data.sql", "ingredient_data.sql") .build(); } With Spring Boot, you can remove this entire block of code and use auto-configuration. If the H2 dependency is available in the classpath, Spring Boot will automatically create the DataSource and add it as a Bean to Spring’s application context and apply SQL scripts as long as they are named schema.sql or data.sql. Spring Boot does this auth-configuration by leveraging the Spring environment that collects the set of properties available to a Java application and uses those properties to configure beans in Spring’s application context. The following figure from Spring in Action shows how the Spring environment is generated by some of the available configuration property sources available in a typical Java application. ...

May 27, 2020 · 3 min · Kevin Sookocheff

Building a Modern Java Web App with Spring Boot

I’m a fan of Java. If you haven’t given it a chance in a while, you may not have noticed that, Java has quietly been adopting some of the best practices that make dynamic and functional languages so appealing, without sacrificing the many hundred person-years of effort that have made Java and the JVM a world-class development environment. Java is still one the world’s most popular programming languages with roughly 9 million programmers using Java. It doesn’t make sense to ignore all of that history and development effort because of a niggling feeling that Java is a little too verbose, or that XML is so last year. ...

May 20, 2020 · 10 min · Kevin Sookocheff

Inversion of Control, Dependency Injection, and the Spring IoC Container

Inversion of Control (IoC), also known as Dependency Injection (DI), allows an object to define their dependencies as constructor arguments (strictly speaking, you can set these dependencies as properties, but the examples I will use today are constructor-based). This is the inverse of the object itself controlling the instantiation or location of its dependencies, hence the name Inversion of Control. Let’s look at an example from Stackoverflow using a text editor with a spell checking component: ...

May 14, 2020 · 5 min · Kevin Sookocheff