Java Spring Boot REST Application Part 2: Queries

In the first part we created a simple REST application to provide access to our employees data. But in most of the cases you also need to query the data. Spring Data Repositories provide a simple way to do so. Let’s take the employees repository interface we’ve created and add an additional method for searching employees by their last name.

package at.grisa.example.rest_example;

import java.util.List;

import org.springframework.data.repository.PagingAndSortingRepository;
import org.springframework.data.repository.query.Param;

public interface EmployeeRepository extends PagingAndSortingRepository<Employee, Long> {
 List<Employee> findByLastNameIgnoreCase(@Param("lastName") String lastName);
}

Spring Data automatically recognizes the method findByLastNameIgnoreCase as a query by parsing the method name.

Continue…

Java Spring Boot REST Application

RESTful webservices are a simple and effective way to provide access to your application’s data. With Spring Boot you are able to create such an interface within minutes. The example code can be found here on GitHub.

Start by creating a new Maven project and set the spring-boot-starter-parent artifact as parent. For the RESTful interface you need the spring-boot-starter-rest and for the database access the spring-boot-starter-data dependency. In this example the embedded in-memory database H2 is used.
Continue…