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 Video Library
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
View Events Video Library
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
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

Migrate, Modernize and Build Java Web Apps on Azure: This live workshop will cover methods to enhance Java application development workflow.

Modern Digital Website Security: Prepare to face any form of malicious web activity and enable your sites to optimally serve your customers.

Kubernetes in the Enterprise: The latest expert insights on scaling, serverless, Kubernetes-powered AI, cluster security, FinOps, and more.

E-Commerce Development Essentials: Considering starting or working on an e-commerce business? Learn how to create a backend that scales.

Related

  • Kotlin: How to Implement a REST API With Spring Boot, Spring Data, and H2 DB
  • Spring Boot - Microservice- JaxRS Jersey - HATEOAS - JerseyTest - Integration
  • Upgrade Guide To Spring Boot 3.0 for Spring Data JPA and Querydsl
  • Spring Boot - How To Use Native SQL Queries | Restful Web Services

Trending

  • Kubernetes vs. Amazon ECS: Container Orchestration Comparison
  • Unlocking Performance: Exploring Java 21 Virtual Threads [Video]
  • Ultimate Guide to Smart Agriculture Systems Using IoT
  • Unlocking Clean Code: 20 Architectural Tips for Every Developer
  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.6K 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.

Related

  • Kotlin: How to Implement a REST API With Spring Boot, Spring Data, and H2 DB
  • Spring Boot - Microservice- JaxRS Jersey - HATEOAS - JerseyTest - Integration
  • Upgrade Guide To Spring Boot 3.0 for Spring Data JPA and Querydsl
  • Spring Boot - How To Use Native SQL Queries | Restful Web Services

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
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends: