Monday 16 December 2019

Spring Data Rest: Change the name of Rest Resource

By default, spring data rest follows below pattern while exposing the data from repositories.

http(s)://server:port/baseURI/{plural_form_of_model_class}

For example, if
protocol is http
server is "localhost",
port is 8080
baseURI is "/api", and
model class is "Employee", then below uri is generated by spring data rest.

http://localhost:8080/api/employees

How to change the name of REST resource?
Use @RepositoryRestResource annotation to change the path.

Example
@RepositoryRestResource(path="myemployees")
public interface EmployeeRepository extends CrudRepository<Employee, Integer> {
  .....
  .....
}

Update EmployeeRepository like below.

EmployeeRepository.java
package com.sample.app.repository;

import java.util.List;

import org.springframework.data.repository.CrudRepository;
import org.springframework.data.rest.core.annotation.RepositoryRestResource;

import com.sample.app.model.Employee;

@RepositoryRestResource(path="myemployees")
public interface EmployeeRepository extends CrudRepository<Employee, Integer> {

  // SELECT * FROM employee WHERE salary=X
  public List<Employee> findBySalary(double salary);

  // SELECT * FROM employee WHERE lastName=X
  public List<Employee> findByLastName(String lastName);

  // SELECT * FROM employee WHERE age>X
  public List<Employee> findByAgeGreaterThan(int age);

  // SELECT * FROM employee WHERE age=X AND salary=Y
  public List<Employee> findByAgeAndSalary(int age, double salary);

}

Total project structure looks like below.

Run App.java, you can see below messages in console.
  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::        (v2.1.6.RELEASE)

Hibernate: 
    
    drop table employee if exists
Hibernate: 
    
    drop sequence if exists hibernate_sequence
Hibernate: create sequence hibernate_sequence start with 1 increment by 1
Hibernate: 
    
    create table employee (
       employee_id integer not null,
        age integer,
        first_name varchar(255),
        last_name varchar(255),
        salary double,
        primary key (employee_id)
    )
2019-07-10 11:35:24.618  WARN 11920 --- [           main] aWebConfiguration$JpaWebMvcConfiguration : spring.jpa.open-in-view is enabled by default. Therefore, database queries may be performed during view rendering. Explicitly configure spring.jpa.open-in-view to disable this warning
Hibernate: 
    call next value for hibernate_sequence
Hibernate: 
    insert 
    into
        employee
        (age, first_name, last_name, salary, employee_id) 
    values
        (?, ?, ?, ?, ?)
Hibernate: 
    call next value for hibernate_sequence
Hibernate: 
    insert 
    into
        employee
        (age, first_name, last_name, salary, employee_id) 
    values
        (?, ?, ?, ?, ?)
Hibernate: 
    call next value for hibernate_sequence
Hibernate: 
    insert 
    into
        employee
        (age, first_name, last_name, salary, employee_id) 
    values
        (?, ?, ?, ?, ?)
Hibernate: 
    call next value for hibernate_sequence
Hibernate: 
    insert 
    into
        employee
        (age, first_name, last_name, salary, employee_id) 
    values
        (?, ?, ?, ?, ?)
Hibernate: 
    call next value for hibernate_sequence
Hibernate: 
    insert 
    into
        employee
        (age, first_name, last_name, salary, employee_id) 
    values
        (?, ?, ?, ?, ?)


Open the url "http://localhost:8080/api/myemployees" in browser, you can see all the employee details.


You can download complete working application from this link.

Previous                                                    Next                                                    Home

No comments:

Post a Comment