Pure Restful web services with Spring Data Rest

What is Pure Restful Web-service ?

Http Status Codes
  1. Projection
  2. Search
  3. Advance Search
  4. Pagination & Sorting
  5. Spring Security
package dev.firelimez.io.domain;


import org.springframework.data.annotation.Id;


public class Product {
@Id
private String productId;
private String name;
private String price;

public String getProductId() {
return productId;
}

public void setProductId(String productId) {
this.productId = productId;
}

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getPrice() {
return price;
}

public void setPrice(String price) {
this.price = price;
}
}
package dev.firelimez.io.domain;

import org.springframework.data.annotation.Id;
import org.springframework.data.mongodb.core.mapping.DBRef;

import java.util.List;

public class Sale {
@Id
private String salesId;
private String date;

@DBRef
private SalesAgent salesAgent;

@DBRef
private List<Product> products;

public String getSalesId() {
return salesId;
}

public void setSalesId(String salesId) {
this.salesId = salesId;
}

public String getDate() {
return date;
}

public void setDate(String date) {
this.date = date;
}

public List<Product> getProducts() {
return products;
}

public void setProducts(List<Product> products) {
this.products = products;
}

public SalesAgent getSalesAgent() {
return salesAgent;
}

public void setSalesAgent(SalesAgent salesAgent) {
this.salesAgent = salesAgent;
}
}
package dev.firelimez.io.domain;


import org.springframework.data.annotation.Id;

import java.io.Serializable;

public class SalesAgent implements Serializable {

@Id
private String agentId;
private String name;
private String lastName;
private String age;

public String getName() {
return name;
}

public void setName(String name) {
this.name = name;
}

public String getLastName() {
return lastName;
}

public void setLastName(String lastName) {
this.lastName = lastName;
}

public String getAge() {
return age;
}

public void setAge(String age) {
this.age = age;
}

public String getAgentId() {
return agentId;
}

public void setAgentId(String agentId) {
this.agentId = agentId;
}
}
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.5.9.RELEASE</version>
</parent>

<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-rest</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-security</artifactId>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>2.8.2</version>
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
</plugins>
</build>
package dev.firelimez.io.repo;

import dev.firelimez.io.domain.Product;
import org.springframework.data.mongodb.repository.MongoRepository;
import org.springframework.data.repository.query.Param;
import org.springframework.data.rest.core.annotation.RepositoryRestResource
import org.springframework.data.rest.core.annotation.RestResource;
import java.util.List;

@RepositoryRestResource(collectionResourceRel = "products", path = "products")
public interface ProductRepository extends MongoRepository<Product, String> {

@RestResource(path = "names", rel = "demo1")
List<Product> findByName(@Param("name") String name);


@RestResource(path = "similar", rel = "demo")
List<Product> findByNameLike(@Param("name") String name);

@Override
@RestResource(exported = true)
void delete(String productId);
}
package dev.firelimez.io;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

@SpringBootApplication
public class StarterApplication {

public static void main(String[] args) {
SpringApplication.run(StarterApplication.class, args);
}
}
server:
port: 9008

spring:
data:
mongodb:
host: 127.0.0.1
port: 27017
database: sales_management
Postman request

Projection

package dev.firelimez.io.domain.projection;

import dev.firelimez.io.domain.Sale;
import org.springframework.data.rest.core.config.Projection;

import java.util.List;

@Projection(name = "expand", types = Sale.class)
interface SalesProjection {

String getSalesId();

String getDate();

SalesAgentProjection getSalesAgent();

List<ProductProjection> getProducts();
}
package dev.firelimez.io.domain.projection;

import org.springframework.beans.factory.annotation.Value;

interface SalesAgentProjection {

int getAge();

String getAgentId();

@Value("#{target.name} #{target.lastName}")
String getFullName();
}
http://localhost:9008/sales?projection=expand

Search & Advance Search

@RestResource(path = "names", rel = "name")
List<Product> findByName(@Param("name") String name);
@Autowired
SalesAgentRepository salesAgentRepository;

@RequestMapping(method = RequestMethod.POST, path = "/api/v2/agent/search/advance", consumes = MediaType.APPLICATION_JSON_VALUE)
@ResponseBody
public List<SalesAgent> claimSupportTicket(@RequestBody SalesAgent salesAgent) {
ExampleMatcher exampleMatcher = ExampleMatcher.matching().withIgnoreNullValues().withIgnoreCase();
List<SalesAgent> advanceSearch = salesAgentRepository.findAll(Example.of(salesAgent, exampleMatcher));
return advanceSearch;
}
Postman for pagination
Postman with pagination

--

--

--

Technical Lead. Passionate about cloud computing & web security | Freelance Mobile Developer | AWS Community Builder

Love podcasts or audiobooks? Learn on the go with our new app.

Recommended from Medium

Testing a Smart Contract with OpenZeppelin Tools

Coding like a drummer

My “Microservices Best Practices on Kubernetes” Talk on booking.com

GitHub Copilot review

Character Encoding

Building reliability into uncertain event delivery.

Shoppers queuing at the supermarket

Article 5. How To Deploy New VPS (For Hosting Your Masternode And Setup CLI Wallet)

Basic Formulas & Functions with Excel

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store
Sajith vijesekara

Sajith vijesekara

Technical Lead. Passionate about cloud computing & web security | Freelance Mobile Developer | AWS Community Builder

More from Medium

H2 In-memory Database

What is a Spring Boot Fat Jar

Spring Security (series) — GitHub SSO #5

Spring boot Starter Project In STS