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
Refcards
Trend Reports

Events

View Events Video Library

Related

  • Spring Boot Application With Spring REST and Spring Data MongoDB
  • Upgrade Guide To Spring Boot 3.0 for Spring Data JPA and Querydsl
  • Spring Boot - How To Use Native SQL Queries | Restful Web Services
  • Less Code With Spring Data Rest

Trending

  • How to Detect Spam Content in Documents Using C#
  • Navigating the Complexities of AI-Driven Integration in Multi-Cloud Environments: A Veteran’s Insights
  • How to Build and Optimize AI Models for Real-World Applications
  • How AI Coding Assistants Are Changing Developer Flow
  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.

By 
Tapas Joshi user avatar
Tapas Joshi
·
Oct. 03, 19 · Tutorial
Likes (4)
Comment
Save
Tweet
Share
37.3K 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.

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

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

Opinions expressed by DZone contributors are their own.

Related

  • Spring Boot Application With Spring REST and Spring Data MongoDB
  • Upgrade Guide To Spring Boot 3.0 for Spring Data JPA and Querydsl
  • Spring Boot - How To Use Native SQL Queries | Restful Web Services
  • Less Code With Spring Data Rest

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

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 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook