0% found this document useful (0 votes)
14 views6 pages

REST Full CRUD Operations

Uploaded by

nameedyusuf
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views6 pages

REST Full CRUD Operations

Uploaded by

nameedyusuf
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Create REST controller to perform CRUD operations-

Step1:

Step2:
Package folder structure:
Step3:
package com.example.restapi;

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

@SpringBootApplication
public class RestapiApplication {

public static void main(String[] args) {


SpringApplication.run(RestapiApplication.class,
args);
}

Step4:
package com.example.restapi.model;

public class Product {


private Integer id;
private String name;

public Integer getId() {


return id;
}

public void setId(Integer id) {


this.id = id;
}

public String getName() {


return name;
}

public void setName(String name) {


this.name = name;
}
}

Step 5:
package com.example.restapi.controller;

import org.springframework.http.ResponseEntity;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;

import com.example.restapi.model.Product;

import java.util.HashMap;
import java.util.Map;
import java.util.Collection;

@RestController
@RequestMapping("/products")
public class ProductServiceController {
private static Map<Integer, Product> productRepo = new
HashMap<>();

static {
Product honey = new Product();
honey.setId(1);
honey.setName("Honey");
productRepo.put(honey.getId(), honey);

Product almond = new Product();


almond.setId(2);
almond.setName("Garlic");
productRepo.put(almond.getId(), almond);
}

@DeleteMapping("/{id}")
public ResponseEntity<Object> delete(@PathVariable("id")
Integer id) {
if (!productRepo.containsKey(id)) {
return new ResponseEntity<>("Product not found",
HttpStatus.NOT_FOUND);
}
productRepo.remove(id);
return new ResponseEntity<>("Product is deleted
successfully", HttpStatus.OK);
}

@PutMapping("/{id}")
public ResponseEntity<Object>
updateProduct(@PathVariable("id") Integer id, @RequestBody
Product product) {
if (!productRepo.containsKey(id)) {
return new ResponseEntity<>("Product not found",
HttpStatus.NOT_FOUND);
}
product.setId(id); // Set the ID in the product
productRepo.put(id, product); // Update the product
return new ResponseEntity<>("Product is updated
successfully", HttpStatus.OK);
}

@PostMapping
public ResponseEntity<Object> createProduct(@RequestBody
Product product) {
if (productRepo.containsKey(product.getId())) {
return new ResponseEntity<>("Product with this ID
already exists", HttpStatus.CONFLICT);
}
productRepo.put(product.getId(), product);
return new ResponseEntity<>("Product is created
successfully", HttpStatus.CREATED);
}

@GetMapping
public ResponseEntity<Collection<Product>>
getAllProducts() {
return new ResponseEntity<>(productRepo.values(),
HttpStatus.OK);
}
}

Output:
`

You might also like