Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

Getting Started With Spring Boot and Spring Data REST

DZone 's Guide to

Getting Started With Spring Boot and Spring Data REST

Get started with Spring Boot and Spring Data REST in this short tutorial.

· Java Zone ·
Free Resource

Developer looking at Spring Data REST and Spring Boot

Get started with Spring Boot and Spring Data REST in this short tutorial.

To start working on Spring Data REST, we need to have some basic knowledge of JPA and the Spring Data JPA, as it is built on top of these projects. You can refer to the following links for documentation on Spring Data JPA and Spring Data REST.

You may also like: Introduction to Spring Data REST

Step 1: Add the Following Dependencies to the Project

// Dependency for Spring data jpa
<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
</dependency>
// Dependency for Spring data rest
<dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-rest</artifactId>
</dependency>


Step 2: Creating a REST Repository

Next, you need to create one repository using any of the repository providers (JpaRepositoryPaggingAndSortingRepositoryCrudRepository) by extending them.

/**
 *
 */
package com.trjoshi.dao.api;

import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.rest.core.annotation.RepositoryRestResource;

import com.trjoshi.bean.SpringDataRestConfig;

/**
 * @author tapasjoshi
 *
 */
@RepositoryRestResource(collectionResourceRel = "springDataRestTestConfigs", path = "springDataRestTestConfigs")
public interface SpringDataRestTestConfigRestRepository extends JpaRepository<SpringDataRestConfig, String> {

}


In the above code snippet, we have added the following annotation where we can provide the collectionResourceRel and the path to specify the REST service and key of the response.

 @RepositoryRestResource(collectionResourceRel = "springDataRestTestConfigs", path = "springDataRestTestConfigs") 

By adding this annotation to the repository, the Spring container will create all of the REST services and CRUD operations for this entity, which we can customize as per our needs.

Step 3: Enable Spring Data REST in Your Spring Boot Application

To enable Spring Data REST in your Spring Boot application, you need to add the following annotation to the main Spring Boot class.

@Import(SpringDataRestConfiguration.class):

package com.trjoshi.springboot;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.support.SpringBootServletInitializer;
import org.springframework.context.annotation.Import;

import springfox.documentation.spring.data.rest.configuration.SpringDataRestConfiguration;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@SpringBootApplication
@EnableSwagger2
@Import(SpringDataRestConfiguration.class)
public class SpringBootDemoApplication extends SpringBootServletInitializer{

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return builder.sources(SpringBootDemoApplication.class);
    }

    public static void main(String[] args) {
        SpringApplication.run(SpringBootDemoApplication.class, args);
    }
}


Now, we are done with the minimum required steps for implementing Spring Data REST, and once we deploy the application, our REST services will be ready, as below.

GET : <Context Path>/springDataRestTestConfigs

POST : <Context Path>/springDataRestTestConfigs

DELETE : <Context Path>/springDataRestTestConfigs/{id}

GET : <Context Path>/springDataRestTestConfigs/{id}

PUT : <Context Path>/springDataRestTestConfigs/{id}


Thanks for reading. Please share your thoughts and questions in the comments section.

Further Reading

The Magic of Spring Data

Introduction to Spring Data REST

Topics:
spring data rest ,spring boot ,spring data ,tutorial ,getting started ,example ,code

Opinions expressed by DZone contributors are their own.

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}