DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports Events Over 2 million developers have joined DZone. Join Today! Thanks for visiting DZone today,
Edit Profile Manage Email Subscriptions Moderation Admin Console How to Post to DZone Article Submission Guidelines
View Profile
Sign Out
Refcards
Trend Reports
Events
Zones
Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones AWS Cloud
by AWS Developer Relations
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Partner Zones
AWS Cloud
by AWS Developer Relations

Trending

  • Front-End: Cache Strategies You Should Know
  • Guide To Selecting the Right GitOps Tool - Argo CD or Flux CD
  • How To Backup and Restore a PostgreSQL Database
  • Insider Threats and Software Development: What You Should Know
  1. DZone
  2. Coding
  3. Frameworks
  4. Getting Started With Spring Boot and Spring Data REST

Getting Started With Spring Boot and Spring Data REST

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

Tapas Joshi user avatar by
Tapas Joshi
·
Oct. 03, 19 · Tutorial
Like (4)
Save
Tweet
Share
36.38K Views

Join the DZone community and get the full member experience.

Join For Free

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 (JpaRepository, PaggingAndSortingRepository, CrudRepository) 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

Spring Framework REST Web Protocols Spring Data Spring Boot Data (computing)

Opinions expressed by DZone contributors are their own.

Trending

  • Front-End: Cache Strategies You Should Know
  • Guide To Selecting the Right GitOps Tool - Argo CD or Flux CD
  • How To Backup and Restore a PostgreSQL Database
  • Insider Threats and Software Development: What You Should Know

Comments

Partner Resources

X

ABOUT US

  • About DZone
  • Send feedback
  • Careers
  • Sitemap

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 600 Park Offices Drive
  • Suite 300
  • Durham, NC 27709
  • support@dzone.com

Let's be friends: