Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

JPA Repository and Spring-data-rest baseURi is /profile

Just learning springboot (and newish to java: from .NET world)

Too PS course on spring-data and spring-data-rest. All went well

Made a test project connection to MS SQlServer. Created a few JPA Repos and unit tests pass for FindAll

I don't have a base-uri set in app properties and when explore the rest interface (with Postman) everything appears under /profile.

{
    "_links": {
        "self": {
            "href": "http://localhost:8080/profile"
        },
        "users": {
            "href": "http://localhost:8080/profile/users"
        },
        "tasks": {
            "href": "http://localhost:8080/profile/tasks"
        }
    }
}

first question is, where does /profile come from?

like image 812
mark1234 Avatar asked Aug 13 '17 21:08

mark1234


People also ask

What is JPA Repository in Spring?

JPA Repository is mainly used for managing the data in a Spring Boot Application. We all know that Spring is considered to be a very famous framework of Java. We mainly use this Spring Boot to create the Spring-based stand-alone and production-based applications with a very minimal amount of effort.

What is used for exposing Spring data repositories over rest using Spring data rest?

Spring Data REST can be used to expose HATEOAS RESTful resources around Spring Data repositories. Without writing a lot of code, we can expose RESTful API around Spring Data Repositories.

What is rest Repository in Spring boot?

This repository is an interface that lets you perform various operations involving Person objects. It gets these operations by extending the PagingAndSortingRepository interface that is defined in Spring Data Commons. At runtime, Spring Data REST automatically creates an implementation of this interface.

What is difference between CrudRepository and JpaRepository?

CrudRepository mainly provides CRUD operations. PagingAndSortingRepository provide methods to perform pagination and sorting of records. JpaRepository provides JPA related methods such as flushing the persistence context and deleting of records in batch.


1 Answers

It's not a base path (url). It's a normal work of SDR:

A profile link, as defined in RFC 6906, is a place to include application-level details. The ALPS draft spec is meant to define a particular profile format, which we explore later in this section.

If you navigate into the profile link at localhost:8080/profile, you see content resembling the following:

{
  "_links" : {
    "self" : {
      "href" : "http://localhost:8080/profile"
    },
    "persons" : {
      "href" : "http://localhost:8080/profile/persons"
    },
    "addresses" : {
      "href" : "http://localhost:8080/profile/addresses"
    }
  }
}

To work with your entities you have to use these links:

  • http://localhost:8080/users
  • http://localhost:8080/tasks

By the way you can set 'base path' in three ways:

  1. In 'application.properties`

    spring.data.rest.basePath=/api

  2. Registering a bean

    @Bean
    public RepositoryRestConfigurer repositoryRestConfigurer() {
    
      return new RepositoryRestConfigurerAdapter() {
    
        @Override
        public void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) {
          config.setBasePath("/api");
        }
      };
    }
    
  3. With a custom implementation of RepositoryRestConfigurer

    @Component
    public class CustomizedRestMvcConfiguration extends RepositoryRestConfigurerAdapter {
    
      @Override
      public void configureRepositoryRestConfiguration(RepositoryRestConfiguration config) {
        config.setBasePath("/api");
      }
    }
    
like image 65
Cepr0 Avatar answered Dec 07 '22 23:12

Cepr0