6

I am trying to understand various dependencies of spring boot. I have come across three of them:

  1. spring-boot-starter-data-rest
  2. spring-boot-starter-data-jpa
  3. spring-boot-starter-data-jdbc

I would like to know the difference between the three. Tried searching online documents, which say the three are almost related to spring data. how to resolve the confusion?

To add in here I have also found an another dependency spring-boot-starter-web-services. I think it supports both SOAP and REST. Its just my assumption, I waiting for an explanation

Learner
  • 73
  • 5
  • 2
    Does this answer your question? [spring data rest vs spring data jpa](https://stackoverflow.com/questions/33422849/spring-data-rest-vs-spring-data-jpa) – omar jayed Sep 16 '21 at 18:37

2 Answers2

13

spring-boot-starter-data-jpa is used to access your database with JPA (Java Persistence API)

spring-boot-starter-data-jdbc is used to access your data with jdbc (Java Database Connectivity)

the difference between JPA and JDBC is the level of abstraction. JDBC is more low level, JPA is more 'magic'

and spring-boot-starter-data-rest is used to provide Rest Endpoints on top of your Spring Data repositories.

To do this, you just annotate your spring data repository with a RepositoryRestResource annotation and direct Spring MVC creates the Restful endpoints.

@RepositoryRestResource(collectionResourceRel = "people", path = "people")
public interface PersonRepository extends PagingAndSortingRepository<Person, Long> {

  List<Person> findByLastName(@Param("name") String name);

}

This is very handy if you do very CRUD like (Create, Read, Update, Delete) applications.

Andreas Radauer
  • 1,083
  • 7
  • 18
  • 1
    how do we provide rest endpoints on spring data repositories. Can you please please add an example inside your answer support it – Learner Sep 17 '21 at 03:35
  • one more question here, how does spring mvc creates rest endpoints and how do a user or a service know the end points. Oh is it the path="people" attribute? I am considering that the same is applicable for spring boot applications as well – Learner Sep 17 '21 at 06:08
5

JPA means "Java Persistence API". It's for querying or saving data in relational databases using object-relational mapping.

REST means "REpresentational State Transfer". It's a style for creating web services that leverages HTTP verbs, a simpler alternative to SOAP.

JDBC starter uses the Spring JdbcTemplate to query or save data in relational databases without relying on object-relational mapping. You write SQL and ask the JdbcTemplate class to execute it in the database using JDBC.

I would say JPA and JDBC starters should be mutually exclusive: either one or the other.

You use REST only if you're writing web services. These may or may not query or persist data in a relational database. I would expect to see both the REST starter and a persistence starter in a pom if a REST service needed persistent data.

Three very different starters.

You ask why Spring Boot has a REST data starter parent that combines the two. In a word: convenience.

duffymo
  • 305,152
  • 44
  • 369
  • 561
  • just like to know, however we have spring-boot-starter-web which already support REST, then why should we need spring-boot-starter-data-rest, is it for exposing rest end points on repos. – Learner Sep 17 '21 at 03:34
  • 1
    Spring Boot tends to be very opinionated. They offer the Spring data starter to make creating REST data services easier, but they also have the separate data and REST starters b/c they can be used in other use cases. Think of it as a restaurant with both a la carte and price fixe menus. Pick the dependencies that are appropriate for your problem and move on. – duffymo Sep 17 '21 at 11:36